一. CABasicAnimation (基礎動畫)
移位:
CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath指定動畫類別,position表示移位 animation.keyPath = @"position"; //移動到x=200,y=200的位置 animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; animation.duration = 2; //動畫執行完畢后不刪除動畫 animation.removedOnCompletion = NO; //保持最新的狀態 animation.fillMode = @"forwards"; //添加動畫 [self.layer addAnimation:animation forKey:nil];
縮放:
CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath指定動畫類別,bounds表示縮放 animation.keyPath = @"bounds"; //縮放到width=50,height=50 animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)]; animation.duration = 2; //動畫完成不刪除動畫 animation.removedOnCompletion = NO; //保持最新的狀態 animation.fillMode = @"forwards"; [self.layer addAnimation:animation forKey:nil];
旋轉:
CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath指定動畫類別,transform表示旋轉 animation.keyPath = @"transform"; //沿x,y軸順時針旋轉45度 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)]; animation.duration = 2; animation.removedOnCompletion = NO; animation.fillMode = @"forwards"; [self.layer addAnimation:animation forKey:nil];
二. CAKeyframeAnimation (關鍵幀動畫)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position"; anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; anim.duration = 2.0; //設置圓形軌跡,并繞圓形軌跡移動 CGMutablePathRef path = CGPathCreateMutable(); CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200)); anim.path = path; CGPathRelease(path); // 設置動畫的執行節奏 // kCAMediaTimingFunctionEaseInEaSEOut : 一開始比較慢, 中間會加速, 臨近結束的時候, 會變慢 anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; anim.delegate = self; [anim setValue:@"aaa" forKey:@"TAG"]; [self.layer addAnimation:anim forKey:nil];
設置代理的回調方法,讓動畫結束后彈出提示
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSString *strTag = [anim valueForKey:@"TAG"]; if ([strTag isEqualToString:@"aaa"]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Animation Done" message:@"動作完成" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil]; [alert show]; }}
三. CATransition(轉場動畫)
(1)視圖跳轉
_newView = [[UIView alloc] init]; _newView.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:_newView]; UIView *view1 = [[UIView alloc] init]; view1.frame = CGRectMake(0, 0, 100, 100); view1.backgroundColor = [UIColor yellowColor]; [_newView addSubview:view1]; UIView *view2 = [[UIView alloc] init]; view2.frame = CGRectMake(0, 0, 100, 100); view2.backgroundColor = [UIColor greenColor]; [_newView addSubview:view2];
添加轉場按鈕事件處理:
- (IBAction)exchangeView { // 轉場動畫 CATransition *transition = [CATransition animation]; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = @"pageCurl"; transition.subtype = kCATransitionFromRight; transition.duration = 1; [_newView exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; [_newView.layer addAnimation:transition forKey:@"myAnimation"];}
(2)控制器跳轉
- (IBAction)pushView { CATransition *transition = [CATransition animation]; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //立體動畫效果 transition.type = @"cube"; [self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"]; TestViewController *testVC = [[TestViewController alloc] init]; [self.navigationController showViewController:testVC sender:nil];}
四. CAAnimationGroup (組合動畫)
//添加圖片 UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"curry.jpg"]]; imgView.frame = CGRectMake(100, 100, imgView.frame.size.width, imgView.frame.size.height); [self.view addSubview:imgView]; //貝塞爾曲線路徑 UIBezierPath *movePath = [UIBezierPath bezierPath]; [movePath moveToPoint:CGPointMake(10.0, 10.0)]; [movePath addQuadCurveToPoint:CGPointMake(100, 300) controlPoint:CGPointMake(300, 100)]; //以下必須導入QuartzCore包 //關鍵幀動畫(位置) CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; posAnim.path = movePath.CGPath; posAnim.removedOnCompletion = YES; //縮放動畫 CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; scaleAnim.removedOnCompletion = YES; //透明動畫 CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"]; opacityAnim.fromValue = [NSNumber numberWithFloat:1.0]; opacityAnim.toValue = [NSNumber numberWithFloat:0.1]; opacityAnim.removedOnCompletion = YES; //動畫組 CAAnimationGroup *animGroup = [CAAnimationGroup animation]; animGroup.animations = [NSArray arrayWithObjects:posAnim, scaleAnim, opacityAnim, nil]; animGroup.duration = 1; [imgView.layer addAnimation:animGroup forKey:nil];
新聞熱點
疑難解答