天天看點

iOS入門-44UIView動畫基礎概述示例

概述

iOS中動畫的實作方式

簡單的展示平移、縮放、透明度動畫

示例

給一張圖檔添加縮放、平移、透明度動畫。很簡單看注釋

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //加載一張圖檔,展示到螢幕中
    UIImage* image = [UIImage imageNamed:@"timg_1.jpg"];
    _imgView = [UIImageView new];
    _imgView.frame = CGRectMake(50, 50, 200, 200);
    _imgView.image = image;
    [self.view addSubview:_imgView];
    
    //點選開始動畫按鈕
    UIButton* btnMove = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnMove setTitle:@"begin animation" forState:UIControlStateNormal];
    btnMove.frame = CGRectMake(40, 700, 150, 40);
    [btnMove addTarget:self action:@selector(pressStart:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnMove];
}

-(void)pressStart:(UIButton*) btn{
    NSLog(@"pressMove==");
    //UIView的動畫
    //p1 動畫執行時間(機關:秒)
    //p2 動畫延時執行時間(機關:秒)
    //p3 動畫配置參數(例如起始狀态、動畫變化曲線等等)
    //p4 動畫執行代碼塊
    //p5 完成動畫回調
    [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        //位移和縮放
        //_imgView.frame = CGRectMake(200, 200, 100, 100);
        //透明度
        _imgView.alpha = 0.3;
    } completion:^(BOOL finished) {
        NSLog(@"動畫完成了。。");
    }];
}

@end