1

I need to make an icon rotate depending on the current rotation of another UIImageview. For that I have one UIImageView which movement is controlled by a rotation user gesture. I also have an UIView which contains icons that I need to rotate properly. This UIView have always the exact same position and rotation as the UIImageview.

Graphically this is what I need to do:

enter image description here

I manage the motion with this code:

I managed the gesture with the following code:

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 touch = touches.first

    if touch!.view === rotateImageView  || touch!.view === viewIcons {
        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)
        viewIcons.transform = CGAffineTransformRotate(tempTransform, deltaAngle)

        rotateIconsWithImageView (ivClock , angle: 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
}

This code prevents another problem which happens when the user starts the gesture on another section of the screen. Detailed information can be seen on this another stackoverflow thread: Rotation gesture produces undesired rotation

For the rotation if the icon I use this function which I invoke as the last instruction on touchesMoved:

func rotateIconsWithImageView (imageview:UIImageView , angle: CGFloat ) {
    imageview.transform = CGAffineTransformRotate(tempTransform, -angle)
}

This works fine normally (as you see on the gif) if I start the gesture without changing the touch area. But If the user changes the touch area the rotation is wrong and since I'm not used with transform I'm not sure what exactly to do.

This gif illustrate the issue:

enter image description here

Thanks in advance

Community
  • 1
  • 1
Programmer Hugo
  • 135
  • 2
  • 7
  • But this is exactly the same issue as the other question! I told you before, your whole `tempTransform` thing is completely unnecessary. It's also wrong, and now that fact is coming back to bite you. – matt Aug 23 '16 at 19:23
  • can you provide me the full code fro this? – Wide Angle Technology Sep 29 '17 at 13:27

1 Answers1

0

Get rid of your tempTransform. It's just confusing you. The transform on the outer wheel is not the same as the transform on the icon, so it is nutty to mix them up like this:

func rotateIconsWithImageView (imageview:UIImageView , angle: CGFloat ) {
    imageview.transform = CGAffineTransformRotate(tempTransform, -angle)
}

Instead, just keep rotating the transform of the icon:

func rotateIconsWithImageView (imageview:UIImageView , angle: CGFloat ) {
    imageview.transform = CGAffineTransformRotate(imageview.transform, -angle)
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This is also what I was telling you on the other question, and it was right, even though you didn't listen. – matt Aug 23 '16 at 19:26
  • Thanks matt but using imageview.transform makes the imageview with the icon to spin constantly with the movement. – Programmer Hugo Aug 23 '16 at 20:02
  • Because you're supplying the wrong value for `angle`. — I'm not going to write the whole code for you. Besides, this sort of "ferris wheel" rotation has been heavily discussed and explained on Stack Overflow already. The problem is you're not thinking about what a rotation transform _is_ and what you are rotating in relation to. – matt Aug 23 '16 at 20:07
  • For example look at the discussion here: http://stackoverflow.com/a/14131927/341994 – matt Aug 23 '16 at 20:10