• UIView Animation


    UIKit 和 Core Animation 都对 animation 提供支持。在UIKit中,动画是用UIView对象展示。

    Animatable UIView properties:

         1.frame :改变view 的大小和该view在superview中的相对位置。

         2.bounds:改变view的大小(尺寸)。

         3.center :改变view 相对于superview坐标系统的位置。

         4.transform:控制UIView的缩放,旋转角度等固定好中心位置之后的变化(这个属性应用于2D空间的展示,如果要展示3D动画,则必须使用Core Animation)

         5.alpha :改变View 的透明度

         6.backgroundColor:改变 view 的背景颜色

         7.contentStretch:改变view 的拉伸方式

    在ios4 以后,使用block-based 类方法来创建动画:

           animateWithDuration: animations:

           animateWithDuration: animations: completion:

          animateWithDuration: delay : options: animations: completion:

    因为是类方法,所以你创建的动画代码块不仅仅是针对一个view。因此,你可以用这些方法创建一个包含多个 view 改变的动画。例如:

    1 [UIView animateWithDuration:3.0 animations:^{
    2         firstView.alpha = 0.25;
    3         secondView.alpha = 1.0;
    4     }];

    当上面代码执行的时候,动画立即在另一个线程中执行,也就避免了阻塞当前线程或者主线程。

    刚才的代码中只使用了淡入,淡出的动画效果。如果你想改变默认的动画参数,你必须使用animateWithDuration: delay: options: animations: completion: 这个方法来创建动画。这个方法可以让你自定义如下动画参数:

         1.动画开始以前的延迟时间。

         2.动画运行的时间曲线。

         3.动画重复的次数。

         4.当动画结束的时候是否自动的反向运行动画

         5.当动画运行的时候,是否触摸事件可以传递到view

         6.是否动画可以中断其他正在运行的动画或者直到其他的其他动画结束才可以开始运行   

    使用Begin/Commit 方法来开始动画:

    如果你的程序运行在 ios 3.2 或者更早的系统, 你必须使用 beginAnimations: context: 和 commitAnimations 的类方法来定义你的动画块。如:

    1     [UIView beginAnimations:@"ToggleViews" context:nil];
    2     [UIView setAnimationDuration:1.0];
    3     firstView.alpha = 0.25;
    4     secondView.alpha = 1.0;
    5     
    6     [UIView commitAnimations];

    你可以为Begin/Commit方法配置如下参数:

     1 + (void)setAnimationDelegate:(id)delegate;                          // default = nil
     2 + (void)setAnimationWillStartSelector:(SEL)selector;                // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
     3 + (void)setAnimationDidStopSelector:(SEL)selector;                  // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
     4 + (void)setAnimationDuration:(NSTimeInterval)duration;              // default = 0.2
     5 + (void)setAnimationDelay:(NSTimeInterval)delay;                    // default = 0.0
     6 + (void)setAnimationStartDate:(NSDate *)startDate;                  // default = now ([NSDate date])
     7 + (void)setAnimationCurve:(UIViewAnimationCurve)curve;              // default = UIViewAnimationCurveEaseInOut
     8 + (void)setAnimationRepeatCount:(float)repeatCount;                 // default = 0.0.  May be fractional
     9 + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;    // default = NO. used if repeat count is non-zero
    10 + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;  // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).

    移动动画的例子:

     1 CGRect rect = _moveView.frame;
     2         if(_count%4==0)
     3             rect.origin.x = 240;
     4         else if(_count%4==1)
     5             rect.origin.y = 336;
     6         else if(_count%4==2)
     7             rect.origin.x = 0;
     8         else if(_count%4==3)
     9             rect.origin.y = 0;
    10         
    11         [UIView animateWithDuration:1 animations:^{
    12             _moveView.frame = rect;
    13         } completion:^(BOOL finished) {
    14             _count++;
    15           
    16         }];

    渐变动画的例子:

     1 [UIView animateWithDuration:1 animations:^{
     2         _view1.alpha = 0;
     3 
     4     } completion:^(BOOL finished) {
     5         if(_isChanged)
     6         {
     7             _view1.backgroundColor = [UIColor purpleColor];
     8         }
     9         else
    10         {
    11             _view1.backgroundColor = [UIColor greenColor];
    12         }
    13         [UIView animateWithDuration:1 animations:^{
    14             _view1.alpha = 1;
    15         } completion:^(BOOL finished) {
    16             _isChanged = !_isChanged;
    17         }];
    18     } ];

    旋转动画的例子:

    1 [UIView animateWithDuration:1 animations:^{
    2         _view1.transform = CGAffineTransformMakeRotation((10*_count)%360*M_PI/180);
    3     } completion:^(BOOL finished) {
    4         _count++;
    5     }];

    翻转动画的例子:

     1 [UIView animateWithDuration:1 animations:^{
     2         if(!_isChanged)
     3         {
     4             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
     5                                    forView:_view1
     6                                      cache:YES];
     7             [_view1 addSubview:_view2];
     8         }
     9         else
    10         {
    11             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
    12                                    forView:_view1
    13                                      cache:YES];
    14             [_view2 removeFromSuperview];
    15         }        
    16     } completion:^(BOOL finished) {
    17         _isChanged = !_isChanged;
    18     }];

    翻页动画的例子:

     1 [UIView animateWithDuration:1 animations:^{
     2         if(!_isChanged)
     3         {
     4             [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
     5                                    forView:_view1
     6                                      cache:YES];
     7             [_view1 addSubview:_view2];
     8         }
     9         else
    10         {
    11             [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
    12                                    forView:_view1
    13                                      cache:YES];
    14             [_view2 removeFromSuperview];
    15         }        
    16     } completion:^(BOOL finished) {
    17         _isChanged = !_isChanged;
    18     }];
  • 相关阅读:
    BUAA2020软工作业(三)——个人项目
    BUAA2020软工作业(二)——对软件工程的初步理解
    BUAA2020软工作业(一)——谈谈我和计算机的缘分
    OO第四单元总结与课程总结
    OO第三单元总结——JML
    面向对象第二单元总结——魔鬼电梯
    面向对象设计与构造——第一单元总结
    提问回顾与个人总结
    【技术博客】Flutter—使用网络请求的页面搭建流程、State生命周期、一些组件的应用
    软件案例分析
  • 原文地址:https://www.cnblogs.com/grq186/p/4618720.html
Copyright © 2020-2023  润新知