zoukankan      html  css  js  c++  java
  • iOS 动画组

      其实早在一个多月以前就已经实现了动作组播放的功能,不过当时感觉好像没有什么难度并没有放在心上,今天突然要用到动画组,发现已经忘记了,所以又将原来的代码翻出来看了下。最后决定还是写下来,以备不时之需。动画组播放很简单,使用的是CAAnimationGroup这个类,将不同的动画添加到里面进行播放。通常可以添加的动画有:1、keyPath,它用来绘制路径,直线或者曲线,从理论上讲任意的线都可以绘制;2、旋转动画,它可以绕x、y、z轴进行旋转;3、缩放动画。

      废话不多说,直接上代码。

      

     1 #define pathAnimation @"position"
     2 #define transformX @"transform.rotation.x"
     3 #define transformY @"transform.rotation.y"
     4 #define transformZ @"transform.rotation.z"
     5 #define scaleAnimation @"position scale"
     6 #import <Foundation/Foundation.h>
     7 #import <QuartzCore/QuartzCore.h>
     8 #import <UIKit/UIKit.h>
     9 
    10 //NSString *const pathAnimation = @"position";          //运动轨迹
    11 //NSString *const transformX = @"transform.rotation.x"; //绕x轴旋转
    12 //NSString *const transformY = @"transform.rotation.y"; //绕y轴旋转
    13 //NSString *const transformZ = @"transform.rotation.z"; //绕z轴旋转
    14 //NSString *const scaleAnimation = @"position scale";   //缩放
    15 
    16 typedef enum AnimationType{
    17     KeyPathAnimation,      //运动轨迹为抛物线
    18     RotationAnimationX,    //绕x轴旋转
    19     RotationAnimationY,    //绕y轴旋转
    20     RotationAnimationZ,    //绕z轴旋转
    21     ScaleAnimation         //缩放
    22 }AnimationType;
    23 
    24 @protocol LMFThowingLineToolDelegate <NSObject>
    25 
    26 - (void)animationDidFinish;
    27 
    28 @end
    29 
    30 @interface LMFThowingLineTool : NSObject
    31 @property (nonatomic, strong) NSMutableArray *typeArray;//用来存储动画类型
    32 @property (nonatomic, strong) NSArray *pointArray;
    33 @property (nonatomic, strong) CAKeyframeAnimation *keyFrameAnimation;
    34 @property (nonatomic, strong) CABasicAnimation *basicAnimation;
    35 @property (nonatomic, strong) UIView *showView;
    36 @property (nonatomic, assign) CGFloat duration;
    37 @property (nonatomic, assign) id<LMFThowingLineToolDelegate> delegate;
    38 - (void)setPointArray:(NSArray *)pointArray;
    39 - (void)setDuration:(CGFloat)duration;
    40 - (void)setKeyFrameAnimation;
    41 - (void)setBasicAnimationWith:(AnimationType)animationType;
    42 - (void)thowingView:(UIView *)view;
    43 - (void)setTransform:(CGFloat)angle and:(AnimationType)animationType;
    44 @end

      实现类:

     

      1 #import "LMFThowingLineTool.h"
      2 #import "LMFPoint.h"
      3 
      4 @interface LMFThowingLineTool ()
      5 
      6 @end
      7 
      8 @implementation LMFThowingLineTool
      9 
     10 - (void)dealloc
     11 {
     12     self.basicAnimation = nil;
     13     self.keyFrameAnimation = nil;
     14     NSLog(@"%@", self.basicAnimation);
     15 }
     16 
     17 - (instancetype)init
     18 {
     19     if (self = [super init])
     20     {
     21         self.typeArray = [NSMutableArray array];
     22     }
     23     return self;
     24 }
     25 - (void)setPointArray:(NSArray *)pointArray
     26 {
     27     _pointArray = pointArray;
     28 }
     29 - (void)setDuration:(CGFloat)duration
     30 {
     31     _duration = duration;
     32 }
     33 - (void)setKeyFrameAnimation
     34 {
     35     if ([self.pointArray count] == 0)
     36     {
     37         return;
     38     }
     39     CGMutablePathRef path = CGPathCreateMutable();
     40     
     41     if ([self.pointArray count] == 3)
     42     {
     43         LMFPoint * startPoint = (LMFPoint *)self.pointArray[0];
     44         LMFPoint * centerPoint = (LMFPoint *)self.pointArray[1];
     45         LMFPoint * endPoint = (LMFPoint *)self.pointArray[2];
     46         CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
     47         //这里用的直线的路径,也可以设置抛物线,获取曲线,可进行自定义
     48         CGPathAddQuadCurveToPoint(path, NULL, centerPoint.x, centerPoint.y, endPoint.x, endPoint.y);
     49     }
     50     else if ([self.pointArray count] > 3)
     51     {
     52         return;
     53     }
     54     self.keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:pathAnimation];
     55     self.keyFrameAnimation.path = path;
     56     CFRelease(path);
     57     [self.typeArray addObject:self.keyFrameAnimation];
     58 }
     59 - (void)setTransform:(CGFloat)angle and:(AnimationType)animationType
     60 {
     61     
     62 }
     63 - (void)setBasicAnimationWith:(AnimationType)animationType
     64 {
     65     self.basicAnimation = [[CABasicAnimation alloc] init];
     66     self.basicAnimation.autoreverses = YES;
     67     self.basicAnimation.repeatCount = MAXFLOAT;
     68     self.basicAnimation.duration = self.duration;
     69     switch (animationType) {
     70         case RotationAnimationX:
     71             self.basicAnimation.keyPath = transformX;
     72             break;
     73         case RotationAnimationY:
     74             self.basicAnimation.keyPath = transformY;
     75             break;
     76         case RotationAnimationZ:
     77             self.basicAnimation.keyPath = transformZ;
     78             break;
     79         case ScaleAnimation:
     80             self.basicAnimation.keyPath = scaleAnimation;
     81             self.basicAnimation.toValue = [NSNumber numberWithFloat:(CGFloat)((arc4random() % 4) + 4) / 10.0];
     82             [self.typeArray addObject:self.basicAnimation];
     83             break;
     84         default:
     85             break;
     86     }
     87     self.basicAnimation.fromValue = [NSNumber numberWithFloat:0.0];
     88     int intNumber = arc4random()%3+1;
     89     self.basicAnimation.toValue = [NSNumber numberWithFloat:intNumber*M_PI];
     90     [self.typeArray addObject:self.basicAnimation];
     91 }
     92 
     93 #pragma mark --开始执行动画
     94 - (void)thowingView:(UIView *)view
     95 {
     96     self.showView = view;
     97     CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
     98 //    groupAnimation.delegate = self;
     99     groupAnimation.repeatCount = 1;
    100     groupAnimation.duration = self.duration;
    101     groupAnimation.removedOnCompletion = NO;
    102     groupAnimation.animations = self.typeArray;
    103     [view.layer addAnimation:groupAnimation forKey:@"position scale"];
    104 }
    105 
    106 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    107 {
    108     [self.showView removeFromSuperview];
    109     self.showView = nil;
    110 }
    111 
    112 @end

      额外需要的类:

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface LMFPoint : NSObject
    @property (nonatomic, assign) CGFloat x;
    @property (nonatomic, assign) CGFloat y;
    - (instancetype)initWith:(CGFloat)x and:(CGFloat)y;
    @end
    
    #import "LMFPoint.h"
    
    @implementation LMFPoint
    - (instancetype)initWith:(CGFloat)x and:(CGFloat)y
    {
        if (self = [super init])
        {
            self.x = x;
            self.y = y;
        }
        return self;
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"x:%f   y:%f", self.x, self.y];
    }
    
    @end
  • 相关阅读:
    利用Python对文件进行批量重命名
    应用程序处于中断模式-设置方法
    idea http请求的插件(测试接口方便)
    ajax 分页(jquery分页插件pagination) 小例3
    JProfile 9.2 linux安装及windows客户端远程监控
    serializeObject 的应用
    linux下 mysql数据库的备份和还原
    ajax 分页(jquery分页插件pagination) 小例2
    bootstrapTable 应用小例(收索)
    表格 td中,取checkbox后几位值
  • 原文地址:https://www.cnblogs.com/lmfboke/p/5993664.html
Copyright © 2011-2022 走看看