1

I'm trying to rotate an image repeatedly (360) with an interval. I found this answer(Image rotation in iOS), appears to be ok, but it's not working for me. When I load the VC I can see slight movement, but that's it. Any ideas? The code I'm using:

 - (void)viewDidAppear:(BOOL)animated {
    [self.compass setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];


    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(rotation) userInfo:nil repeats:YES];

}
-(void)rotation{
    [self.compass setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];

}

EDIT:

- (void)viewDidAppear:(BOOL)animated {
    [self.compass setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];


    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(rotation) userInfo:nil repeats:YES];

}
-(void)rotation{
    [self.compass setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];
    CGAffineTransform transform = CGAffineTransformRotate(self.compass.transform, 10*(M_PI/360));
    [self.compass setTransform:transform];
}
Community
  • 1
  • 1
FuManchu
  • 145
  • 2
  • 11

3 Answers3

2

Animate the rotation as follow:

[UIView animateWithDuration:0.3 animations:^{
        compas.transform = CGAffineTransformMakeRotation(M_PI/2);
    }];
Constantin Saulenco
  • 2,353
  • 1
  • 22
  • 44
  • well thanks for the answers, but the rotation isnt continuous: the method rotation: -(void)rotation{ [self.compass setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))]; CGAffineTransform transform = CGAffineTransformRotate(self.compass.transform, 180*(M_PI/360)); [self.compass setTransform:transform]; } Like this the compass just moves 180 dgs and stays that way... – FuManchu Feb 15 '17 at 22:20
  • ok, i misunderstand because of `repeats:YES`, i updated my answer – Constantin Saulenco Feb 15 '17 at 22:26
  • No, its the same, Constantin, I'll Edit so you can see the present code – FuManchu Feb 15 '17 at 22:30
0

Your rotation is happening but it's just a very small rotation: 10*pi/360 radians is equal to 5 degrees. Pi radians is equal to 180 degrees so if you want to turn it 360 degrees rotate 2 * M_PI.

Note however that you're currently not animating the movement and when you're just rotating a view without animation it will look exactly the same as when it's transform was still CGAffineTransformIdentity. To animate the transition, take a look at UIView animations: https://www.raywenderlich.com/2454/uiview-tutorial-for-ios-how-to-use-uiview-animation

erikvdplas
  • 551
  • 5
  • 10
0

You'll need to add the previous rotation in as well, otherwise you're rotating to the same angle over and over:

CGAffineTransformMakeRotation(NewAngle + 10*(M_PI/360))
Trev14
  • 3,626
  • 2
  • 31
  • 40