在动画 UIView 上点击手势不起作用 [英] Tap Gesture on animating UIView not working

查看:41
本文介绍了在动画 UIView 上点击手势不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正在动画翻译的 UILabel 上有一个点击手势.每当您在动画过程中点击标签时,点击手势都没有响应.

I have a tap gesture on a UILabel who's translation is being animated. Whenever you tap on the label during the animation there's no response from the tap gesture.

这是我的代码:

    label.addGestureRecognizer(tapGesture)

    label.userInteractionEnabled = true
    label.transform = CGAffineTransformMakeTranslation(0, 0)

    UIView.animateWithDuration(12, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
        label.transform = CGAffineTransformMakeTranslation(0, 900)
        }, completion: nil)

手势代码:

func setUpRecognizers() {
    tapGesture = UITapGestureRecognizer(target: self, action: "onTap:")
}
func onTap(sender : AnyObject) {
    print("Tapped")
}

有什么想法吗?谢谢:)

Any ideas? Thanks :)

现在这很容易,你只需覆盖 hitTest.

These days this is dead easy, you just override hitTest.

推荐答案

由于 1 个重要原因,在使用 Tapgesture 后您将无法完成您的任务.Tapgesture 与标签的框架相关联.开始动画时,标签最终帧会立即更改,而您只是在观看假电影(动画).如果您能够触摸屏幕上的 (0,900),它会在动画发生时正常触发.不过,有一种方法可以做到这一点.最好是使用touchesBegan.这是我刚刚写的一个扩展来测试我的理论,但可以根据您的需要进行调整.例如,您可以使用您的实际子类并访问标签属性,而无需循环.

You will not be able to accomplish what you are after using a tapgesture for 1 huge reason. The tapgesture is associated with the frame of the label. The labels final frame is changed instantly when kicking off the animation and you are just watching a fake movie(animation). If you were able to touch (0,900) on the screen it would fire as normal while the animation is occuring. There is a way to do this a little bit different though. The best would be to uses touchesBegan. Here is an extension I just wrote to test my theory but could be adapted to fit your needs.For example you could use your actual subclass and access the label properties without the need for loops.

extension UIViewController{

public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else{return}
    let touchLocation = touch.locationInView(self.view)

    for subs in self.view.subviews{
        guard let ourLabel = subs as? UILabel else{return}
        print(ourLabel.layer.presentationLayer())

        if ourLabel.layer.presentationLayer()!.hitTest(touchLocation) != nil{
            print("Touching")
            UIView.animateWithDuration(0.4, animations: {
                self.view.backgroundColor = UIColor.redColor()
                }, completion: {
                    finished in
                    UIView.animateWithDuration(0.4, animations: {
                        self.view.backgroundColor = UIColor.whiteColor()
                        }, completion: {
                            finished in
                    })
                })
            }
        }

    }
}

你可以看到它正在测试 CALayer.presentationLayer()..这就是我所说的电影的坐标.老实说,我还没有完全了解表示层及其工作原理.

You can see that it is testing the coordinates of the CALayer.presentationLayer()..That's what I was calling the movies. To be honest, I have still not wrapped my head completely around the presentation layer and how it works.

这篇关于在动画 UIView 上点击手势不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆