Ios UIView设置动画/旋转时,UIButton触摸位置不准确

Ios UIView设置动画/旋转时,UIButton触摸位置不准确,ios,swift,uibutton,rotation,uiviewanimation,Ios,Swift,Uibutton,Rotation,Uiviewanimation,大家好,你们这些了不起的家伙!我一直有一个奇怪的问题,如果UIView是动画的,在我的例子中是旋转的,在动画期间,子视图的触摸位置是不准确的 例如,如果我一直单击按钮1,有时会按下按钮2或什么也不按下。我在STO和其他博客上搜索过,尝试过他们的方法,但似乎没有任何效果。我的目标是在视图旋转时使按钮可单击 我认为这是一个UIButton错误或问题,但即使使用ImageView、触摸代理或添加手势,我也会得到同样的结果。我觉得有些简单的东西,比如presentation.layer或者需要添加到UI

大家好,你们这些了不起的家伙!我一直有一个奇怪的问题,如果UIView是动画的,在我的例子中是旋转的,在动画期间,子视图的触摸位置是不准确的

例如,如果我一直单击按钮1,有时会按下按钮2或什么也不按下。我在STO和其他博客上搜索过,尝试过他们的方法,但似乎没有任何效果。我的目标是在视图旋转时使按钮可单击

我认为这是一个UIButton错误或问题,但即使使用ImageView、触摸代理或添加手势,我也会得到同样的结果。我觉得有些简单的东西,比如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")
     }
}
func updateRotateView()
{
UIView.animateWithDuration(2,
延迟:0.0,
选项:[UIViewAnimationOptions.CurveLinear,UIViewAnimationOptions.AllowUserInteraction],
动画:{
self.rotationView.transform=CGAffineTransformRotate(self.rotationView.transform,3.1415926)
//测试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)
}
完成:{在}中完成)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
//测试B
让我们先触摸
让firstHitPoint=firstTouch.locationInView(自旋转视图)
if(self.button1.frame.contains(firstHitPoint)){
打印(“在按钮1上检测到第一次点击”)
}
if(self.button1.layer.frame.contains(firstHitPoint)){
打印(“在按钮1.层上检测到第一次点击”)
}
if((self.button1.layer.presentationLayer()?.hitTest(firstHitPoint!))!=nil){
打印(“检测到的按钮1.层命中测试”)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
//测试C
让lastTouch=touch.first!
让lastHitPoint=lastTouch.locationInView(自旋转视图)
if(self.button1.frame.contains(lasthpoint)){
打印(“检测到上次点击按钮1”)
}
if(self.button1.layer.frame.contains(lasthpoint)){
打印(“在按钮1.层上检测到最后一次点击”)
}
if((self.button1.layer.presentationLayer()?.hitTest(lasthpoint!))!=nil){
打印(“检测到的按钮1.层命中测试”)
}
}
扩展按钮{
//测试D
公共覆盖功能hitTest(点:CGPoint,带事件:UIEvent?->UIView?
{
打印(“称为hitTest”)
}
公共覆盖功能点内部(点:CGPoint,带事件:UIEvent?->Bool
{
打印(“点内调用”)
}
}
测试A->rotationView.presentationLayer值更改,但按钮没有更改,并且将其设置为更新的帧,与button1帧或层不起作用。帧大小值不会更改

测试B/C->UI按钮不会对触摸代理做出响应,但即使使用ImageView也无法准确点击。我也尝试过同样的手势


Test D->Hitpoint确实会被调用,但很可能也是在错误的按钮上调用的。“Point inside”只会被调用几次,如果我能够按正确的按钮,但还是非常不准确。

因为您正在为视图设置动画。在设置动画时,您应该知道有一个表示层和一个模型层

Once
self.rotationView.transform=CGAffineTransformRotate(self.rotationView.transform,3.1415926)

在动画块中调用,rotationView的变换已经设置好,您看到的动画只是表示层(它是id类型,但应该是CALayer)。

感谢您的快速响应。我尝试添加1)self.rotationView.layer.transform=catTransferm3dMakeRotation(3.1415926,0,0,1);为UIView动画块和2)尝试使用CABasic动画作为旋转关键点,将其添加到rotationView.layer.addAnimation(rotateAnimation,forKey:“rotateKey”);两者仍然有错误的接触位置。你介意发布一些我可能不理解或丢失的代码吗?