您的位置:首页 > 移动开发 > IOS开发

iOS 动画之购物车 贝塞尔曲线

2016-01-29 10:15 323 查看
//
//  ViewController.m
//  test_shapeLayer_01
//
//  Created by admin on 1/28/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import "ViewController.h"

#define pi 3.14159265359
#define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/
180)

@interface ViewController ()

@property(nonatomic,
strong) CAShapeLayer *shapeLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super
viewDidLoad];

    self.shapeLayer = [CAShapeLayer
layer];
//    _shapeLayer.frame = CGRectMake(0, 0, 200, 200);
    _shapeLayer.frame =
self.view.frame;
//    _shapeLayer.position = self.view.center;
    _shapeLayer.fillColor = [UIColor
clearColor].CGColor;
    
    _shapeLayer.lineWidth =
1.0f;
    _shapeLayer.strokeColor = [UIColor
cyanColor].CGColor;
    
//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    
//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    
    
//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
//                                                         radius:75
//                                                     startAngle:0
//                                                       endAngle:DEGREES_TO_RADIANS(135)
//                                                      clockwise:YES];
    
//    //绘制二次贝塞尔曲线
//    UIBezierPath *circlePath = [UIBezierPath bezierPath];
////    [circlePath moveToPoint:CGPointMake(20, 100)];
//    [circlePath moveToPoint:CGPointMake(0, 100)];
//    [circlePath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(100, 0)];
    
    
#if 0
    //绘制三次贝塞尔曲线
    UIBezierPath *circlePath = [UIBezierPath bezierPath];
    [circlePath moveToPoint:CGPointMake(20,
50)];
    [circlePath addCurveToPoint:CGPointMake(200,
50) controlPoint1:CGPointMake(110,
0) controlPoint2:CGPointMake(110,
100)];
    _shapeLayer.path = circlePath.CGPath;
#endif
    
    
    //绘制三次贝塞尔曲线
    UIBezierPath *circlePath = [UIBezierPath
bezierPath];
    [circlePath moveToPoint:CGPointMake(20,
100)];
    [circlePath addCurveToPoint:CGPointMake(350,
600) controlPoint1:CGPointMake(110,
0) controlPoint2:CGPointMake(110,
100)];
    _shapeLayer.path = circlePath.CGPath;
    
    
    _shapeLayer.strokeStart =
0;
    _shapeLayer.strokeEnd =
1.0;
    
    [self.view.layer
addSublayer:_shapeLayer];
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
    animation.duration =
3;
    animation.timingFunction = [CAMediaTimingFunction
functionWithName:@"easeInEaseOut"];
    animation.repeatCount =
1;
    animation.path = circlePath.CGPath;
    
    UIView *subView = [[UIView
alloc] initWithFrame:CGRectMake(100,
20, 10,
10)];
    
//    subView.center = self.view.center;
    
    subView.backgroundColor = [UIColor
redColor];
    [self.view
addSubview:subView];
    
//    subView.center = self.view.center;
    
    //为subView视图添加动画
    [subView.layer
addAnimation:animation forKey:@"test"];
    
    
//    //绘制曲线
//    CGContextMoveToPoint(context, 20, 100);//移动到起始位置
//    /*绘制二次贝塞尔曲线
//     c:图形上下文
//     cpx:控制点x坐标
//     cpy:控制点y坐标
//     x:结束点x坐标
//     y:结束点y坐标
//     */
//    CGContextAddQuadCurveToPoint(context, 160, 0, 300, 100);
//    
//    CGContextMoveToPoint(context, 20, 500);
//    /*绘制三次贝塞尔曲线
//     c:图形上下文
//     cp1x:第一个控制点x坐标
//     cp1y:第一个控制点y坐标
//     cp2x:第二个控制点x坐标
//     cp2y:第二个控制点y坐标
//     x:结束点x坐标
//     y:结束点y坐标
//     */
//    CGContextAddCurveToPoint(context, 80, 300, 240, 500, 300, 300);
//    
//    //设置图形上下文属性
//    [[UIColor yellowColor]setFill];
//    [[UIColor redColor]setStroke];
//    
//    
//    //绘制路径
//    CGContextDrawPath(context, kCGPathFillStroke);
    
}

- (void)didReceiveMemoryWarning {
    [super
didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: