您的位置:首页 > 产品设计 > UI/UE

UIView 动画基本学习

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

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

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

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

以下就是代码

- (void)viewDidLoad {

    [super
viewDidLoad];
   
UIView *view = [[UIView
alloc] initWithFrame:CGRectMake(40,
40, 100,
100)];

    [view setBackgroundColor:[UIColor
greenColor]];
    [view
setTag:1001];
    [self.view
addSubview:view];

    //第一个button

    UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
    button.frame =
CGRectMake(10,
400, 80,
40);

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

    [button addTarget:self
action:@selector(doAnimations:)
forControlEvents:UIControlEventTouchUpInside];
    [self.view
addSubview:button];

    //第二个button

    UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
    button1.frame =
CGRectMake(100,
400, 80,
40);

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

    [button1 addTarget:self
action:@selector(doTransition:)
forControlEvents:UIControlEventTouchUpInside];
    [self.view
addSubview:button1];

    //第三个button

    UIButton *button2 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
    button2.frame =
CGRectMake(200,
400, 80,
40);

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

    [button2 addTarget:self
action:@selector(doBlockAnimation:)
forControlEvents:UIControlEventTouchUpInside];
    [self.view
addSubview: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(60, 200, 170, 110);

    //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(60, 200, 170, 110);

    //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(40, 70, 200, 150);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor yellowColor]];

    [UIView commitAnimations];
}

//通过block进行动画操作, block函数也有几个,只使用其中一个
- (void)doBlockAnimation:(UIButton *)button
{
   
UIView *view = [self.view
viewWithTag:1001];

    //  [UIView set]

    [UIView
beginAnimations:nil
context:NULL];

    

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

    [UIView
animateWithDuration:2
animations:^{

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

        [UIView
setAnimationDuration:5];

        [UIView
setAnimationRepeatCount:4];

        //自动恢复到位置

        [UIView
setAnimationRepeatAutoreverses:YES];

        [UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
        view.frame =
CGRectMake(40,
70, 200,
150);

        //view.alpha = 0.0;

        [view setBackgroundColor:[UIColor
yellowColor]];
    }
completion:^(BOOL finished) {

       // [view removeFromSuperview];
    }];

    

    [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(60, 200, 170, 110);

    //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(40, 70, 200, 150);

    //view.alpha = 0.0;

    [view setBackgroundColor:[UIColor yellowColor]];

    [UIView commitAnimations];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiview 动画