iOS之动画
這幾天做項目,涉及到美觀和趣味性的問題,所以用到了動畫,但是現在的iOS動畫資料比較少,所以我自己整理了一些自己曾經用過的動畫例子來和大家分享,同時也歡迎有好的想法大家一起交流~~
序列幀動畫
曾經項目里的一段源碼:
UIImageView * activityImageView = [[UIImageView alloc] init]; NSMutableArray *imagesList = [NSMutableArray array]; for (NSInteger i = 1; i < 3; i++) {NSString *fileName = [NSString stringWithFormat:@"eggplant%i.png",i]; UIImage *image = [UIImage imageNamed:fileName]; [imagesList addObject:image]; } [activityImageView setAnimationImages:imagesList]; [activityImageView setAnimationDuration:0.5]; //0為無限循環 [activityImageView setAnimationRepeatCount:0]; [activityImageView startAnimating]; // [activityImageView stopAnimating];UIView 動畫
UIViewAnimation
//創建一個CGAffineTransform transform對象 CGAffineTransform transform; //設置旋轉度數 transform = CGAffineTransformRotate(testView.transform,M_PI/6.0); //動畫開始 [UIView beginAnimations:@"rotate" context:nil ]; //動畫時常 [UIView setAnimationDuration:2]; //自動反轉 // [UIView setAnimationRepeatAutoreverses:YES]; [UIView setAnimationRepeatCount:3]; //添加代理 [UIView setAnimationDelegate:self]; //獲取transform的值 [testView setTransform:transform]; //關閉動畫 [UIView commitAnimations];UIViewAnimationWithBlocks
/* Duration 動畫持續時間 delay 動畫延遲時間 options 動畫的節奏控制 */[UIView animateWithDuration:5 delay:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{ testView.frame = CGRectMake(100, 300, 100, 100); } completion:^(BOOL finished) {}];/* Damping 動畫的彈力指數 Velocity 彈力的初速度 */[UIView animateWithDuration:0.5 delay:1 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:0 animations:^{ testView.frame = CGRectMake(100, 300, 100, 100); } completion:^(BOOL finished) {}];CoreAnimation
CATransition
繼承關系:CATransition -> CAAnimation
CATransition *transition = [CATransition animation]; transition.duration = 0.5; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //動畫類型 transition.type = kCATransitionPush; //動畫方向 transition.subtype = kCATransitionFromTop; [testView.layer addAnimation:transition forKey:nil];CAPropertyAnimation
繼承關系:CABasicAnimation,CAKeyframeAnimation -> CAPropertyAnimation -> CAAnimation
CABasicAnimation
CABasicAnimation * animation = [CABasicAnimation animation]; animation.keyPath = @"position.y";//運動的絕對距離 animation.fromValue = @77; animation.toValue = @455;//運動的相對距離 // animation.byValue = @222; animation.duration = 1; //留在最終狀態 animation.fillMode = @"forwards"; //防止它被自動移除 animation.removedOnCompletion = NO; animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5 :0 :0.9 :0.7]; [testView.layer addAnimation:animation forKey:@"basic"];CAKeyframeAnimation 例一
CAKeyframeAnimation * animation = [CAKeyframeAnimation animation]; animation.keyPath = @"position.x"; animation.values = @[@0,@10,@-10,@10,@0]; //指定關鍵幀動畫發生的時間 animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ]; animation.duration = 0.4; //提前無需設置位置 animation.additive = YES; [testView.layer addAnimation:animation forKey:@"shake"];CAKeyframeAnimation 例二
CGRect boundingRect = CGRectMake(-150, -150,300, 300);CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation]; orbit.keyPath = @"position"; //創建一個圓形的 CGPath 作為我們的關鍵幀動畫的 path。 orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL)); orbit.duration = 2; orbit.additive = YES; orbit.repeatCount = HUGE_VALF; //恒定速度 orbit.calculationMode = kCAAnimationPaced; //確保沿著路徑旋轉 orbit.rotationMode = kCAAnimationRotateAuto; [testView.layer addAnimation:orbit forKey:@"orbit"];CAAnimationGroup 組動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; animation.duration = 3.; animation.fromValue = @(0.1); animation.toValue = @(1.);CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"]; animation2.duration = 3.; animation2.fromValue = @(1); animation2.toValue = @(2.); animation2.beginTime = 3.;CAAnimationGroup *group = [CAAnimationGroup animation]; group.duration = 6.; group.animations = @[animation,animation2]; [testView.layer addAnimation:group forKey:nil];先就總結這么多,歡迎大家來補充。以后遇到更好也會寫出來和大家分享
轉載于:https://www.cnblogs.com/LeoTai/p/5493852.html
總結
- 上一篇: 前后端分离+本地服务实时刷新+缓存管理+
- 下一篇: 实验三 进程调度模拟程序2.0