UIView动画

IOS动画实现很简单,几行代码就能制作出很漂亮的效果。

实现一个这样的动画:从视图控制器底部创建一个红色区域的视图,并且慢慢上升。

1.创建

    _aniView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 50)];
    _aniView.backgroundColor = [UIColor redColor];
    _aniView.alpha = 0;
    [self.view addSubview:_aniView];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    _aniView.frame = CGRectMake(0, 500, self.view.bounds.size.width,self.view.bounds.size.height-300);
    _aniView.alpha = 1;
    [UIView commitAnimations];
    
    
    _ctrl = [[UIControl alloc] initWithFrame:self.view.bounds];
    _ctrl.backgroundColor = [UIColor colorWithRed:200.0f/255.0f green:100.0f/255.0f blue:50/255 alpha:0.5];
    [_ctrl addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_ctrl];


2.点击UIControl后,动画消失。

- (void)showView:(UIControl *)ctl{
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1.0];
//    showView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 100);
//    showView.alpha = 0;
//    [UIView commitAnimations];
    
    [UIView animateWithDuration:1.0 animations:^{
        _aniView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height - 300);
//        [_aniView removeFromSuperview];
    }];

    
    [ctl removeFromSuperview];
    
}


你可能感兴趣的:(ios,动画)