Skip to content

iOS — UIView动画 #5

Open
Open

Description

@cssoul

IOS — UIView动画

在iOS开发中,零零散散接触了一些动画效果,不过没有系统的学习,接到有动画效果的需求总会有些恐慌。所以,最近抽时间在学习iOS动画知识。UIView动画是开发过程中使用比较多的,今天先总结下如何使用UIView动画:

一、使用UIView类实现动画

基本写法,代码必须放在Begin和Commit之间:

[UIView beginAnimations:nil context:nil]; // 开始动画
// Code...
[UIView commitAnimations]; // 提交动画  

举个例子:

[UIView beginAnimations:nil context:nil]; // 开始动画
[UIView setAnimationDuration:5.f]; // 动画时长

CGPoint point = _imgView.center;
point.x += 100;                     
[_imgView setCenter:point];     //图片向右边移动

[UIView commitAnimations]; // 提交动画同时运行多个动画效果:

如果要同时运行多个动画效果,可以这样:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.f];
[_imageView setAlpha:0.5];
[UIView commitAnimations];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.f];
CGPoint point = _imageView.center;
point.x += 100;
[_imageView setCenter:point];
[UIView commitAnimations];

以上代码实现的动画效果为:图片透明度为变为0、同时向右平移150像像,两个动画效果会同时进行

一些常用的方法及属性:

// 开始动画
+ (void)beginAnimations:(NSString *)animationID context:(void *)context;

// 提交动画
+ (void)commitAnimations; 

// 设置动画曲线,默认是匀速进行:
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;

// 设置动画时长:
+ (void)setAnimationDuration:(NSTimeInterval)duration;

// 默认为YES。为NO时跳过动画效果,直接跳到执行后的状态。
+ (void)setAnimationsEnabled:(BOOL)enabled;

// 设置动画延迟执行(delay:秒为单位):
+ (void)setAnimationDelay:(NSTimeInterval)delay;

// 动画的重复播放次数
+ (void)setAnimationRepeatCount:(float)repeatCount;

// 设置动画代理:
+ (void)setAnimationDelegate:(id)delegate;

// 动画将要开始时执行方法××(必须要先设置动画代理):
+ (void)setAnimationWillStartSelector:(SEL)selector;

// 动画已结束时执行方法××(必须要先设置动画代理):
+ (void)setAnimationDidStopSelector:(SEL)selector;

/**
 *  设置动画过渡效果
 *
 *  @param transition 过渡效果
 *  @param view 作用视图
 *  @param cache 如果为YES,开始和结束视图分别渲染一次并在动画中创建帧;否则,视图将会渲染每一帧。
 */
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

// 删除所有动画层
// 注意:层指的是layout,例:[_imageView.layer removeAllAnimations];
- (void)removeAllAnimations;

二、使用UIView的动画块代码:

方法一:

[UIView animateWithDuration:1.0 // 动画时长
                animations:^{
                    // code...
                }];

方法二:

[UIView animateWithDuration:1.0 // 动画时长
                animations:^{
                    // code...
                }
                completion:^(BOOL finished) {
                    // 动画完成后执行
                    // code...
                }];

方法三:

[UIView animateWithDuration:4.0 // 动画时长
                    delay:2.0 // 动画延迟
                  options:UIViewAnimationOptionCurveEaseIn // 动画过渡效果
                 animations:^{
                   // code...
                 }
                 completion:^(BOOL finished) {
                   // 动画完成后执行
                   // code...
                 }];

方法四:

在IOS7开始,系统动画效果广泛应用Spring Animation 。Spring Animation 是线性动画或 ease-out 动画的理想替代品。由于 iOS 本身大量使用的就是 Spring Animation,用户已经习惯了这种动画效果,因此使用它能使 App 让人感觉更加自然

[UIView animateWithDuration:4.0 // 动画时长
            delay:0.0 // 动画延迟
   usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
    initialSpringVelocity:5.0 // 初始速度
          options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
         animations:^{
           // code...
         } completion:^(BOOL finished) {
           // code...
         }];

注意:
usingSpringWithDamping:它的范围为 0.0f 到 1.0f ,数值越小振动效果越明显;

initialSpringVelocity:初始的速度,数值越大一开始移动越快。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

看到这些动画,使用方法其实和CSS3的transition很相似,不过OC的动画更强大,手机上体验更加流畅一些。

方法五:

IOS7以后苹果新加了一个animateKeyframesWithDuration的方法,我们可以使用它来创建更多更复杂更酷炫的动画效果,此方法跟CSS3的Animation动画一样,可以逐帧处理动画效果。

主要是两个方法,一个是创建关键帧,还有一个是添加关键帧:

/**
 *  添加关键帧方法
 *
 *  @param duration   动画时长
 *  @param delay      动画延迟
 *  @param options    动画效果选项
 *  @param animations 动画执行代码
 *  @param completion 动画结束执行代码
 */
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
                               delay:(NSTimeInterval)delay
                             options:(UIViewKeyframeAnimationOptions)options
                          animations:(void (^)(void))animations
                          completion:(void (^)(BOOL finished))completion;


/**
 *  添加关键帧
 *
 *  @param frameStartTime 动画相对开始时间
 *  @param frameDuration  动画相对持续时间
 *  @param animations     动画执行代码
 */
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
                        relativeDuration:(double)frameDuration
                              animations:(void (^)(void))animations;

分享一个弹窗动画:

self.transform = CGAffineTransformMakeScale(0.6, 0.6);
[UIView animateWithDuration:0.2
                 animations:^{
                     self.transform = CGAffineTransformMakeScale(1, 1);
                     }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:1.0/15.0
                                      animations:^{
                                          self.transform = CGAffineTransformMakeScale(0.9, 0.9);
                                      }
                                      completion:^(BOOL finished) {
                                          [UIView animateWithDuration:1.0/7.5
                                                           animations:^{
                                                                   self.transform = CGAffineTransformIdentity;
                                                           }];
                                     }];
                 }];

总结:

其实UIView实现动画的方法非常多,一般简单的动画效果可以随意使用某种方法,比较复杂的动画效果可以选用关键帧KeyFrame方法来实现。至于具体选哪种方式,还得根据交互的效果来定夺了:-D

Activity

  1. changed the title [-]IOS — UIView动画[/-] [+]iOS — UIView动画[/+] on May 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions