UIView 动画基本学习

来源:互联网 发布:网络言论自由权的内涵 编辑:程序博客网 时间:2024/06/05 19:01

UIView 可以设置很多动画, 淡入淡出,改变颜色等,只要把这些独立动画合并到一起,就会起到意想不到的效果

创建一个 UIview  添加一个UIButton 当点击button时执行动作。我添加了三个button做不同的动画效果。

前两个button只是普通操作,第三个button使用的是block动画方式, 这两种的区别是普通的动画要写更多的代码,block只是用一行代码实现功能,

但是block里面也可以写入很多设置代码,如设置时间,透明度,执行次数等。

以下就是代码

- (void)viewDidLoad {

    [superviewDidLoad];

   UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(40,40, 100, 100)];

    [view setBackgroundColor:[UIColorgreenColor]];

    [viewsetTag:1001];

    [self.viewaddSubview:view];

    //第一个button

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame =CGRectMake(10,400, 80, 40);

    [button setTitle:@"Do Ani"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(doAnimations:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    //第二个button

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button1.frame =CGRectMake(100,400, 80, 40);

    [button1 setTitle:@"Do Back"forState:UIControlStateNormal];

    [button1 addTarget:selfaction:@selector(doTransition:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button1];

    //第三个button

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button2.frame =CGRectMake(200,400, 80, 40);

    [button2 setTitle:@"Blk ani"forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(doBlockAnimation:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button2];

    // Do any additional setup after loading the view, typically from a nib.

}




- (void)doAnimations:(UIButton *) button

{

//通过tag值取得当前的view

    UIView *view = [self.view viewWithTag:1001];

//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间

    [UIView beginAnimations:nil context:NULL];


//动画过程 

    [UIView setAnimationDuration:1.5]; //动画时间

    [UIView setAnimationRepeatCount:2]; //动画执行次数

    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果 枚举类型,可以通过API修改其他值

    [UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复

    view.frame = CGRectMake(60200170110);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor redColor]]; //改变颜色


    [UIView commitAnimations];

}


- (void)doAnimations:(UIButton *) button

{

//通过tag值取得当前的view

    UIView *view = [self.view viewWithTag:1001];

//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间

    [UIView beginAnimations:nil context:NULL];


//动画过程 

    [UIView setAnimationDuration:1.5]; //动画时间

    [UIView setAnimationRepeatCount:2]; //动画执行次数

    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果 枚举类型,可以通过API修改其他值

    [UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复

    view.frame = CGRectMake(60200170110);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor redColor]]; //改变颜色


    [UIView commitAnimations];

}


- (void)doTransition:(UIButton *) button

{

    UIView *view = [self.view viewWithTag:1001];


  //  [UIView set]

    [UIView beginAnimations:nil context:NULL];

//加入翻页效果,也是枚举类型 可以查API, cache的就是说要不要缓存,为了提高运行速度,一般选 YES

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];

    [UIView setAnimationDuration:1.5];

    [UIView setAnimationRepeatCount:2];

    //自动恢复到位置

    [UIView setAnimationRepeatAutoreverses:YES];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    view.frame = CGRectMake(4070200150);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor yellowColor]];

    [UIView commitAnimations];

}


//通过block进行动画操作, block函数也有几个,只使用其中一个

- (void)doBlockAnimation:(UIButton *)button

{

   UIView *view = [self.viewviewWithTag:1001];


    //  [UIView set]

    [UIViewbeginAnimations:nilcontext:NULL];

    

//第一个参数执行次数, 第二个参数就是动作, 第三个结束后执行什么动作,可以是删除,改变颜色等等

    [UIViewanimateWithDuration:2animations:^{

        [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:view cache:YES];

        [UIViewsetAnimationDuration:5];

        [UIViewsetAnimationRepeatCount:4];

        //自动恢复到位置

        [UIViewsetAnimationRepeatAutoreverses:YES];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

        view.frame =CGRectMake(40,70, 200, 150);

        //view.alpha = 0.0;

        [view setBackgroundColor:[UIColoryellowColor]];

    }completion:^(BOOL finished) {

       // [view removeFromSuperview];

    }];

    

    [UIViewcommitAnimations];

}







- (void)doAnimations:(UIButton *) button

{

//通过tag值取得当前的view

    UIView *view = [self.view viewWithTag:1001];

//开始执行动作代码, 所有动画效果代码都要写在这个 开始和结束动画代码之间

    [UIView beginAnimations:nil context:NULL];


//动画过程 

    [UIView setAnimationDuration:1.5]; //动画时间

    [UIView setAnimationRepeatCount:2]; //动画执行次数

    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //动画效果 枚举类型,可以通过API修改其他值

    [UIView setAnimationRepeatAutoreverses:YES]; //要不要恢复

    view.frame = CGRectMake(60200170110);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor redColor]]; //改变颜色


    [UIView commitAnimations];

}


- (void)doTransition:(UIButton *) button

{

    UIView *view = [self.view viewWithTag:1001];


  //  [UIView set]

    [UIView beginAnimations:nil context:NULL];

// 

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];

    [UIView setAnimationDuration:1.5];

    [UIView setAnimationRepeatCount:2];

    //自动恢复到位置

    [UIView setAnimationRepeatAutoreverses:YES];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    view.frame = CGRectMake(4070200150);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor yellowColor]];

    [UIView commitAnimations];

}

0 0
原创粉丝点击