IOS动画学习小记(1)-图形

画各种不动图形:(CALayer CAShapeLayer)

CAShapeLayerCALayer 的子类,但是比CALayer 更灵活,可以画出各种图形。

CALayer本身并不包含在UIKit中,它不能响应事件。

CAShapeLayer 有一个属性 path 配合上 UIBezierPath 这个类可以画出各种图形。

UIBezierPath:
//创建方法
//画矩形
+ (instancetype)bezierPathWithRect:(CGRect)rect;

//根据矩形画内切曲线,通常用于画圆或则椭圆
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

//画带圆角的矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

//可以指定矩形的某角为圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

//画圆弧(center: 中心点坐标 radius: 半径 startAngle: 弧线开始角度值 endAngle: 弧线结束角度值 clockwise: 是否顺时针画弧线)
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;

//使用CGPath生成贝塞尔曲线
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

//画图方法Path construction
//移动起点
- (void)moveToPoint:(CGPoint)point;

//画直线
- (void)addLineToPoint:(CGPoint)point;

//画三次贝塞尔曲线,两个控制点.
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

//画二次贝塞尔曲线,一个控制点.
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

//画圆弧
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);

//闭合线
- (void)closePath;

//用于在- (void)drawRect:(CGRect)rect中调用
//填充
- (void)fill;
//连线
- (void)stroke;

CALayer CAShapeLayer CAKeyframeAnimation等都是跨平台QuartzCore框架中的。不能直接使用UIKit中的对象。

tip:设置画的路径path
  1. 如果使用UIBezierPath

     //他们的path都是CGPath, 如果使用UIBezierPath,则需要转化为.CGPath。
     
     UIBezierPath *path = [UIBezierPath bezierPath];
     //首先设置起始点
     [path moveToPoint:startPoint];
     
     //添加曲线
     [path addCurveToPoint:startPoint
               controlPoint1:controlPoint1
               controlPoint2:controlPoint2];
     
     //线宽
     path.lineWidth = 1.5;
     
     /*设置线条拐角帽的样式(
     kCGLineCapButt,  默认
     kCGLineCapRound, 轻微圆角
     kCGLineCapSquare 正方形)
      */
     path.lineCapStyle = kCGLineCapRound;
     
     /*设置两条线连结点的样式(    
     kCGLineJoinMiter, 斜接
     kCGLineJoinRound, 圆滑衔接
     kCGLineJoinBevel  斜角连接)
     */
     path.lineJoinStyle = kCGLineJoinBevel;
     
     CALayer layer = [[CALayer alloc] init];
     layer.path    = path.CGPath; //转化为CGPath
     layer.fillColor   = [UIColor clearColor].CGColor;
     layer.strokeColor = [UIColor blackColor].CGColor;
     [self.view.layer addSublayer:layer];
     
     /**
     << 在drawRect中调用使用fill,和stroke
     // 设置填充颜色
     UIColor *fillColor = [UIColor greenColor];
     [fillColor set];
     [path fill];
     
     // 设置画笔颜色
     UIColor *strokeColor = [UIColor blueColor];
     [strokeColor set];
     
     // 连接个点
     [path stroke];
     >>
     **/
    
  2. 直接使用CGPathRef

     //1.创建关键帧动画并设置动画属性
     CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
     //2.设置路径
     //绘制贝塞尔曲线
     CGPathRef path=CGPathCreateMutable();
     CGPathMoveToPoint(path, NULL, _layer.position.x, _layer.position.y);//移动到起始点
     CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, 55, 400);//绘制二次贝塞尔曲线
    
     keyframeAnimation.path=path;//设置path属性
     CGPathRelease(path);//释放路径对象
     //设置其他属性
     keyframeAnimation.duration=8.0;
     keyframeAnimation.beginTime=CACurrentMediaTime()+5;//设置延迟2秒执行
    
     //3.添加动画到图层,添加动画后就会执行动画
     [_layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
    

推荐阅读更多精彩内容