日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

GIF动画,菊花动画,UIView动画,CoreAnimation动画(CALayer动画)的用法

發(fā)布時(shí)間:2025/5/22 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GIF动画,菊花动画,UIView动画,CoreAnimation动画(CALayer动画)的用法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.GIF動(dòng)畫(huà)

1 // 創(chuàng)建一個(gè)顯示圖片的imageView // viewController創(chuàng)建 2 UIImageView *showGifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)]; 3 [self.view addSubview:showGifImageView]; 4 5 6 //創(chuàng)建一個(gè)存儲(chǔ)圖片的數(shù)組 7 NSMutableArray *saveImageViewArray = [NSMutableArray array]; 8 9 for (int i = 1; i < 20; i++) { 10 NSString *imageName = [NSString stringWithFormat:@"%d.tiff",i]; 11 UIImage *image = [UIImage imageNamed:imageName]; 12 [saveImageViewArray addObject:image]; 13 } 14 15 // 設(shè)置gif圖片組 16 showGifImageView.animationImages = saveImageViewArray; 17 // 設(shè)置播放速率 18 showGifImageView.animationDuration = 1.0f; 19 // 設(shè)置播放次數(shù)(設(shè)置動(dòng)態(tài)圖重復(fù)次數(shù)) 20 showGifImageView.animationRepeatCount = -1;// -1無(wú)限為播放 21 // 動(dòng)畫(huà)需要設(shè)置開(kāi)辟 22 [showGifImageView startAnimating]; 23 24 }

2.菊花動(dòng)畫(huà)

1 self.view.backgroundColor =[UIColor grayColor]; // viewController創(chuàng)建 2 // 加載旋轉(zhuǎn)的菊花效果 3 4 //[UIActivityIndicatorView實(shí)現(xiàn)要實(shí)現(xiàn)的風(fēng)火輪效果] 5 6 // 無(wú)需設(shè)置frame 7 UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 8 indicatorView.center = self.view.center; 9 [self.view addSubview:indicatorView]; 10 11 // 將風(fēng)火輪動(dòng)畫(huà)效果開(kāi)啟 12 [indicatorView startAnimating];

3.UIView動(dòng)畫(huà)

?

3.1基礎(chǔ)動(dòng)畫(huà)

1 #pragma mark - 改變View的frame 2 - (IBAction)changeFrame:(id)sender { 3 // UIView動(dòng)畫(huà)有開(kāi)始beginAnimation,有結(jié)束commitAnimation 4 // 第一步: 開(kāi)始Uiview動(dòng)畫(huà) 5 [UIView beginAnimations:@"move" context:nil]; 6 // 第二步: 設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) 7 [UIView setAnimationDuration:3]; 8 // 第三步: 設(shè)置UIView動(dòng)畫(huà)的回調(diào)代理 9 [UIView setAnimationDelegate:self]; 10 // 第四步: 設(shè)置相關(guān)的對(duì)象的frame 11 _testView.frame = CGRectMake(100, 100, 200, 100); 12 // 第五步: 結(jié)束動(dòng)畫(huà)(提交動(dòng)畫(huà)效果) 13 [UIView commitAnimations]; 14 15 16 17 } 18 19 #pragma mark - UIViewAnimationDelegate的代理方法 20 // 開(kāi)始動(dòng)畫(huà)的方法 21 -(void)animationWillStart:(NSString *)animationID context:(void *)context 22 { 23 NSLog(@"ID = %@,context = %@",animationID,context); 24 } 25 26 // 結(jié)束動(dòng)畫(huà)的方法 27 -(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 28 { 29 NSLog(@"ID = %@, context = %@", animationID, context); 30 } 31 32 33 34 #pragma mark - 改變View的color 35 - (IBAction)changecolor:(id)sender { 36 // 第一步: 開(kāi)始Uiview動(dòng)畫(huà) 37 38 [UIView beginAnimations:@"color" context:nil]; 39 // 第二步: 設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) 40 41 [UIView setAnimationDuration:3]; 42 // 第二步: 設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) 43 44 [UIView setAnimationDelegate:self]; 45 46 _testView.backgroundColor = [UIColor redColor]; 47 // 第五步: 結(jié)束動(dòng)畫(huà)(提交動(dòng)畫(huà)效果) 48 49 [UIView commitAnimations]; 50 51 52 } 53 54 #pragma mark - 改變view的alpha 55 - (IBAction)ChangeAlpha:(id)sender { 56 57 [UIView beginAnimations:@"alpha" context:nil]; 58 [UIView setAnimationDuration:5]; 59 [UIView setAnimationDelegate:self]; 60 61 _testView.alpha = 0.4; 62 [UIView commitAnimations]; 63 }

66 #pragma mark - 仿射翻轉(zhuǎn)效果的響應(yīng)方法 67 - (IBAction)rotationAction:(id)sender { 68 // 第一步: 開(kāi)始動(dòng)畫(huà) 69 [UIView beginAnimations:@"transform" context:nil]; 70 // 第二步: 設(shè)置時(shí)長(zhǎng) 71 [UIView setAnimationDuration:1]; 72 // 第三步: 設(shè)置淡入的效果 73 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 74 // 第四步: 設(shè)置代理 75 76 [UIView setAnimationDelegate:self]; 77 78 //第五步: 設(shè)置旋轉(zhuǎn)方向 79 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_testView cache:YES]; 80 // 第六步: 提交動(dòng)畫(huà) 81 [UIView commitAnimations]; 84 } 85 #pragma mark - 仿射旋轉(zhuǎn)效果的響應(yīng)方法 86 - (IBAction)transfromAction:(id)sender { 87 88 [UIView beginAnimations:@"rotation" context:nil]; 89 [UIView setAnimationDuration:2]; 90 [UIView setAnimationDelegate:self]; 91 // 要進(jìn)行翻轉(zhuǎn),所以需要設(shè)置旋轉(zhuǎn)角度 92 CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI); 93 94 // 設(shè)置旋轉(zhuǎn)角度的對(duì)象 95 [_testView setTransform:transform]; 96 97 [UIView commitAnimations]; 98 99 }

3.2UIView的block動(dòng)畫(huà)

?

1 #pragma mark - 簡(jiǎn)單block動(dòng)畫(huà) 2 - (IBAction)easyBlockAnimation:(id)sender { 3 4 // 第一個(gè)參數(shù): 設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) 5 // 第二個(gè)參數(shù): 動(dòng)畫(huà)要顯示的效果 6 7 __weak typeof (self)weakSelf = self; 8 9 // [UIView animateWithDuration:2.0 animations:^{ 10 // 11 // // 改變iamgeView的center位置 12 // weakSelf.playIamgeView.center = self.view.center; 13 // 14 // }]; 15 16 // 第一個(gè)參數(shù): 設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) 17 // 第二個(gè)參數(shù): 動(dòng)畫(huà)要顯示的效果 18 // 第三個(gè)參數(shù): 動(dòng)畫(huà)完成時(shí)進(jìn)行的事情 19 [UIView animateWithDuration:2.0f animations:^{ 20 weakSelf.playIamgeView.center = self.view.center; 21 22 } completion:^(BOOL finished) { 23 NSLog(@"美女"); 24 }]; 25 } 26 27 #pragma mark - 復(fù)雜block動(dòng)畫(huà) 28 - (IBAction)complexBlockAnimation:(id)sender { 29 30 // 參數(shù)1: 時(shí)長(zhǎng) 31 // 參數(shù)2: 動(dòng)畫(huà)的延遲時(shí)間 32 // 參數(shù)3: 動(dòng)畫(huà)的枚舉值 33 // 參數(shù)4: 要實(shí)現(xiàn)的動(dòng)畫(huà)效果 34 // 參數(shù)5: 動(dòng)畫(huà)完成的時(shí)候要干的事情 35 36 __weak typeof (self)weakSelf = self; 37 38 39 [UIView animateWithDuration:5.0f delay:1.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ 40 weakSelf.playIamgeView.frame = CGRectMake(100, 100, 100, 100); 41 } completion:^(BOOL finished) { 42 NSLog(@"美女?"); 43 }]; 44 45 } 46 #pragma mark - 關(guān)鍵幀動(dòng)畫(huà) 47 - (IBAction)keyFramesAnimation:(id)sender { 48 49 // 參數(shù)1: 時(shí)長(zhǎng) 50 // 參數(shù)2: 延遲時(shí)間 51 // 參數(shù)3: 枚舉值動(dòng)畫(huà)效果 52 // 參數(shù)4: 開(kāi)始動(dòng)畫(huà) 53 __weak typeof(self)weakSelf = self; 54 [UIView animateKeyframesWithDuration:5.0f delay:1.0f options:UIViewKeyframeAnimationOptionAllowUserInteraction animations:^{ 55 // 在這里需要添加一個(gè)方法,即創(chuàng)建block的關(guān)鍵幀 56 // 幀動(dòng)畫(huà)的開(kāi)始時(shí)間 57 // 幀動(dòng)畫(huà)的持續(xù)時(shí)間 58 [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{ 59 //在這個(gè)里邊實(shí)現(xiàn)相關(guān)的效果 60 weakSelf.playIamgeView.center = self.view.center; 61 62 }]; 63 } completion:^(BOOL finished) { 64 NSLog(@"美女"); 65 }]; 66 67 }

3.3UIView的UIViewSpring動(dòng)畫(huà)

- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.self.title = @"spring動(dòng)畫(huà)";// 參數(shù)1: 動(dòng)畫(huà)時(shí)長(zhǎng)// 參數(shù)2: 延遲時(shí)間// 參數(shù)3: 類(lèi)似彈簧的效果值:0-1// 參數(shù)4: 初始化spring的一個(gè)速度// 參數(shù)5: spring動(dòng)畫(huà)的枚舉值// 參數(shù)6: 開(kāi)始動(dòng)畫(huà)// 參數(shù)7: 動(dòng)畫(huà)完成__weak typeof(self)weakSelf = self;[UIView animateWithDuration:3.0f delay:0.1f usingSpringWithDamping:1.0 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{weakSelf.springImageView.center = weakSelf.view.center;} completion:^(BOOL finished) {NSLog(@"帥哥");}]; }

4.CoreAnimation動(dòng)畫(huà)(CALayer動(dòng)畫(huà))

Layer的常用屬性

1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 self.view.backgroundColor = [UIColor grayColor]; 5 #pragma mark - Layer的常用屬性 6 //設(shè)置圖片為圓角 7 self.myImageView.layer.cornerRadius = 95; 8 // 注意: 光設(shè)置上邊一句代碼是實(shí)現(xiàn)不了效果(下邊的masksToBounds這個(gè)屬性影響layer層的陰影效果) 9 self.myImageView.layer.masksToBounds = YES; 10 // 設(shè)置layer的陰影顏色 11 self.myImageView.layer.shadowColor = [UIColor blueColor].CGColor; 12 //設(shè)置layer層的透明度 13 self.myImageView.layer.shadowOpacity = 0.5; 14 15 // 設(shè)置陰影的偏移量 16 self.myImageView.layer.shadowOffset = CGSizeMake(-20, 10); 17 // 設(shè)置陰影的模糊度 18 self.myImageView.layer.shadowRadius = 1.0; 19 20 // 需求:拖進(jìn)來(lái)一個(gè)UIView設(shè)置它的陰影 21 22 self.myView.layer.shadowOpacity = 0.5; 23 self.myView.layer.shadowOffset = CGSizeMake(-20, -10); 24 self.myView.layer.shadowRadius = 0.2; 25 26 // 自定義layer 27 [self customLayer]; 28 } 29 30 #pragma mark - 自定義layer 31 - (void)customLayer 32 { 33 //創(chuàng)建一個(gè)layer對(duì)象 34 CALayer *layer = [CALayer layer]; 35 //設(shè)置對(duì)象的位置和大小 36 layer.frame = CGRectMake(0, 280, 100, 100); 37 // 設(shè)置背景顏色 38 layer.backgroundColor = [UIColor redColor].CGColor; 39 40 // 設(shè)置錨點(diǎn) 41 layer.anchorPoint = CGPointMake(0, 0); 42 43 // 設(shè)置大小(位置) 44 layer.position = CGPointMake(100, 100); 45 46 // layer需要添加到layer層 47 [self.view.layer addSublayer:layer]; 48 }

?

1 #pragma mark - CABasicAnimation動(dòng)畫(huà)的響應(yīng)方法 2 - (IBAction)basicAnimation:(id)sender { 3 4 //第一步: 創(chuàng)建動(dòng)畫(huà)的對(duì)象 5 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; 6 //第二步: 告訴layer層需要什么執(zhí)行樣子的動(dòng)畫(huà)[后邊設(shè)置的內(nèi)容為CALayer的相關(guān)屬性] 7 basicAnimation.keyPath = @"position"; 8 //第三步: 告訴告訴layer從哪里來(lái),要到哪里去 9 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 10 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; 11 12 // 注意點(diǎn): 如果要實(shí)現(xiàn)移動(dòng)到位置不回到原來(lái)的位置,需要實(shí)現(xiàn)以下代碼 13 14 basicAnimation.removedOnCompletion = NO; 15 // 設(shè)置保存動(dòng)畫(huà)狀態(tài)的內(nèi)容 16 basicAnimation.fillMode = kCAFillModeForwards; 17 18 //第四步: 設(shè)置動(dòng)畫(huà)持續(xù)時(shí)長(zhǎng) 19 basicAnimation.duration = 6; 20 21 // 第五步: 將要執(zhí)行的動(dòng)畫(huà)添加到CALayer上 22 [self.myImageView.layer addAnimation:basicAnimation forKey:@"basic"]; 23 24 //==========翻轉(zhuǎn)效果============= 25 CABasicAnimation *basic = [CABasicAnimation animation]; 26 basic.keyPath = @"transform"; 27 28 // 設(shè)置翻轉(zhuǎn)到的地方 29 basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, 1)]; 30 // 設(shè)置動(dòng)畫(huà)時(shí)間 31 basic.duration = 2.0f; 32 [self.myImageView.layer addAnimation:basic forKey:@"aaa"]; 33 34 // 根據(jù)key去移除動(dòng)畫(huà) 35 [self.myImageView.layer removeAnimationForKey:@"basic"]; 36 37 } 38 39 #pragma mark - CAKframeAnimation動(dòng)畫(huà)按鈕的響應(yīng)方法 40 - (IBAction)keyFrameAnimation:(id)sender { 41 42 // 第一步: 創(chuàng)建對(duì)象 43 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation]; 44 // 第二步: 設(shè)置動(dòng)畫(huà)軌跡 45 keyFrameAnimation.keyPath = @"transform.rotation"; 46 47 // 第三步: 設(shè)置旋轉(zhuǎn)的角度(弧度的計(jì)算公式: 度數(shù) / 180 *M_PI) 48 keyFrameAnimation.values = @[@(-90/180.0*M_PI),@(90/180.0*M_PI),@(8/180.0*M_PI)]; 49 50 // 第四步: 設(shè)置時(shí)長(zhǎng) 51 keyFrameAnimation.duration = 3; 52 53 // 第五步: 添加動(dòng)畫(huà)到layer層 54 [self.myImageView.layer addAnimation:keyFrameAnimation forKey:@"keyFrameAnimation"]; 55 56 } 57 58 #pragma mark - 組動(dòng)畫(huà)的響應(yīng)事件 59 - (IBAction)animationGroup:(id)sender { 60 // 平移動(dòng)畫(huà) 61 CABasicAnimation *basicAnimation1 = [CABasicAnimation animation]; 62 basicAnimation1.keyPath = @"transform.translation.y"; 63 basicAnimation1.toValue = @(400); 64 65 // 翻轉(zhuǎn)動(dòng)畫(huà) 66 CABasicAnimation *basicAnimation2 = [CABasicAnimation animation]; 67 basicAnimation2.keyPath = @"transform.scale"; 68 basicAnimation2.toValue = @(0.2); 69 70 // 旋轉(zhuǎn)動(dòng)畫(huà) 71 CABasicAnimation *basicAnimation3 = [CABasicAnimation animation]; 72 basicAnimation3.keyPath = @"transform.rotation"; 73 basicAnimation3.toValue = @(M_PI); 74 75 // 需要?jiǎng)?chuàng)建管理各個(gè)動(dòng)畫(huà)的動(dòng)畫(huà)組 76 CAAnimationGroup *group = [CAAnimationGroup animation]; 77 78 group.animations = @[basicAnimation1,basicAnimation2,basicAnimation3]; 79 // 設(shè)置時(shí)間 80 group.duration = 5.0f; 81 [self.myImageView.layer addAnimation:group forKey:@"groupAnimation"]; 82 83 } 84 85 #pragma mark - spring動(dòng)畫(huà)的響應(yīng)方法 86 - (IBAction)springAnimation:(id)sender { 87 88 CASpringAnimation *springAnimation = [CASpringAnimation animation]; 89 90 springAnimation.keyPath = @"transform.scale"; 91 92 springAnimation.fromValue = @1; 93 springAnimation.toValue = @0.25; 94 95 springAnimation.duration = 3; 96 [self.myImageView.layer addAnimation:springAnimation forKey:@"springAnimation"]; 97 98 99 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/leikun1113/p/5532738.html

總結(jié)

以上是生活随笔為你收集整理的GIF动画,菊花动画,UIView动画,CoreAnimation动画(CALayer动画)的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。