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