English 中文(简体)
Need help to make my iPhone animation work?
原标题:

I m brand new in iPhone animation feature. I wrote a simple testing program to spin a red rectangle; however, the animation doesn t act at all. What s wrong with my code? Thanks a lot...

//.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

#define DegreesToRadians(X) (M_PI*X/180.0)

//.m
- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect theRect = CGRectMake(0,0,100,100);
    UIView *theBox = [[UIView alloc] initWithFrame:theRect];
    theBox.backgroundColor = [UIColor redColor];
    [self.view addSubview:theBox];
    CALayer *theLayer = [theBox layer];
    [self spinLayer:theLayer];

}

-(CAAnimation *)animationForSpinning{
    float radians = DegreesToRadians(360);
    CATransform3D theTransform = CATransform3DMakeRotation(radians, 0, 0, 1.0);
    CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    theAnimation.toValue = [NSValue valueWithCATransform3D:theTransform];
    theAnimation.duration = 2;  // 2 seconds
    theAnimation.repeatCount = 10000;
    theAnimation.cumulative = YES;

    return theAnimation;    
}

-(void)spinLayer:(CALayer *)theLayer{
    CAAnimation *spinningAnimation = [self animationForSpinning];
    [theLayer addAnimation:spinningAnimation forKey:@"opacity"];

    // trigger the animation
    theLayer.opacity = 0.99;
}
问题回答

Your animation is not running because Core Animation sees a transform of 360 degrees being the same as your original image, so it does nothing.

The easiest way to handle a 360 degree rotation is described in this answer to this question, where you set up your CABasicAnimation to animate the transform.rotation.z property from 0 to 2 * pi radians.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签