如何使图像在iPhone主屏幕上摇摆?

时间:2011-02-12 19:29:20

标签: iphone objective-c ios core-animation

我的应用程序中有很多图标,我想以类似于尝试从iPhone主屏幕删除应用程序时的方式设置它们的动画。你怎么能这样做?

此外,有没有办法让图标以类似于解锁iPhone时的方式显示在屏幕上?

3 个答案:

答案 0 :(得分:26)

如果你想让你的视图,图像等摆动,就像主屏幕一样,你可以这样做:

    CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-15.0));
    CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(15.0));

    view.transform = leftWobble;  // starting point

    [UIView beginAnimations:@"wobble" context:view];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:5]; // adjustable
    [UIView setAnimationDuration:0.125];
    [UIView setAnimationDelegate:self];
    view.transform = rightWobble; // end here & auto-reverse
    [UIView commitAnimations];

您还需要添加此定义:

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

答案 1 :(得分:10)

使用块(iOS 4+),它看起来像:

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

    CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-2.0));
    CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(2.0));

    cell.transform = leftWobble;  // starting point
    cell.deleteButton.hidden = NO;

    [UIView animateWithDuration:0.125 delay:0 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^{
        cell.transform = rightWobble;
    }completion:^(BOOL finished){
    }];

答案 2 :(得分:1)

如果您的意思是iOS主屏幕中的图标,我认为这不可能。

当然,如果你的意思是你的应用程序中的图标,你可以随心所欲。