iOS-UIView动画

iOS-UIView动画


今天的主题是UIView的动画。

在iOS中UIView的动画是基于CALayer动画封装。

动画就是静态的图片通过一定频率显示,给人们动画的效果。

UIView动画有基于类方法的实现和基于Block方法块的实现。

一.UIView基于类方法的实现的使用

类方法列表:

@interface UIView(UIViewAnimation)

+ (void)beginAnimations:(NSString *)animationID context:(void *)context;  // additional context info passed to will start/did stop selectors. begin/commit can be nested
+ (void)commitAnimations;                                                 // starts up any animations when the top level animation is commited

// no getters. if called outside animation block, these setters have no effect.
+ (void)setAnimationDelegate:(id)delegate;                          // default = nil
+ (void)setAnimationWillStartSelector:(SEL)selector;                // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(SEL)selector;                  // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
+ (void)setAnimationDuration:(NSTimeInterval)duration;              // default = 0.2
+ (void)setAnimationDelay:(NSTimeInterval)delay;                    // default = 0.0
+ (void)setAnimationStartDate:(NSDate *)startDate;                  // default = now ([NSDate date])
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;              // default = UIViewAnimationCurveEaseInOut
+ (void)setAnimationRepeatCount:(float)repeatCount;                 // default = 0.0.  May be fractional
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;    // default = NO. used if repeat count is non-zero
+ (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).

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;  // current limitation - only one per begin/commit block

+ (void)setAnimationsEnabled:(BOOL)enabled;                         // ignore any attribute changes while set.
+ (BOOL)areAnimationsEnabled;
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);

@end

1. + (void)beginAnimations:(NSString *)animationID context:(void *)context; 

该方法是动画的起点,它总是和commitAnimation成对出现。

2.+ (void)commitAnimations

提交动画,

其他的主要是用来设置动画的代理,执行时间,延迟执行动画,是否自动的重复,重复的次数等。下面是它们的使用。

-(void)classMethodAnimation{
    [UIView beginAnimations:@"animation" context:nil];
    //设置动画重复次数
    [UIView setAnimationRepeatCount:10.];
    //开始动画改变它的位置从originPoint(0,0)变味originPoint(100,100)
    anim.frame = CGRectMake(100, 100, 50, 50);
    
    //设置动画曲线,也就是动画效果
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //设置动画延迟
    [UIView setAnimationDelay:4];
    //设置动画代理
    [UIView setAnimationDelegate:self];
    //设置动画停止时的时间
    [UIView setAnimationDidStopSelector:@selector(animationStop)];
    //设置动画执行时间
    [UIView setAnimationDuration:10];
    //设置动画是否自动反转
    [UIView setAnimationRepeatAutoreverses:YES];
    
    //设置动画开始时调用的方法
    [UIView setAnimationWillStartSelector:@selector(startAnimation)];
    
    [UIView commitAnimations];

}

控制台运行输出结果:(主要是动画代理方法的调用,动画效果见demo)

2015-04-06 08:28:14.230 ViewAnimation[1299:49180] 动画停止了-[ViewController startAnimation]

2015-04-06 08:28:16.227 ViewAnimation[1299:49180] 动画停止了-[ViewController animationStop]


二.UIView动画block方法

@interface UIView(UIViewAnimationWithBlocks)

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

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(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

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

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview


+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

@end


block的动画效果就是把你想要改变的东西包含到block动画块中。


-(void)blockAnimation{
    [UIView animateWithDuration:5 animations:^{
        anim.frame = CGRectMake(100, 100, 50, 50);
    }];
    
    [UIView animateWithDuration:5 animations:^{
        anim.frame = CGRectMake(200, 200, 50, 50);
        anim.backgroundColor = [UIColor grayColor];
    } completion:^(BOOL finished) {
        NSLog(@"block animation complet operation");
    }];
}


运行控制态输出效果:

2015-04-06 08:33:49.944 ViewAnimation[1359:51632] block animation complet operation

两个动画小结:

通过block把动画内容包含到block中实现动画,使用beginAnimation和commitsAnimation函数对包含动画,都是通过参数设置动画的执行属性。



你可能感兴趣的:(iOS-UI动画,iOS-UIKit,iOS)