Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate in z-axis like core-animation slot machine

I was to able to move or animate my UIView by this code down here:

- (void) makeAnim1{

    //downward animation 
    [UIView animateWithDuration:1.5
                          delay:0.15
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         carousel.frame = CGRectOffset(carousel.frame, 0, 650);
                     }
                     completion:^(BOOL finished){ //task after an animation ends
                         [self performSelector:@selector(makeAnim1_1) withObject:nil afterDelay:2.0];
                         NSLog(@"Done!");
                     }];    
}

- (void) makeAnim1_1{

    //upward animation
    [UIView animateWithDuration:1.5
                          delay:0.1
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         carousel.frame = CGRectOffset(carousel.frame, 0, -650);
                     } 
                     completion:^(BOOL finished){
                        NSLog(@"Done!");
                     }];    


} 

But it only move the UIView up and down. How can I make it spin like a Slot machine but only contains one image or view. Like rotate in z axis. But make it look like it contains more than one image.

Thanks for the help.

like image 370
Bazinga Avatar asked Nov 13 '22 02:11

Bazinga


1 Answers

Instead of changing the frame inside the animation block you change the transform. The transform can be used to scale, rotate and translate (move) the view. You can only rotate around the z-axis but that is what you asked for. The transform property on the view takes a CGAffineTransform, like this:

// rotate pi/2 degrees clockwise
carousel.transform = CGAffineTransformMakeRotation(M_PI_2);

If you need to do more advanced transforms like rotating around another axis then you would need to use a little bit of Core Animation and to set the transform property of the views layer (which takes a CATransform3D instead of a CGAffineTransform).

As with all Core Animation code you need to import QuartzCore.framework and include QuartzCore/QuartzCore.h in your code.

The above animations you are doing is UIView animations which are only meant to animate views but the animation you are asking for requires more advanced animations of the views layer. I suggest that you look at the documentation for CABasicAnimation and also take a look at the Core Animation Programming Guide for iOS to learn more.

You can animate the x rotation of a views layer like this:

CABasicAnimation *slotAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[slotAnimation setToValue:[NSNumber numberWithFloat:M_PI_2]];
// animation customizations like duration and timing 
// that you can read about in the documentation 
[[carousel layer] addAnimation:slotAnimation forKey:@"mySlotAnimation"];

The above code will indeed rotate the view around the x axis but will look very silly without perspective (search SO for "perspective Core Animation", it has been asked about before). There is probably a lot of tweaking to get the correct look but this should be enough to get you started.

like image 110
David Rönnqvist Avatar answered Nov 16 '22 02:11

David Rönnqvist