iOS 圓形擴散跳轉(zhuǎn)轉(zhuǎn)場動畫

最近寫代碼的時候得到了一個需求,很多APP上都遇見過,就是圓形擴散的轉(zhuǎn)場動畫。今天分享給大家,看似很復(fù)雜的效果,實際上非常簡單。

先上圖:


轉(zhuǎn)場動畫.gif

首先我們需要兩個控制器:ViewController、SecondViewController。我們從ViewController跳轉(zhuǎn)到SecondViewController。

這里我們先展示SecondViewController控制器的內(nèi)容:

#import "SecondViewController.h"

@interface SecondViewController ()

/// 圖片
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.imageView];
}

#pragma mark - Get方法
-(UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
        _imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    
    return _imageView;
}

@end

只有一張圖片,是一個很簡單的控制器
接下來展示ViewController:

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController () <CAAnimationDelegate>

/// 按鈕
@property (nonatomic, strong) UIButton *presentButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.presentButton];
}

#pragma mark - 點擊事件
-(void)persentAction:(UIButton *)button {
    // 充當(dāng)漸變的背景(可以用自定義view代替)
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    bgImageView.frame = self.view.frame;
    [self.view addSubview:bgImageView];
    
    UIBezierPath *maskStartBP =  [UIBezierPath bezierPathWithOvalInRect:button.frame];
    CGFloat radius = [UIScreen mainScreen].bounds.size.height - 100;
    UIBezierPath *maskFinalBP = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(button.frame, -radius, -radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = maskFinalBP.CGPath;
    maskLayer.backgroundColor = (__bridge CGColorRef)([UIColor whiteColor]);
    bgImageView.layer.mask = maskLayer;
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.fromValue = (__bridge id)(maskStartBP.CGPath);
    maskLayerAnimation.toValue = (__bridge id)((maskFinalBP.CGPath));
    // 時間
    maskLayerAnimation.duration = 5.f;
    maskLayerAnimation.delegate = self;
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}

/// 結(jié)束事件
-(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    SecondViewController *viewController = [[SecondViewController alloc] init];
    
    [self presentViewController:viewController animated:NO completion:nil];
}

/// 開始事件
-(void)animationDidStart:(CAAnimation *)anim {
    NSLog(@"開始事件");
}

#pragma mark - Get方法
-(UIButton *)presentButton {
    if (!_presentButton) {
        _presentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _presentButton.frame = CGRectMake(0, 0, 100, 40);
        _presentButton.center = self.view.center;
        _presentButton.backgroundColor = [UIColor whiteColor];
        [_presentButton setTitle:@"跳轉(zhuǎn)" forState:UIControlStateNormal];
        [_presentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_presentButton addTarget:self action:@selector(persentAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _presentButton;
}

@end

其實在做轉(zhuǎn)場動畫的時候,我添加了一個“假”的視圖,當(dāng)動畫結(jié)束的時候進行跳轉(zhuǎn)。效果就顯得很酷炫了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 好像記事以來,最近的一場發(fā)燒是上小學(xué)的時候。自己的體質(zhì)一直都不是易生小病的屬性。這次從年三十開始低燒,燒到初一晚上...
    aurora硬硬同學(xué)閱讀 222評論 0 0
  • 我并不知道要如何寫這封信,如同不能與你好好做道別?;厥走^去漫長的時光里,我們不斷地犯錯,不斷的錯過。你最喜歡我...
    37s閱讀 456評論 0 0
  • 卡沙是一條圍巾。 當(dāng)他還是一團毛線時,他就知道,自己將來會被眼前這個,笑起來眉眼彎彎的女孩送給男朋友。 從貨柜上的...
    瑜音蕘閱讀 552評論 16 28
  • 笑話是很早就鬧出去了,因為我又迎上你那驚奇的目光?!罢娴倪@么想???”說這話時,你的眼睛特別的明亮,它閃著細(xì)細(xì)密密的...
    壯小夢閱讀 276評論 0 1

友情鏈接更多精彩內(nèi)容