iOS开发之UIView中的动画属性

源博客:(http://www.cnblogs.com/pengyingh/articles/2378777.html)
areAnimationsEnabled返回一个布尔值表示动画是否结束
+ (BOOL)areAnimationsEnabled返回值如果动画结束返回YES,否则NO。

beginAnimations:context:开始一个动画块
+ (void)beginAnimations:(NSString *)animationID context:(void *)context
参数animationID动画块内部应用程序标识用来传递给动画代理消息-这个选择器运用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法来设置。context附加的应用程序信息用来传递给动画代理消息-这个选择器使用setAnimationWillStartSelector: 和setAnimationDidStopSelector: 方法。讨论这个值改变是因为设置了一些需要在动画块中产生动画的属性。动画块可以被嵌套。如果在没有在动画块中调用那么setAnimation类方法将什么都不做。使用 beginAnimations:context:来开始一个动画块并用commitAnimations类方法来结束一个动画块。 结束一个动画块并开始当他在动画块外时。+ (void)commitAnimations如果当前的动画块是最外层的动画块,当应用程序返回到循环运行时开始动画块。动画在一个独立的线程中所有应用程序不会中断。使用这个方法,多个动画可以被实现。查看setAnimationBeginsFromCurrentState:来了解如果开始一个动画当另外一个动画在播放的时候。

layerClass返回类用来创建这一个本类的layer实例对象。
+ (Class)layerClass
返回值一个用来创建视图layer的类重写子类来指定一个自定义类用来显示。当在创建视图layer时候调用。默认的值是CALayer类对象。

setAnimationBeginsFromCurrentState:设置动画从当前状态开始播放。
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState
参数fromCurrentStateYES如果动画需要从他们当前状态开始播放。否则为NO。讨论如果设置为YES那么当动画在运行过程中,当前视图的位置将会作为新的动画的开始状态。如果设置为NO,当前动画结束前新动画将使用视图最後状态的位置作为开始状态。这个方法将不会做任何事情如果动画没有运行或者没有在动画块外调用。使用beginAnimations:context:类方法来开始并用commitAnimations类方法来结束动画块。默认值是NO。

setAnimationCurve:设置动画块中的动画属性变化的曲线。
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
讨论动画曲线是动画运行过程中相对的速度。如果在动画块外调用这个方法将会无效。使用 beginAnimations:context:类方法来开始动画块并用commitAnimations来结束动画块。默认动画曲线的值是UIViewAnimationCurveEaseInOut。

setAnimationDelay:在动画块中设置动画的延迟属性(以秒为单位)
+ (void)setAnimationDelay:(NSTimeInterval)delay
讨论这个方法在动画块外调用无效。使用beginAnimations:context: 类方法开始一个动画块并用commitAnimations类方法结束动画块。默认的动画延迟是0.0秒。 内容来自泠云天天在线

setAnimationDelegate:设置动画消息的代理。
+ (void)setAnimationDelegate:(id)delegate参数delegate你可以用setAnimationWillStartSelector:和setAnimationDidStopSelector:
方法来设置接收代理消息的对象。讨论这个方法在动画块外没有任何效果。使用beginAnimations:context:类方法开始一个动画块并用commitAnimations类方法结束一个动画块。默认值是nil

setAnimationDidStopSelector: 设置消息给动画代理当动画停止的时候。
+ (void)setAnimationDidStopSelector:(SEL)selector
参数selector当动画结束的时候发送给动画代理。默认值是NULL。这个选择者须有下面方法的签名:animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context。animationID一个应用程序提供的标识符。和传给beginAnimations:context: 相同的参数。这个参数可以为空。finished如果动画在停止前完成那返回YES;否则就是NO。context一个可选的应用程序内容提供者。和beginAnimations:context: 方法相同的参数。可以为空。 本文来自泠云天天在线讨论这个方法在动画块外没有任何效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法结束。默认值是NULL。

setAnimationDuration:设置动画块中的动画持续时间(用秒)
+ (void)setAnimationDuration:(NSTimeInterval)duration
参数duration一段动画持续的时间。讨论这个方法在动画块外没有效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法来结束一个动画块。默认值是0.2。

setAnimationRepeatAutoreverses:设置动画块中的动画效果是否自动重复播放。
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
参数repeatAutoreverses如果动画自动重复就是YES否则就是NO。讨论自动重复是当动画向前播放结束後再重头开始播放。使用setAnimationRepeatCount: 类方法来指定动画自动重播的时间。如果重复数为0或者在动画块外那将没有任何效果。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations方法来结束一个动画块。默认值是NO。

setAnimationRepeatCount:设置动画在动画模块中的重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount
参数repeatCount动画重复的次数,这个值可以是分数。讨论这个属性在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束。默认动画不循环。

setAnimationsEnabled:设置是否激活动画
+ (void)setAnimationsEnabled:(BOOL)enabled
参数enabled如果是YES那就激活动画;否则就是NO讨论当动画参数没有被激活那么动画属性的改变将被忽略。默认动画是被激活的。

setAnimationStartDate:设置在动画块内部动画属性改变的开始时间
+ (void)setAnimationStartDate:(NSDate *)startTime
参数startTime一个开始动画的时间讨论使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束动画块。默认的开始时间值由CFAbsoluteTimeGetCurrent方法来返回。

setAnimationTransition:forView:cache:在动画块中为视图设置过渡
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
参数transition把一个过渡效果应用到视图中。可能的值定义在UIViewAnimationTransition中。view需要过渡的视图对象。cache如果是YES,那么在开始和结束图片视图渲染一次并在动画中创建帧;否则,视图将会在每一帧都渲染。例如缓存,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。
讨论如果你想要在转变过程中改变视图的外貌。举个例子,文件从一个视图到另一个视图,然後使用一个UIView子类的容器视图,
如下:
1.Begin an animation block.1.开始一个动画块。
2.Set the transition on the container view.2.在容器视图中设置转换。
3.Remove the subview from the container view. 3.在容器视图中移除子视图。
4.Add the new subview to the container view.4.在容器视图中添加子视图。
5.Commit the animation block. 5.结束动画块。

setAnimationWillStartSelector:当动画开始时发送一条消息到动画代理
+ (void)setAnimationWillStartSelector:(SEL)selector
参数selector在动画开始前向动画代理发送消息。默认值是NULL。这个selector必须由和beginAnimations:context: 方法相同的参数,一个任选的程序标识和内容。这些参数都可以是nil。讨论这个方法在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束

UIView基础动画Animation使用参数配置

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

- (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:(voidvoid *)context  
{  
    // 判断是哪个动画  然后执行相应操作  
    if ([animationId isEqualToString:@"ChangeColorAnimation"]) {  
        [self changeAlphaAnimation];  
    }  
    if ([animationId isEqualToString:@"ChangeAlphaAnimation"]) {  
        [self transitionAnimation];  
    }  
}  

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

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

你可能感兴趣的:(iOS开发之UIView中的动画属性)