iOS偽轉場動畫

Guru發表於2016-11-20

寫在艱苦的日子裡。

  • 本人在簡書作者BOC提供的思路下來實現一些動畫效果。
  • Github原始碼地址:github.com/JonHory/Tra…

  • 通過modal進行轉場,設定modalTransitionStyleUIModalTransitionStyleCrossDissolve,設定modalPresentationStyleUIModalPresentationOverFullScreen

圖片放大縮小的效果

外部只需要傳入一個外部的UIImageView即可
1.建立一個UIImageView和一個CGRect來儲存圖片資訊和圖片frame資訊。
使用- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;方法來獲取圖片的相對位置。
2.檢視將要出現時做動畫效果

    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [UIView animateWithDuration:AnimationTime animations:^{       
        //這裡可以自由發揮了
        self.view.backgroundColor = [UIColor greenColor];self.currentIV.frame = CGRectMake(0, 0, 200, 200);   self.currentIV.center = CGPointMake(self.view.center.x, 200);
        } completion:^(BOOL finished) {
        }];
    }複製程式碼

3.返回的點選事件處理

- (void)back{
    [UIView animateWithDuration:AnimationTime animations:^{
        if ([self.type isEqualToString:@"vv"]) {
            self.view.center = CGPointMake(self.view.center.x, self.view.bounds.size.height*1.5);
        }else {
            self.currentIV.frame = self.oldFrame;
            self.view.backgroundColor = [UIColor clearColor];
        }
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];

}複製程式碼

4.就這樣,效果就出來啦

iOS偽轉場動畫

Boss直聘動畫效果

用的是截圖的方法來實現背景縮小後置的效果
1.在當前UIViewController用一個UIImageView來儲存截圖

-(UIImage *)screenImageWithSize:(CGSize )imgSize{
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate; //獲取app的appdelegate,便於取到當前的window用來截圖
    [app.window.layer renderInContext:context];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}複製程式碼

2.在下一個VC建立一個減方法,在當前VC中使用

[self presentViewController:vc animated:NO completion:nil];

    [UIView animateWithDuration:0.3 animations:^{
    //當前VC下截圖的縮小效果
        self.screenshot.bounds = CGRectMake(50, 50, SCREEN.width - 100, SCREEN.height - 100);
    } completion:^(BOOL finished) {
    //這就是減方法 =_= 動畫結束時,改變背景塊的frame
        [vc changeBigView];
    }];複製程式碼

3.在下一個VC返回時,當前VC的截圖檢視移除。

[weakSelf.screenshot removeFromSuperview];複製程式碼

4.這樣,BOSS直聘的動畫效果就出現了。

iOS偽轉場動畫

按以上思路,就可以實現一些簡單常見的動畫效果^0^

相關文章