iOS中動畫(一)——UIView的動畫

來源:互聯網
上載者:User

iOS中UIView的動畫

ViewController1.h

#import @interface ViewController1 : UIViewController@property(nonatomic,retain)UIView* view1;@end
ViewController1.m



#import "ViewController1.h"@interface ViewController1 ()@end@implementation ViewController1- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.           _view1 = [[UIView alloc]init];    _view1.frame = CGRectMake(0, 44, 160, 200);    _view1.backgroundColor = [UIColor redColor];    [self.view addSubview:_view1];                //過度動畫的效果,是2個View的切換    UIButton* button2 = [[UIButton alloc]initWithFrame:CGRectMake(20, 340, 150, 30)];    [button2 addTarget:self action:@selector(button2) forControlEvents:UIControlEventTouchUpInside];    [button2 setTitle:@"開始動畫" forState:UIControlStateNormal];    button2.backgroundColor = [UIColor redColor];    [self.view addSubview:button2];}-(void)button2{    // 1 傳統方法    //開始動畫    /*[UIView beginAnimations:@"testanimation" context:nil];    [UIView setAnimationDuration:0.5];    //動畫的代理    [UIView setAnimationDelegate:self];    //動畫的響應事件,使視圖自動回到原來的位置    [UIView setAnimationDidStopSelector:@selector(animationstop)];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    //獲得視圖的位置    CGRect frames = self.view1.frame;    //將座標移到右邊160處    frames.origin.x = 160;    self.view1.frame = frames;    [UIView commitAnimations];*/        // 2 用block文法實現    [UIView animateWithDuration:0.5 animations:^{        CGRect frames = self.view1.frame;        frames.origin.x = 160;        self.view1.frame = frames;        //縮放        self.view1.transform = CGAffineTransformScale(self.view1.transform, 0.01, 0.01);    }completion:^(BOOL finished) {        CGRect frames = self.view1.frame;        frames.origin.x = 0;        self.view1.frame = frames;        //恢複到原始的縮放        self.view1.transform = CGAffineTransformIdentity;    }];        }-(void)animationstop{    //傳統方法    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.5];    CGRect frames = self.view1.frame;    frames.origin.x = 0;    self.view1.frame = frames;    [UIView commitAnimations];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.