旋转手势产生不希望的旋转

时间:2016-08-23 02:49:43

标签: swift uiimageview rotation uigesturerecognizer

我需要仅使用一个手指动作就用户的动作旋转UIImageview。为此,我使用旋转手势,然后使用以下代码覆盖touchesMoved:

  override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touch = touches.first
    if touch!.view === rotateImageView {
        let position = touch!.locationInView(self.view)

        let target = rotateImageView.center
        let angle = atan2(target.y-position.y, target.x-position.x)
        rotateImageView.transform = CGAffineTransformMakeRotation(angle)
    }
  }

只要用户停止触摸屏幕并在屏幕的另一部分上进行动作,此功能就会起作用。在该场景中,当用户开始移动手指时,UIImageview在不期望的位置上旋转。我需要保持UIImageview的原始旋转位置以避免这种情况,但我不知道该怎么做。

enter image description here

这个gif显示了我所拥有的项目原型的当前行为。正如你可以看到当我开始在右侧旋转时,火焰图标突然出现在触点附近,这就是我想要阻止的。

2016年8月23日更新

我使用Matt和Dasem建议使用CGAffineTransformRotate,但我仍然看到了跳跃。我拿了Dasem代码并适应我的项目,如下所示(让我知道适应是否错误):

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    origin = (touches.first?.locationInView(self.view))!

    //save the current transform
    tempTransform = rotateImageView.transform

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touchPoint = touches.first?.locationInView(self.view)

    xOffSet = CGVector(dx:(origin.x)-rotateImageView.center.x, dy:(origin.x) - rotateImageView.center.y)
    yOffSet = CGVector(dx:touchPoint!.x - rotateImageView.center.x, dy:touchPoint!.y - rotateImageView.center.y)

    let angle = atan2(yOffSet.dy,yOffSet.dx) - atan2(xOffSet.dy,xOffSet.dx)
    rotateImageView.transform = CGAffineTransformRotate(tempTransform, angle)

}

当用户触摸触摸屏的不同区域时,此更改会产生类似的效果。在这个gif中我改变了图像并删除了图标以使效果更加明显。

enter image description here

更新解决方案

使用Jasem更新重新应用代码并且它工作正常,以下代码在不使用子类化的情况下解决了问题

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    origin = (touches.first?.locationInView(self.view))!
    xOffSet = CGVector(dx:(origin.x)-rotateImageView.center.x, dy:(origin.y) - rotateImageView.center.y)
    startingAngle = atan2(xOffSet.dy,xOffSet.dx)

    //save the current transform
    tempTransform = rotateImageView.transform
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touchPoint = touches.first?.locationInView(self.view)
    yOffSet = CGVector(dx:touchPoint!.x - rotateImageView.center.x, dy:touchPoint!.y - rotateImageView.center.y)
    let angle = atan2(yOffSet.dy,yOffSet.dx)

    let deltaAngle = angle - startingAngle!
    rotateImageView.transform = CGAffineTransformRotate(tempTransform, deltaAngle)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    startingAngle = nil
}

//reset in case drag is cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    rotateImageView.transform = tempTransform
    startingAngle = nil
}

2 个答案:

答案 0 :(得分:2)

您可能希望将self.superview更改为self.view(我的是子类)

var xOffSet:CGVector = CGVectorMake(0, 0)
var yOffSet:CGVector = CGVectorMake(0, 0)
var origin:CGPoint = CGPointZero
var tempTransform=CGAffineTransform()
var startingAngle:CGFloat?


   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    origin = (touches.first?.locationInView(self.superview))!
    xOffSet = CGVector(dx:(origin.x)-self.center.x, dy:(origin.y) - self.center.y)
    startingAngle = atan2(xOffSet.dy,xOffSet.dx)

    //save the current transform
    tempTransform = self.transform
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touchPoint = touches.first?.locationInView(self.superview)
    yOffSet = CGVector(dx:touchPoint!.x - self.center.x, dy:touchPoint!.y - self.center.y)
    let angle = atan2(yOffSet.dy,yOffSet.dx)

    let deltaAngle = angle - startingAngle!
    self.transform = CGAffineTransformRotate(tempTransform, deltaAngle) 
}


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {  
  startingAngle = nil
}

//reset in case drag is cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        self.transform = tempTransform
        startingAngle = nil
    }

答案 1 :(得分:1)

问题是你在说

angle

因此,无论图像视图已经(从前一次旋转)变换,被抛弃并完全被<{em>替换~CGBitmapInfo.AlphaInfoMask.rawValue开始的新变换。那是&#34;跳跃&#34;你看到了。

您要做的是将旋转变换应用于图像视图的现有变换。你可以通过调用CGAffineTransformRotate而不是CGAffineTransformMakeRotation来做到这一点。