本文介绍了点击手势与UIGestureRecognizerState不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用了点击手势的视图。我想有视图缩水,当一个人的手指在视图,且具有视图回到正常时,手指抬起。我试图用 UIGestureRecognizerState 来实现这一点,但它不工作。视图缩小后才能删除我的手指不回去。这里是我的code:

  @IBAction FUNC shareButton(发件人:AnyObject){    如果sender.state == {UIGestureRecognizerState.Changed
        UIView.animateWithDuration(0.1,延迟:0.0,usingSpringWithDamping:0.4,initialSpringVelocity:0.4,选项:无,动画:{
            self.shareButton.transform = CGAffineTransformMakeScale(0.9,0.9)
        }完成:无)
    }否则如果sender.state == {UIGestureRecognizerState.Ended
        UIView.animateWithDuration(0.1,延迟:0.0,usingSpringWithDamping:0.4,initialSpringVelocity:0.4,选项:无,动画:{
            self.shareButton.transform = CGAffineTransformMakeScale(0.7 0.7)
        }完成:无)
    }
}


解决方案

  VAR delaysTouchesEnded:BOOL //默认是YES。

原因touchesEnded事件被传递到只有此手势未能识别目标视图。这确保了触摸即手势的一部分可以如果该手势被识别被取消

所以它会调用后,才将进行自来水动作下一次的操作将被执行的操作。
由于操作方法将执行时,触摸已经结束。

但是你可以使用touchesBegin和touchesEnded方法。
如果你正在使用,这将无法正常工作,当你触摸将被释放它的操作方法将被调用点击手势。您也可以使用长preSS手势缩小视图。

覆盖FUNC

 的touchesBegan(触摸:的NSSet,withEvent事件:的UIEvent){
        UIView.animateWithDuration(1,延迟:0.0,usingSpringWithDamping:0.4,initialSpringVelocity:0.4,选项:无,
            动画:{
                self.vwBlue.transform = CGAffineTransformMakeScale(0.5,0.5)
            }完成:无)
    }    覆盖FUNC touchesEnded(触摸:的NSSet,withEvent事件:的UIEvent){
        UIView.animateWithDuration(1,延迟:0.0,usingSpringWithDamping:0.4,initialSpringVelocity:0.4,选项:无,
            动画:{
                self.vwBlue.transform = CGAffineTransformMakeScale(1,1)
            }完成:无)
    }

I have a view with a tap gesture applied to it. I want to have the view "shrink" when someone's finger is on the view and have the view go back to normal when the finger is lifted. I'm trying to achieve this with a UIGestureRecognizerState but it's not working. The view shrinks only after I remove my finger and does not go back. Here's my code:

@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)
    }
}
解决方案
var delaysTouchesEnded: Bool // default is YES.

causes touchesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch that is part of the gesture can be cancelled if the gesture is recognised

So it will call the action next time as operation will be performed only after tap action will be performed.As action method will perform when touch has ended.

But you can use touchesBegin and touchesEnded method.If you are using tap gesture for that it won’t work as it action methods will be called when your touch will be released. You can also use long press gesture for shrinking view.

override 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)
    }

这篇关于点击手势与UIGestureRecognizerState不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:50