2014-10-06 76 views
1

我有一个应用于它的轻击手势的视图。当手指抬起时,我想让视线“缩小”,并且让视图恢复正常。我试图用UIGestureRecognizerState来达到这个目的,但它不起作用。只有当我移开手指并且不回去时,视图才会缩小。这里是我的代码:点击手势与UIGestureRecognizerState不起作用

@IBAction func shareButton(sender: AnyObject) { 

    if sender.state == UIGestureRecognizerState.Changed { 
     UIView.animateWithDuration(0.1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, animations: { 
      self.shareButton.transform = CGAffineTransformMakeScale(0.9, 0.9) 
     }, completion: nil) 
    } else if sender.state == UIGestureRecognizerState.Ended { 
     UIView.animateWithDuration(0.1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, animations: { 
      self.shareButton.transform = CGAffineTransformMakeScale(0.7, 0.7) 
     }, completion: nil) 
    } 
} 

回答

0

我想你可以尝试添加两个目标到你的视图。针对UIControlEventTouchDown收缩动画,另一个针对UIControlEventTouchUpInside或其他事件取决于应该发生的情况? Ref iOS docs

+0

不只是与按钮图像UIControlEventTouchDown? – mlevi 2014-10-06 05:25:48

1
var delaysTouchesEnded: Bool // default is YES. 

原因touchesEnded交付给后才这个手势未能识别目标视图事件。这确保了如果手势被识别,则作为手势一部分的触摸可以被取消。

因此,它将在下次调用动作,因为只有在执行了点击动作后才会执行操作。 触摸结束后,将执行操作方法。

但是,您可以使用touchesBegin和touchesEnded方法。 如果您正在使用轻击手势,则无法使用,因为它会在您的触摸被释放时调用操作方法。您还可以使用长按手势缩小视图。

覆盖FUNC

touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
     UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, 
      animations: { 
       self.vwBlue.transform = CGAffineTransformMakeScale(0.5, 0.5) 
      }, completion: nil) 
    } 

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 
     UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, 
      animations: { 
       self.vwBlue.transform = CGAffineTransformMakeScale(1, 1) 
      }, completion: nil) 
    } 
+0

我如何确保touchesBegan仅适用于单个视图? (从来没有真正使用过touhces之前,有点困惑)。谢谢。 – mlevi 2014-10-06 19:00:49

+0

什么触摸开始将做的是,当用户触摸屏幕时,它将执行操作,因此它只会在您已经编写代码的视图上执行操作。它会在屏幕上的任何位置检测触摸。 此外,您还可以使用触摸点检查触摸点在哪个区域进行检查并检查其是否正确,然后仅执行所需的操作。 如需进一步了解,请访问: https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIResponder_Class/index.html#//apple_ref/occ/instm/UIResponder/touchesBegan:withEvent: – 2014-10-07 05:10:32

+0

所以我认为这不能用轻拍手势完成? – mlevi 2014-10-07 15:58:49