ios 动画浅析

来源:互联网 发布:如何测试sql注入 编辑:程序博客网 时间:2024/05/16 12:38

个人理解iOS 动画大致分为两类:1.UI层的实现 UIViewAnimation,2.媒体层的

CATransition 继承于基类CAAnimation ,直接对view.layer操作实现动画。

常见的动画有:改变大小、位置、透明度、角度旋转(

CGAffineTransform

)、过渡动画(两个视图切换)


一、UI层的动画有3个实现的接口

UIVIewAnimation

@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.....................@end

实例代码如下

//UIView开始动画,第一个参数是动画的标识,第二个参数附加的应用程序信息用来传递给动画代理消息    [UIView beginAnimations:@"View Flip" context:nil];    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:3];        //动画持续时间    [UIView setAnimationDuration:1.25];        //设置动画的回调函数,设置后可以使用回调方法    [UIView setAnimationDelegate:self];        //设置动画曲线,控制动画节奏    [UIView  setAnimationCurve: UIViewAnimationCurveEaseInOut];        //设置动画过渡方式,并指出动画发生的位置 有向上翻页 向下翻页 左翻转 右翻转    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view  cache:YES];    //提交UIView动画    [UIView commitAnimations];


UIViewAnimationWithBlocks

@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

UIViewKeyframeAnimations

@interface UIView (UIViewKeyframeAnimations)+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); // start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation@end


二、CATransition实现

CATransition *animation = [CATransition animation];    animation.delegate = self;    animation.duration = kDuration;    animation.timingFunction = UIViewAnimationCurveEaseInOut;        switch (tag) {        case 101:            animation.type = kCATransitionFade;  //淡化效果            break;        case 102:            animation.type = kCATransitionPush; //推挤            break;        case 103:            animation.type = kCATransitionReveal;//揭开            break;        case 104:            animation.type = kCATransitionMoveIn;//覆盖            break;        case 201:            animation.type = @"cube";//立方体效果            break;        case 202:            animation.type = @"suckEffect";//吸收效果            break;        case 203:            animation.type = @"oglFlip";//翻转            break;        case 204:            animation.type = @"rippleEffect";//水波效果            break;        case 205:            animation.type = @"pageCurl";//翻页            break;        case 206:            animation.type = @"pageUnCurl";//反翻页            break;        case 207:            animation.type = @"cameraIrisHollowOpen";//镜头开            break;        case 208:            animation.type = @"cameraIrisHollowClose";//镜头关            break;        default:            break;    }        switch (self.typeID) {        case 0:            animation.subtype = kCATransitionFromLeft;            break;        case 1:            animation.subtype = kCATransitionFromBottom;            break;        case 2:            animation.subtype = kCATransitionFromRight;            break;        case 3:            animation.subtype = kCATransitionFromTop;            break;        default:            break;    }    self.typeID += 1;    if (self.typeID > 3) {        self.typeID = 0;    }        NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];    NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];    [self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];        [[self.view layer] addAnimation:animation forKey:@"animation"];






0 0
原创粉丝点击