Viral Narshana
Viral Narshana

Reputation: 1877

Start and stop animation with CABasicAnimation iphone

I want to use animation for image, start on button clicked and stop after 5 seconds.I use this code.

-(IBAction) btnClicked
{   
    CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 6;
    fullRotation.repeatCount = 1e100f;
    fullRotation.delegate = self;
    [imgView.layer addAnimation:fullRotation forKey:@"360"];
    [imgView.layer setSpeed:3.0];
}

with this code animation is started but i don't know how to stop this animation after 5 seconds.

Upvotes: 2

Views: 2181

Answers (1)

David Neiss
David Neiss

Reputation: 8237

Don't even deal with CoreAnimation. I think this can be do this with UIView animations, which are easier to program. Look at UIView documentation and try the block animations (assuming iOS 4.0. if not, use older style).

Look here for the docs on UIView animations. Set the transform property to do the rotation using CGAffineTransformMakeRotation.

Here is a UIView based animated rotation:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *viewToAnimate = [[[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)] autorelease];
    viewToAnimate.backgroundColor = [UIColor blueColor];
    [self.view addSubview:viewToAnimate];
    [UIView animateWithDuration:5 animations:^{
        viewToAnimate.transform=CGAffineTransformMakeRotation(M_PI);
    }];
}

Upvotes: 1

Related Questions