iOS Learning note 10-uiview animations

Source: Internet
Author: User

Last time I learned iOS learning Note 09-core animation coreanimation, this time to continue to learn animation, the last use of CoreAnimation many people feel very cumbersome to use, there is no more convenient animation effect to achieve it? The answer is yes, that's UIView. Animation Encapsulation

First, UIView animation

Apple knows that the use of layer animation is troublesome, it is encapsulated UIView in us, so that we can easily achieve a variety of animation effects.

1. The main implementation of the basic animation method is:
+ (void)animateWithDuration:(NSTimeInterval)duration                       delay:(NSTimeInterval)delay                    options:(UIViewAnimationOptions)options                  animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
To move an animation using an instance:
/*   开始动画,UIView的动画方法执行完后动画会停留在终点位置,而不需要进行任何特殊处理  duration:执行时间  delay:延迟时间  options:动画设置,例如自动恢复、匀速运动等  completion:动画完成回调方法*/[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ _imageView.center = location;} completion:^(BOOL finished) { NSLog(@"Animation end.");}];

The above method basically satisfies most of the basic animation requirements, there is a more interesting animation effect

Spring Animation effect:
/*  创建弹性动画  damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显  springVelocity:弹性复位的速度*/[UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{ _imageView.center = location;} completion:^(BOOL finished) { NSLog(@"Animation end.");}];

There is an options parameter in the UIView animation method, which is an enumeration type that can be used in combination:

/* General animation property settings, you can select multiple, use or combination of operations */Uiviewanimationoptionlayoutsubviews/*< to ensure the child view follows the motion during animation */Uiviewanimationoptionallowuserinteraction/*< allow user interaction during animation */Uiviewanimationoptionbeginfromcurrentstate/*< all views run from the current state */Uiviewanimationoptionrepeat/*< Repeat animation */Uiviewanimationoptionautoreverse/*< animation runs back to the starting point */Uiviewanimationoptionoverrideinheritedduration/*< Ignore nested animation time settings */Uiviewanimationoptionoverrideinheritedcurve/*< Ignore nested animation speed settings */uiviewanimationoptionallowanimatedcontent/*< The view during animation, only for transition animations * / Uiviewanimationoptionshowhidetransitionviews/*< View toggle Hide Old view directly, display new view, only for transition animation * / Uiviewanimationoptionoverrideinheritedoptions/*< does not inherit the parent animation settings or animation type/////////////// * Animate speed Change control, choose one  Uiviewanimationoptioncurveeaseinout/*< Animation first slow, after gradual acceleration, default value */ Uiviewanimationoptioncurveeasein/*< Animation slows down */ uiviewanimationoptioncurveeaseout/*< animation gradually accelerates */ uiviewanimationoptioncurvelinear/* < animation uniform execution *               /
2. Key frame animations are mainly implemented by:
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration                                delay:(NSTimeInterval)delay                             options:(UIViewAnimationOptions)options                           animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
Instance:
[UIView animatekeyframeswithduration:5.0 delay:0 Options:Uiviewanimationoptioncurvelinear |Uiviewanimationoptioncurvelinear animations:^{First Keyframe: Lasts 50% of the time from 0 seconds, that is, 5.0*0.5=2.5 seconds [uiview addkeyframewithrelativestarttime:0.0 Relativeduration:0.5 animations:^{_imageview.center = Location1;}]; //second keyframe: 25% time from 50% time, i.e. 5.0*0.25=1.25 seconds [uiview Addkeyframewithrelativestarttime:0.5 relativeduration:0.25 animations:^{_imageview.center = Location2;}]; //third keyframe: 25% time from 75% time, i.e. 5.0*0.25=1.25 seconds [uiview Addkeyframewithrelativestarttime:0.75 relativeduration:0.25 animations:^{_imageview.center = Location3;}];} completion:^ (bool finished) {nslog ( @ "Animation end.");           
The options for key-frame animations are a few options:
/* 动画模式选择,选择一个 */UIViewKeyframeAnimationOptionCalculationModeLinear/*< 连续运算模式 */UIViewKeyframeAnimationOptionCalculationModeDiscreter/*< 离散运算模式 */UIViewKeyframeAnimationOptionCalculationModePacedr/*< 均匀执行运算模式 */UIViewKeyframeAnimationOptionCalculationModeCubicr/*< 平滑运算模式 */UIViewKeyframeAnimationOptionCalculationModeCubicPacedr/*< 平滑均匀运算模式 */

UIView animations now support only property keyframe animations, and path keyframe animations are not supported

3. Transition animation is mainly implemented by:
+ (void)transitionWithView:(UIView *)view                   duration:(NSTimeInterval)duration                    options:(UIViewAnimationOptions)options                 animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
Instance:
 #pragma mark Transitions Animation-( void) transitionanimation: (bool) isNext{uiviewanimationoptions option; if (isnext) {option = uiviewanimationoptioncurvelinear | Span class= "hljs-built_in" >uiviewanimationoptiontransitionflipfromright; } else {option = uiviewanimationoptioncurvelinear | uiviewanimationoptiontransitionflipfromleft; } [uiview transitionwithview:_imageview duration:1.0 options : option animations:^{_imageview.image = [self Getimage:isnext];} completion:nil";}               
Transition animation options have a few more options:
/* Transition Type */Uiviewanimationoptiontransitionnone/*< no transition animation effect *//*< flip effect from left */ uiviewanimationoptiontransitionflipfromright/*< Flip effect from right */uiviewanimationoptiontransitioncurlup/*< Animated transition effect */  uiviewanimationoptiontransitioncurldown /*< animated transitions to the front page */   /*< Dissolve Vanishing effect */  uiviewanimationoptiontransitionflipfromtop/*< flip effect from above */uiviewanimationoptiontransitionflipfrombottom/*< Flip effect from Bottom */              
  • Transition animations packaged with UIView animations are less effective and cannot be used directly from the private API
  • Transitions between two views can be used
    ```
    • (void) Transitionfromview: (UIView ) Fromview
      Toview: (UIView
      ) Toview
      Duration: (nstimeinterval) duration
      Options: (uiviewanimationoptions) options
      Completion: (void (^) (BOOL finished)) completion;
      ```
    • By default, the transferred view is removed from the parent view and re-added after it is transferred

iOS Learning note 10-uiview animations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.