ios之UIView执行基础动画Animation使用参数配置

在ios开发中一般用到的基础动画有以下几种,所有的动画参数配置大致相同,但是有时候在开发过程中很少这样配置一般使用代码块比较方便,而且代码也比较简单以下是常用基础动画类型的一个配置

#pragma mark -- Action methods

- (void)transitionAnimation// 转场动画
{

    //设置动画名称,方便代理方法判断是哪个动画
    [UIView beginAnimations:@"TransitionAnimation" context:NULL];
    //设置动画时长
    [UIView setAnimationDuration:3.0];
    //设置动画的变化规律 --有以下4中枚举值
    
//    UIViewAnimationCurveEaseInOut,     开始和结束减速    // slow at beginning and end
//    UIViewAnimationCurveEaseIn,            // slow at beginning
//    UIViewAnimationCurveEaseOut,           // slow at end
//    UIViewAnimationCurveLinear           匀速
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    // 对View设置转场动画方向 有以下枚举方向
//    typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
//        UIViewAnimationTransitionNone,
//        UIViewAnimationTransitionFlipFromLeft,
//        UIViewAnimationTransitionFlipFromRight,
//        UIViewAnimationTransitionCurlUp,
//        UIViewAnimationTransitionCurlDown,
//    };
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_view cache:NO];
    // 对View设置初始状态 并对其他进行配置(这里只是让View变成之前的参数而已,如果不需要就可以去掉这段代码)
    [_view setTransform:CGAffineTransformIdentity];
    [_view setBackgroundColor:[UIColor blackColor]];
    [_view setAlpha:1];
    [_view setCenter:CGPointMake(50, 50)];
    // 设置代理
    [UIView setAnimationDelegate:self];
    // 动画结束执行代理方法(这里走得时代理方法也可以走其他动画方法就可以形成动画组)
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    //动画结束
    [UIView commitAnimations];
}
- (void)changeAlphaAnimation //改变透明度动画
{
    [UIView beginAnimations:@"ChangeAlphaAnimation" context:NULL];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [_view setAlpha:0.2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

- (void)changeColorAnimation // 改变颜色动画
{
    [UIView beginAnimations:@"ChangeColorAnimation" context:NULL];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [_view setBackgroundColor:[UIColor redColor]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}
- (void)rotationAnimation // 旋转动画
{
    [UIView beginAnimations:@"RotationAnimation" context:NULL];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [_view setTransform:CGAffineTransformRotate(_view.transform, M_PI_4)];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(changeColorAnimation)];
    [UIView commitAnimations];
}

- (void)scareAnimation //放大缩小动画
{
    [UIView beginAnimations:@"ScareAnimation" context:NULL];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [_view setTransform:CGAffineTransformScale(_view.transform, 2, 2)];
    [UIView setAnimationDidStopSelector:@selector(rotationAnimation)];
    [UIView commitAnimations];
}

- (void)positionAnimation //位移动画
{
    [UIView beginAnimations:@"PositionAnition" context:NULL];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    _view.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
    [UIView setAnimationDidStopSelector:@selector(scareAnimation)];
    [UIView commitAnimations];
}

// 代理方法,检测动画介绍然后进行其他操作 还有其他两个方法
- (void)animationDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context
{
    // 判断是哪个动画  然后执行相应操作
    if ([animationId isEqualToString:@"ChangeColorAnimation"]) {
        [self changeAlphaAnimation];
    }
    if ([animationId isEqualToString:@"ChangeAlphaAnimation"]) {
        [self transitionAnimation];
    }
}


下面我们来看下代码块的用法,代码块的话用起来很方便并且可以执行回调,在APP点击菜动态单切换两个View或是其他动画

<pre name="code" class="objc">[UIView animateWithDuration:0.5 animations:^{
        [UIView setAnimationDelay:0.8];//配置动画时延
        _currentView.center = CGPointMake(X,Y);//可以对多个view进行我们想要的动画配置
        newView.center = CGPointMake(X,Y);
   
    } completion:^(BOOL finished) {
        //执行完后走这里的代码块
    }];






郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。