大家好,你们好!我一直遇到一个奇怪的问题,如果 UIView 是动画的,在我的例子中是旋转的,在动画过程中 subview 触摸位置不准确。
例如,如果我一直点击button1,有时button2会被按下,或者什么也不会被按下。我在 STO 和其他博客上搜索过,尝试过他们的方法,但似乎没有任何效果。我的目标是在 View 旋转时让按钮可单击。
我认为这是一个 UIButton 错误或问题,但即使使用 imageViews、触摸委托(delegate)或添加的手势,我也得到同样的结果。我觉得一些简单的东西,比如presentation.layer或者需要添加到UIView动画函数中的东西,但我有点不知所措。希望您能提供一些见解!谢谢大家。
func updateRotateView ()
{
UIView.animateWithDuration(2,
delay: 0.0,
options: [UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.AllowUserInteraction],
animations: {
self.rotationView.transform = CGAffineTransformRotate(self.rotationView.transform, 3.1415926)
//Test A
//self.button1.frame = self.rotationView.convertRect(self.button1.frame, fromView:self.rotationView)
//self.button1.layer.frame = self.rotationView.layer.convertRect(self.button1.layer.frame, fromLayer: self.rotationView.layer)
}
completion: {finished in })
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
//Test B
let firstTouch = touches.first
let firstHitPoint = firstTouch.locationInView(self.rotationView)
if (self.button1.frame.contains(firstHitPoint)) {
print("Detected firstHit on Button1")
}
if (self.button1.layer.frame.contains(firstHitPoint)) {
print("Detected firstHit on Button1.layer")
}
if ((self.button1.layer.presentationLayer()?.hitTest(firstHitPoint!)) != nil) {
print("Detected button1.layer hit test")
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Test C
let lastTouch = touches.first!
let lastHitPoint = lastTouch.locationInView(self.rotationView)
if (self.button1.frame.contains(lastHitPoint)) {
print("Detected lastHit on Button1")
}
if (self.button1.layer.frame.contains(lastHitPoint)) {
print("Detected lastHit on Button1.layer")
}
if ((self.button1.layer.presentationLayer()?.hitTest(lastHitPoint!)) != nil) {
print("Detected button1.layer hit test")
}
}
extension UIButton {
//Test D
public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
{
print("hitTest called")
}
public override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool
{
print("pointInside called")
}
}
测试 A ->rotationView.presentationLayer 值发生变化,但按钮没有变化,并且将其设置为更新的框架不起作用,因为button1框架或layer.frame大小值没有变化。
测试 B/C -> UIButton 不响应触摸委托(delegate),但即使使用 imageViews 也无法准确命中。也尝试了手势,结果相同。
测试 D -> Hitpoint 确实被调用,但很可能也按错了按钮。如果我能够按下正确的按钮,则内部点只会被调用几次,但又非常不准确。
最佳答案
因为您正在对 View 进行动画处理。你应该知道动画时有一个表示层和一个模型层。
一次 self.rotationView.transform = CGAffineTransformRotate(self.rotationView.transform, 3.1415926)
在动画 block 中调用,rotationView的transform已经设置好了,你看到的动画只是表示层(它是一个id类型,但应该是CALayer)。
关于ios - UIView 动画/旋转时 UIButton 触摸位置不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645171/