如何在iPhone sdk中以圆形移动图像

时间:2010-11-25 13:49:44

标签: iphone

我有一个圆形格式的图像,当我触摸它时我想以圆形方式旋转,所以任何人都可以告诉我我必须做什么并给我一些源代码

感谢所有人,..

1 个答案:

答案 0 :(得分:2)

尝试这样的事情。

- (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];
    }
}

通过调用类似

的内容结束动画
- (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];
        }
    }
}