开发者

How to Move an image in circular in iPhone sdk

开发者 https://www.devze.com 2023-01-26 18:04 出处:网络
I have one image in circular format a开发者_Go百科nd i want to rotate in circular way when i touch it so can any one tell me what i have to do and give me some source code

I have one image in circular format a开发者_Go百科nd i want to rotate in circular way when i touch it so can any one tell me what i have to do and give me some source code

thank to all,..


try something like this.

- (void)spinButton
{
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    CGRect frame = [button frame];
    button.layer.anchorPoint = CGPointMake(0.5, 0.5);
    button.layer.position = CGPointMake(frame.origin.x + 0.5 * frame.size.width, frame.origin.y + 0.5 * frame.size.height);
    [CATransaction commit];

    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
    [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];

    CABasicAnimation *animation;
    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
    animation.delegate = self;
    [button.layer addAnimation:animation forKey:@"rotationAnimation"];

    [CATransaction commit];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished
{
    if (finished)
    {
        [self spinButton];
    }
}

End the animation by calling something like

- (void)setButtonImage:(UIImage *)image
{
    [button.layer removeAllAnimations];
    if (!image)
    {
        [button setImage:[UIImage imageNamed:@"playbutton.png"] forState:0];
    }
    else
    {
        [button setImage:image forState:0];

        if ([button.currentImage isEqual:[UIImage imageNamed:@"loadingbutton.png"]])
        {
            [self spinButton];
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消