iOS彈簧動畫與視圖過渡動畫

這兩種動畫實現起來都很簡單,蘋果已經為我們封裝好了,并且提供了多種好看實用的效果

彈簧動畫

彈簧動畫
- (void)viewDidLoad {
    [super viewDidLoad];

    // 創建一個氣泡
    UIButton * bubble = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:bubble];
    bubble.layer.cornerRadius = 50;
    bubble.backgroundColor = [UIColor cyanColor];

    [bubble addTarget:self action:@selector(bubbleAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)bubbleAction:(UIButton *)button {
    // 先縮小
    button.transform = CGAffineTransformMakeScale(0.7, 0.7);
    
    // 彈簧動畫,參數分別為:時長,延時,彈性(越小彈性越大),初始速度
    [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
        // 放大
        button.transform = CGAffineTransformMakeScale(1, 1);
    } completion:nil];
}

完事

視圖過渡動畫

視圖過渡動畫有兩種,一種是單個視圖的過渡動畫,另一種是兩個UIView之間的過渡

過渡類型:

  • UIViewAnimationOptionTransitionFlipFromTop由上向下翻轉動畫
  • UIViewAnimationOptionTransitionFlipFromBottom由下向上翻轉動畫
  • UIViewAnimationOptionTransitionCrossDissolve漸現動畫
  • UIViewAnimationOptionTransitionCurlDown由上到下翻頁動畫
  • UIViewAnimationOptionTransitionCurlUp由下到上翻頁動畫
  • UIViewAnimationOptionTransitionFlipFromRight由左向右翻轉
  • UIViewAnimationOptionTransitionFlipFromLeft由右向左翻轉

速度

  • UIViewAnimationOptionCurveEaseInOut 緩入緩出,開始和結束時減速
  • UIViewAnimationOptionCurveEaseIn 緩入,開始時候減速
  • UIViewAnimationOptionCurveEaseOut 緩出,結束時減速
  • UIViewAnimationOptionCurveLinear 線性,勻速運動

過渡視圖和速度在動畫的options參數中使用。但是不要使用多種過渡類型,真是要難看死了我的天哪。

options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut

Swift寫法(感謝熊格智障的評論):

options: [.TransitionCrossDissolve,.CurveEaseInOut]

1. 單個視圖的過渡

單個視圖的動畫
- (void)viewDidLoad {
    [super viewDidLoad];

    // 創建一個按鈕
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor orangeColor];
    button.layer.cornerRadius = 3;
    
    [button setTitle:@"??" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAction:(UIButton *)button {
    // transition動畫:指定一個view,單獨為它設置transition的動畫
    // option設置動畫類型,這里使用翻頁動畫以及淡出淡入
    [UIView transitionWithView:button duration:0.7 options:UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionCurveEaseOut animations:^{
        [button setTitle:@"??" forState:UIControlStateNormal];
    } completion:nil];
}

再來一個實用的imageView圖片漸變效果:

圖片漸變
@interface ViewController ()
@property (nonatomic, retain) UIImageView * imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
    [self.view addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"1.jpg"];
    
    // 1s后換圖片
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
}

- (void)changeImage {
    // 使用漸變效果就夠了
    [UIView transitionWithView:_imageView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        _imageView.image = [UIImage imageNamed:@"2.jpg"];
    } completion:nil];
}

2. 兩個視圖的過渡動畫

兩個視圖的過渡動畫

方法:UIViewtransitionFromView: toView: duration: options:方法
官方解釋:toView added to fromView.superview, fromView removed from its superview
實際效果:動畫的作用范圍為fromView的父視圖的frame。動畫結束后fromView會被刪除,toView會被添加到fromView的父視圖

@interface ViewController ()
@property (nonatomic, retain) UIView * view1;
@property (nonatomic, retain) UIView * view2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 創建占用半個屏幕的view,作為過渡view的父視圖,測試動畫的作用范圍
    UIView * bigView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:bigView];
    bigView.backgroundColor = [UIColor orangeColor];

    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    [bigView addSubview:_view1];
    _view1.backgroundColor = [UIColor redColor];
    
    self.view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    // 不要加到視圖上!看效果
//    [self.view addSubview:_view2];
    _view2.backgroundColor = [UIColor blackColor];
    
    // 1s后執行
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(animate) userInfo:nil repeats:NO];
}

- (void)animate {
    [UIView transitionFromView:_view1 toView:_view2 duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
    }];
}
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容