iOS开发iOS Developer小知识点

iOS Animation: 玩转贝塞尔曲线之KYAnimate

2017-08-17  本文已影响205人  LeeLom

读书笔记

先看效果

2017-08-17 18_31_25.gif
源码链接:https://github.com/LeeLom/AnimationSample

这节内容主要是利用贝塞尔曲线来模拟一个球产生形变的过程。利用四条曲线来拼接出完整的球体。


图1.png

所以,我们画出这个球体的步骤,实际上也就是确定多边形Ac1c2Bc3c4Cc5c6Dc7c8的过程,也即是确定各个点坐标的过程。我们得到这个多边形后,利用贝塞尔曲线去逼近,既可以得到我们所需要的形变的图像。

书中给出的确定多边形的方法如下:

    CGFloat offset = _outsideRect.size.width / 3.6;
    CGFloat movedDistance = (_outsideRect.size.width * 1 / 6) * fabs(_progress - 0.5) * 2;

其余点类似,这里不做过多解释。如果不太理解,其实可以讲progress分别设置成为不同的值进行查看即可。

以上,就是书中本节的内容,最本质的就是不断的根据滑动条的值去更新正方形的位置,从而调整外接多边形的形状,进而得到模拟出的贝塞尔曲线。


补充知识点

关于这节内容,在代码实现的过程中,发现了不少知识盲区,网上查找了不少资料进行了一个整理:

关于initWithFrameinitWithCoder的区别

PS2. Stack Overflow上看到一个比较有意思的问题,跟Runloop结合的。
ViewController.m

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    View1 *view1 = [[View1 alloc] init];
    [self.view addSubview:view1];
}
@end

View1.m

@implementation View1

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"initWithFrame");
    }
    return self;
}

- (id)init {
    self = [super init];
    if (self) {
        NSLog(@"init");
    }
    return self;
}

@end

控制台的输出结果如下:

2013-10-17 12:33:46.209 test1[8422:60b] initWithFrame
2013-10-17 12:33:46.211 test1[8422:60b] init

分析一下控制台出现的原因:

  - [view1(View1) init] 

      - [view1(UIView) init] (called by [super init] inside View1)

        - [view1(View1) initWithFrame:CGRectZero] (called inside [view(UIView) init] )

           - [view1(UIView) initWithFrame:CGRectZero] (called by [super initWithFrame] inside View1) 
              - ...

           - NSLog(@"initWithFrame"); (prints "test1[8422:60b] initWithFrame")

      - NSLog(@"init"); (called inside [view1(View1) init] ; prints "test1[8422:60b] init")

关于UIViewCALayer的区别

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

关于绘图的一些函数

/**
 * 示例:stroke模式绘制线
 */
override func draw(rect: CGRext) {
      //获取上下文
      let context = UIGraphicsGetCurrentContext();
      //设置stroke模式的颜色使用CGColor(这里涉及颜色和颜色空间的概念)
      let strokeColor = UIColor.blueColor();
      CGContextSetStrokeColorWIthColor(context, strokeColor.CGColor);
      //设置线的宽度
      CGContextSetLineWidth(context, 5.0);
      //设置连接处样式
      CGContextSetLineJoin(context, CGLineJoin.Round);
      //起始点
      CGContextMoveToPoint(context, 10.0, 10.0);
      //在两个点之间画一条线,所以当前的结束点成为了下一个的起始点
      CGContextAddLineToPoint(context, 10.0, 80.0);
      //继续在两个点之间画一条线
      CGContextAddLineToPoint(context, 80.0, 80.0);
      //设置为封闭路径,将收尾连接起来
      CGContextClosePath(context);
      //绘制当前路径
      CGContextStrokePath(context);
}
/**
 * 示例:fill模式绘制线
 */
override func draw(rect: CGRext) {
      //获取上下文
      let context = UIGraphicsGetCurrentContext();
      //设置stroke模式的颜色使用CGColor(这里涉及颜色和颜色空间的概念)
      let fillColor = UIColor.blueColor();
      CGContextSetFillColorWIthColor(context, fillColor.CGColor);
      //设置线的宽度
      CGContextSetLineWidth(context, 5.0);
      //设置连接处样式
      CGContextSetLineJoin(context, CGLineJoin.Round);
      //起始点
      CGContextMoveToPoint(context, 10.0, 10.0);
      //在两个点之间画一条线,所以当前的结束点成为了下一个的起始点
      CGContextAddLineToPoint(context, 10.0, 80.0);
      //继续在两个点之间画一条线
      CGContextAddLineToPoint(context, 80.0, 80.0);
      // 设置为封闭路径,将收尾连接起来
      // CGContextClosePath(context);
      //绘制当前路径
      CGContextFillPath(context);
}
上一篇下一篇

猜你喜欢

热点阅读