Swift:长按手势识别器 – 检测轻敲和长按
我想连线一个动作,如果这个动作是一个轻敲,它确实以一种特定的方式来animation一个对象,但是如果这个动作的持续时间超过0.5秒,它就会做其他事情。
现在,我只是把animation连上了。 我不知道如何区分长按和水龙头? 如何获得新闻发布时间以达到上述目的?
@IBAction func tapOrHold(sender: AnyObject) { UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: { UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: { self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2)) }) UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: { self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2)) }) UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: { self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2)) }) }, completion: { (Bool) in let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView") self.showViewController(vc as UIViewController, sender: vc) })
定义两个IBActions
并为其中的每个设置一个Gesture Recognizer
。 这样,您可以为每个手势执行两个不同的操作。
您可以将每个Gesture Recognizer
器设置为界面生成器中的不同IBActions。
@IBAction func tapped(sender: UITapGestureRecognizer) { println("tapped") //Your animation code. } @IBAction func longPressed(sender: UILongPressGestureRecognizer) { println("longpressed") //Different code }
通过代码没有接口生成器
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:") self.view.addGestureRecognizer(tapGestureRecognizer) let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:") self.view.addGestureRecognizer(longPressRecognizer) func tapped(sender: UITapGestureRecognizer) { println("tapped") } func longPressed(sender: UILongPressGestureRecognizer) { println("longpressed") }
通过代码没有接口生成器
// Global variables declaration var longPressed = false var selectedRow = 0 override func viewDidLoad() { super.viewDidLoad() let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ContactListTableViewController.handleLongPress(_:))) longPressGesture.minimumPressDuration = 1.0 // 1 second press longPressGesture.allowableMovement = 15 // 15 points longPressGesture.delegate = self self.tableView.addGestureRecognizer(longPressGesture) } // Long tap work goes here !! if (longPressed == true) { if(tableView.cellForRowAtIndexPath(indexPath)?.accessoryType == .Checkmark){ tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None self.selectedRow -= 1 if(self.selectedRow == 0){ self.longPressed = false } } else { self.selectedRow += 1 tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark } } else if(self.selectedRow == 0) { // Single tape work goes here !! }
但唯一的问题是长按手势运行两次。 如果您发现任何解决scheme,请在下面评论!