溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS中怎么實(shí)現(xiàn)一個(gè)AirPods彈出動(dòng)畫

發(fā)布時(shí)間:2021-06-16 14:27:02 來(lái)源:億速云 閱讀:539 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章給大家分享的是有關(guān)iOS中怎么實(shí)現(xiàn)一個(gè)AirPods彈出動(dòng)畫,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

思路

在當(dāng)前ViewController下Present另外一個(gè)AnimationViewController,在彈出的AnimationViewController中播放動(dòng)畫,彈出的時(shí)候原來(lái)的ViewController上有一個(gè)全屏覆蓋的maskView,在彈出時(shí),有一個(gè)漸變動(dòng)畫(頁(yè)面漸黑),在AnimationViewController聲明一個(gè)代理,在代理方法中實(shí)現(xiàn)收起的動(dòng)畫效果(dissmissController和maskView消失)

主要代碼

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ì)覆蓋在原來(lái)的控制器之上

- (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)

- (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)畫開源庫(kù)(Airbnb),這個(gè)開源庫(kù)主要的功能是可以將After Effects制作的動(dòng)畫通過插件導(dǎo)出為json格式的文件,然后通過這個(gè)開源庫(kù)解析成動(dòng)畫。

- (void)onConfirmButtonClick
{
  if ([self.delegate respondsToSelector:@selector(onAirPodsAnimationViewControllerConfirmButtonClick:)]) {
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.delegate onAirPodsAnimationViewControllerConfirmButtonClick:self];
  }
}

dissmiss當(dāng)前的控制器,讓viewController來(lái)實(shí)現(xiàn)這個(gè)代理方法,并且在代理方法中隱藏maskView

- (void)onAirPodsAnimationViewControllerConfirmButtonClick:(HCAirPodsAnimationViewController *)vc
{
  [UIView animateWithDuration:0.2 animations:^{
    self.maskBgView.alpha = 0.0;
  } completion:nil];
}

以上就是iOS中怎么實(shí)現(xiàn)一個(gè)AirPods彈出動(dòng)畫,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI