您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在动画UIView上点击手势不起作用

在动画UIView上点击手势不起作用

使用Tapgesture后,您将无法完成自己的工作,原因有1个。手势与标签的框架相关联。开始动画时,标签的最后一帧立即更改,而您只是在观看假电影(动画)。如果您能够触摸屏幕上的(0,900),它将在发生动画时照常触发。不过,有一种方法可以使此方法有所不同。最好的办法是使用touchesBegan。这是我刚刚编写的用于检验理论的扩展,但可以进行扩展以适合您的需求,例如,您可以使用实际的子类并访问标签属性而无需循环。

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()的坐标。这就是我所说的电影。老实说,我还没有完全围绕表示层及其工作原理进行研究。

其他 2022/1/1 18:16:09 有391人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶