视图在画出得路径上移动

本文介绍如何使用QuartzCore框架进行绘图,并通过Core Animation实现视图动画效果。文章详细展示了如何创建自定义视图DrawView,利用Core Graphics绘制图形,并应用路径动画来移动视图中的子元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要导入 QuartzCore.framework

代码

#import "DrawView.h"

#import <QuartzCore/QuartzCore.h>


@implementation DrawView


- (instancetype)init

{

    self = [super init];

    if (self) {

        subview = [[UIView alloc] init];

        subview.frame = CGRectMake(0, 0, 10, 10);

        subview.backgroundColor = [UIColor redColor];

        [self addSubview:subview];

    }

    return self;

}

- (void)drawRect:(CGRect)rect {

    // 绘图上下文(画布)

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 画笔颜色

    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);

    // 设置填充颜色(矩形、圆等)

    //CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);

    

    // 功能同上

    //[[UIColor purpleColor] setStroke]; // 线

    //[[UIColor greenColor]setFill]; // 填充颜色

    

    // 线条宽度

    CGContextSetLineWidth(ctx, 2.0);

    // 画笔移动到某点(开始的位置)

    CGContextMoveToPoint(ctx, 0, 0);

    // 画线

    //CGContextAddLineToPoint(ctx, self.frame.size.width, self.frame.size.height);

    // 画多条线

    //CGContextAddLines(<#CGContextRef c#>, <#const CGPoint *points#>, <#size_t count#>)

    // 画矩形

    CGContextAddRect(ctx, CGRectMake(10, 10, 80, 80));

    // 贝塞尔曲线

    //CGContextAddCurveToPoint(ctx, 20, 80, 200, 150, 50, 100);

    /*

     kCGPathFill, // 填充

     kCGPathEOFill,

     kCGPathStroke, // 线

     kCGPathFillStroke, // 线和填充

     kCGPathEOFillStroke

     */

    CGContextDrawPath(ctx, kCGPathStroke); // 开始画

    

    // 绘制路径

    CGMutablePathRef path = CGPathCreateMutable();

    //CGPathAddCurveToPoint(<#CGMutablePathRef path#>, <#const CGAffineTransform *m#>, <#CGFloat cp1x#>, <#CGFloat cp1y#>, <#CGFloat cp2x#>, <#CGFloat cp2y#>, <#CGFloat x#>, <#CGFloat y#>)

    CGPathAddRect(path, NULL, CGRectMake(10, 10, 80, 80));

    

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    animation.duration = 3.0;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];

    animation.repeatCount = 20.0;

    animation.path = path;

    [subview.layer addAnimation:animation forKey:@"suibianxie"];

    

//    UIImage *image = [UIImage imageNamed:@"abc"];

//    [image drawInRect:<#(CGRect)#>]

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值