#define ANIM_ROTATE @"animationRotate"
#define ANIM_FALLING @"animationFalling"
#define ANIM_GROUP @"animationFallingRotate"
- (IBAction)do3dRotate:(id)sender
{
CAAnimation* myAnimationRotate = [self animationRotate];
[transformItem.layer addAnimation:myAnimationRotate forKey:ANIM_ROTATE];
}
-(IBAction)do3dFalling:(id)sender
{
CAAnimation* myAnimationFalling = [self animationFallingDown];
[transformItem.layer addAnimation:myAnimationFalling forKey:ANIM_FALLING];
}
-(IBAction)do3dShrink:(id)sender
{
CAAnimation* myAnimationShrink = [self animationShrink];
[transformItem.layer setAnchorPoint:CGPointMake(1, 1)];
[transformItem.layer addAnimation:myAnimationShrink forKey:ANIM_FALLING];
}
//- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
//{
// //这边为什么是nil? :(
// NSLog(@"anim = %@", [m_pMyImageView.layer valueForKey:ANIM_GROUP]);
//#if 1
// //识别动画
// //?debug发现两者的地址不一样,这个问题很纠结
// if ([anim isEqual:m_pGroupAnimation])//[m_pMyImageView.layer valueForKey:ANIM_GROUP])
// {
// NSLog(@"removeFromSuperview...");
// [m_pMyImageView removeFromSuperview];
// [m_pMyImageView release];
//
// }
//#else
// //这种方法,虽然能解决方法,但是处理多个CAAnimationGroup动画或者CAAnimation动画时,就不能有效处理,方法待定
// //这组动画结束,移除视图
// if ([anim isKindOfClass:[CAAnimationGroup class]])
// {
// //这边为什么是nil? :(
// NSLog(@"anim = %@", [m_pMyImageView.layer valueForKey:ANIM_GROUP]);
//
// [m_pMyImageView removeFromSuperview];
// [m_pMyImageView release];
//
// }
//#endif
//}
- (CAAnimation *)animationShrink
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.toValue = [NSNumber numberWithDouble:2.0];
animation.duration = 3.0;
animation.autoreverses = YES;
animation.repeatCount = FLT_MAX; //"forever"
animation.removedOnCompletion = NO;
//设置动画代理
animation.delegate = self;
return animation;
}
- (CAAnimation *)animationFallingDown
{
//falling down animation:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.duration = 5.0;
animation.autoreverses = NO;
animation.removedOnCompletion = NO;
animation.repeatCount = FLT_MAX; //"forever"
animation.fromValue = [NSNumber numberWithInt: 0];
animation.toValue = [NSNumber numberWithInt: 1024];
//设置动画代理
animation.delegate = self;
return animation;
}
- (CAAnimation *)animationRotate
{
// rotate animation
CATransform3D rotationTransform = CATransform3DMakeRotation(M_PI, 0, 1.0, 0.0);
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
animation.duration = 1.5;
animation.autoreverses = NO;
animation.cumulative = YES;
animation.repeatCount = FLT_MAX; //"forever"
//设置开始时间,能够连续播放多组动画
animation.beginTime = 0.5;
//设置动画代理
animation.delegate = self;
return animation;
}