6

I have one image. I want to spin it like a coin spin on surface. I tried rotation transform but it does not spin like that. How to achieve such an animation?

enter image description here

code:

- (void)viewDidLoad {
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
lbl_facebook.font=[UIFont fontWithName:GZFont size:12.0f];
txtPassword.font=[UIFont fontWithName:GZFont size:15.0f];
txtUsername.font=[UIFont fontWithName:GZFont size:15.0f];

CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = @"flip";
transition.subtype = @"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;
[_Image.layer addAnimation:transition forKey:@"transition"];
}

nd:

#import "LoginViewController.h"

#import "RegistrationViewController.h"
#import "ForgetPasswordViewController.h"
#import "ForgetPasswordController.h"
#import "SearchServiceProviderViewController.h"
#import <QuartzCore/QuartzCore.h>
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36

2 Answers2

19

This will make a nice, coin-like flip:

CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = @"flip";
transition.subtype = @"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;

And add the transition animation to the layer of your view:

[_yourView.layer addAnimation:transition forKey:@"transition"];

See it in action:

enter image description here

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
3

One good trick to spin like this is that take different images of coin with different angle like spinning image. And then add all those images to array and start animation of images with that array..it will give you much better effect...Simple process like video framing.

Like:

NSArray *animationArray = [NSArray arrayWithObjects:
                          [UIImage imageNamed:@"images.jpg"],
                          [UIImage imageNamed:@"images1.jpg"],
                          [UIImage imageNamed:@"images5.jpg"],
                          [UIImage imageNamed:@"index3.jpg"],
                          nil];
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor      = [UIColor purpleColor];
animationView.animationImages      = animationArray;
animationView.animationDuration    = 1.5;
animationView.animationRepeatCount = 0;
[animationView startAnimating]; 
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
  • 1
    @Elhoej Check my edited answer...and if it helps you upvote it :) – vivek bhoraniya Oct 08 '17 at 08:10
  • With this method, how do I set the animation to be spinning? Can you show me full code example? Sorry, can't get it to work :( – Elhoej Oct 08 '17 at 11:16
  • you need multiple images which simulate coin is spinning...and then you need to start animation by passing all those images sequence to imageview...@Elhoej – vivek bhoraniya Oct 09 '17 at 05:05