本文實(shí)例為大家分享了ios仿airpods彈出動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
預(yù)覽圖
思路
在當(dāng)前viewcontroller下present另外一個(gè)animationviewcontroller,在彈出的animationviewcontroller中播放動(dòng)畫,彈出的時(shí)候原來的viewcontroller上有一個(gè)全屏覆蓋的maskview,在彈出時(shí),有一個(gè)漸變動(dòng)畫(頁面漸黑),在animationviewcontroller聲明一個(gè)代理,在代理方法中實(shí)現(xiàn)收起的動(dòng)畫效果(dissmisscontroller和maskview消失)
主要代碼
1
2
3
4
5
6
7
8
9
10
11
|
hcairpodsanimationviewcontroller *vc = [[hcairpodsanimationviewcontroller alloc] init]; vc.delegate = self; vc.modalpresentationstyle = uimodalpresentationovercurrentcontext; [uiview animatewithduration:0.2 animations:^{ self.maskbgview.alpha = 0.5; } completion:nil]; [self presentviewcontroller:vc animated:yes completion:^{ [vc.animationview play]; }]; |
模態(tài)跳轉(zhuǎn)的style有一個(gè)枚舉值,在ios13以前modalpresentationstyle的默認(rèn)值為uimodalpresentationfullscreen,ios13以后變成了uimodalpresentationpagesheet,在這里我們把style設(shè)置為uimodalpresentationovercurrentcontext彈出的這個(gè)控制器就會(huì)覆蓋在原來的控制器之上
1
2
3
4
5
6
7
8
9
10
|
- (uiview *)maskbgview { if (!_maskbgview) { _maskbgview = [[uiview alloc] initwithframe:cgrectmake(0, 0, [uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height)]; _maskbgview.backgroundcolor = [uicolor blackcolor]; _maskbgview.alpha = 0; [self.view addsubview:_maskbgview]; } return _maskbgview; } |
一個(gè)覆蓋全屏的maskview采用懶加載的方式實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
- ( void )initcontentview { cgfloat containerw = screen_width - 20; cgfloat containerh = containerw * 0.9; uiview *containerview = [[uiview alloc] initwithframe:cgrectmake(10, screen_height - containerh - 10, containerw, containerh)]; containerview.layer.cornerradius = 20; containerview.backgroundcolor = [uicolor whitecolor]; [self.view addsubview:containerview]; self.animationview = [[lotanimationview alloc] initwithframe:cgrectmake(70, 70, containerw - 140, containerh - 140)]; [containerview addsubview:self.animationview]; self.animationview.animation = @ "gift_animation" ; self.animationview.loopanimation = yes; uibutton *confirmbutton = [[uibutton alloc] initwithframe:cgrectmake(0, 0, 200, 34)]; confirmbutton.center = cgpointmake(self.animationview.center.x, containerh - 44); [confirmbutton settitle:@ "close" forstate:uicontrolstatenormal]; [confirmbutton settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; [confirmbutton setbackgroundcolor:[uicolor bluecolor]]; confirmbutton.layer.cornerradius = 10; [confirmbutton addtarget:self action:@selector(onconfirmbuttonclick) forcontrolevents:uicontroleventtouchupinside]; [containerview addsubview:confirmbutton]; } |
動(dòng)畫這里用到的是lottie這個(gè)動(dòng)畫開源庫(airbnb),這個(gè)開源庫主要的功能是可以將after effects制作的動(dòng)畫通過插件導(dǎo)出為json格式的文件,然后通過這個(gè)開源庫解析成動(dòng)畫。
1
2
3
4
5
6
7
|
- ( void )onconfirmbuttonclick { if ([self.delegate respondstoselector:@selector(onairpodsanimationviewcontrollerconfirmbuttonclick:)]) { [self dismissviewcontrolleranimated:yes completion:nil]; [self.delegate onairpodsanimationviewcontrollerconfirmbuttonclick:self]; } } |
dissmiss當(dāng)前的控制器,讓viewcontroller來實(shí)現(xiàn)這個(gè)代理方法,并且在代理方法中隱藏maskview
1
2
3
4
5
6
|
- ( void )onairpodsanimationviewcontrollerconfirmbuttonclick:(hcairpodsanimationviewcontroller *)vc { [uiview animatewithduration:0.2 animations:^{ self.maskbgview.alpha = 0.0; } completion:nil]; } |
項(xiàng)目地址:airpodsanimation
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Peter_Huang0623/article/details/103533619