CAAnimation的一个动画效果
在做cocos2d项目的时候,ios自带的菊花控件太难看,而我也没找到能改变菊花控件图片的方法,故自己用cocos2d写了一个控件。
我将这个控件的add&run和stop&remove封起来,在发送request的时候调用run,得到request返回时stop。
由于游戏中场景经常变动,我不确定当前调用时的场景是什么,所以我用下面方法:
[[CCDirector ShareDirector].runningScene addChild:rotateview z:2000];
[rotateview removeFromParentAndCleanup:YES];
但是当我remove的时候会报错,我简单查看了一下,只要我调用rotateview.parent就会报错,比如
if (rotateview.parent)//在这行都会报错
无解,如果有高手知道是为什么,或者如何解决,请PM小弟,或者QQ,邮件(44807476,wangyutao0424@163.com)
所以,为了逃避这个parent,我不得不自己写一个小控件,把他放到rootview上去
我在网上找到两种方法,
第一种如果设置M_PI*2系统会判断原地不动,没有动画,所以要做一个变量,让弧度递增
-(void) startAnimation
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.01];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(endAnimation)];
imageView.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
[UIView commitAnimations];
}
-(void)endAnimation
{
angle += 10;
[self startAnimation];
}
或者:
- (void)startAnimation
{
CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));
[UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.transform = endAngle;
} completion:^(BOOL finished) {
angle += 10; [self startAnimation];
}];
}
另外可以用CAAnimation.h的方法,记得添加头<QuartzCore/CAAnimation.h>
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat;
[_loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
posted on 2012-11-02 12:21 wangyutao0424 阅读(467) 评论(0) 编辑 收藏 举报