iOS 动画--UIView动画

2018-03-05  本文已影响27人  yaqiong

首先看一下官方文档对UIView动画的一些说明

Changes to several view properties can be animated—that is, changing the property creates an animation starting at the current value and ending at the new value that you specify. The following properties of the UIView class are animatable:

  • frame
  • bounds
  • center
  • alpha
  • backgroundColor

改变view的一些属性可以产生动画效果,也就是说,这种动画是从属性的初始值开始,到给定的新值为止。以下几个属性是可动画属性:frame、bounds、center、alpha、backgroundColor

我们可以使用系统提供的UIView的类方法实现动画效果,通过方法中的参数来设置时长、延迟等值

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL

duration:动画时长
delay:延迟时间
options:动画的展示方式
animation:需要进行怎样的动画
completion:动画完成后执行
另外在iOS7之后,又提供了两个关键帧动画方法:

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); 

以及一些转场动画的方法

参数

这些方法,传入不同参数,会得出不同的动画效果:

option
NSArray *optionArray  = @[@(UIViewAnimationOptionTransitionNone),
                              @(UIViewAnimationOptionTransitionFlipFromLeft),
                              @(UIViewAnimationOptionTransitionFlipFromRight),
                              @(UIViewAnimationOptionTransitionCurlUp),
                              @(UIViewAnimationOptionTransitionCurlDown),
                              @(UIViewAnimationOptionTransitionCrossDissolve),
                              @(UIViewAnimationOptionTransitionFlipFromTop),
                              @(UIViewAnimationOptionTransitionFlipFromBottom)];
    for (int i = 0;i < TransitionCount;i ++) {
        UIImageView *subBall = self.imgArray[i];
        [UIView transitionWithView:subBall duration:3 options:[optionArray[i] integerValue] animations:^{
            subBall.image = [UIImage imageNamed:@"leilei1"];
        } completion:nil];
    }

以上代码可以实现如图效果,图片从有小哥哥的一面翻到没有的一面,动画时长3秒:


transition.gif

可以看出,切换图片的时候,原来的图片会以view中心为轴翻转,而且系统还自动加上了阴影效果

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;

主要注意一下参数:

  1. dampingRatio:衰减比例,取值范围为0 -1,因为是衰减比例,所以该值越低震动越强
  2. velocity:初始化速度。值越高速度越快
    如图所示是在velocity不变的情况下,不同dampingRatio的效果:


    spring.gif
上一篇 下一篇

猜你喜欢

热点阅读