首页 > 解决方案 > UIView 更改其大小后 UIGestureRecognizer 延迟

问题描述

我里面有一个视图和一个子视图。在子视图中,有一些手势识别器可以工作。都是在创建子视图时正常添加的,例如:

let assetViewTapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleAssetViewTap(_:)))
    assetViewTapGesture.delegate = self
    assetView.addGestureRecognizer(assetViewTapGesture)

在超级视图中,我有一个 UIPinchGestureRecognizer,它调用子视图中的一个函数,该函数又更改子视图中帧的大小:

    public func scale(newWidth:CGFloat){
    let factor:CGFloat = newWidth / self.internalAssetView.frame.width
    self.internalAssetView.frame = CGRect(x: self.internalAssetView.frame.origin.x, y: self.internalAssetView.frame.origin.y, width: newWidth, height: self.internalAssetView.frame.height)
    self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: newWidth + additionParameter, height: self.frame.height)
}

当我捏紧时,一切都表现良好,并且视图会以应有的方式改变它们的大小。

问题: 在捏结束后,我点击以触发子视图内的 TapGestureRecognizer。它正在被发射,但仅在几秒钟后。实际上 - 在点击屏幕和实际触发识别器之间,我捏得越多,等待的时间就越长。为什么改变视图的帧会影响手势识别器触发的延迟?

标签: swiftuigesturerecognizer

解决方案


我不知道这是什么原因,但是我可以提供一个可以缓解您的问题的解决方案。

无需添加独立的点击手势,事实上每个 UIVIEW 都有一个 touches 方法。所以你可以删除点击手势并尝试输入这个函数

覆盖 func touchesBegan()

我省略了参数,当你开始输入 touches 时......它会自动完成。将要执行的代码放入其中。同样,您也可以使用触摸开始、触摸结束的方法。试试 UIVIEW Touch 方法。希望他们不会滞后,您的问题将得到解决。


推荐阅读