美文网首页iOS 开发 学习iOS语法技巧iOS Developer
UITouch绘图,不使用贝塞尔曲线

UITouch绘图,不使用贝塞尔曲线

作者: 芝麻绿豆 | 来源:发表于2016-02-16 21:47 被阅读387次

首先简单介绍一下UITouch吧!

属相与方法:

  • window:触摸产生时所处的窗口。由于窗口可能发生变化,当前所在的窗口不一定是最开始的窗口。
  • view:触摸产生时所处的视图。由于视图可能发生变化,当前视图也不一定时最初的视图。
  • tapCount:轻击(Tap)操作和鼠标的单击操作类似,tapCount表示短时间内轻击屏幕的次数。因此可以根据tapCount判断单击、双击或更多的轻击。
  • timestamp: 时间戳记录了触摸事件产生或变化时的时间。单位是秒。
  • phase:触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。通过phase可以查看当前触摸事件在一个周期中所处的状态;
    • phase是UITouchPhase类型的,包括:
typedef NS_ENUM(NSInteger, UITouchPhase) {
    UITouchPhaseBegan,  (开始)           // whenever a finger touches the surface.
    UITouchPhaseMoved,  (移动)           // whenever a finger moves on the surface.
    UITouchPhaseStationary,  (无移动)      // whenever a finger is touching the surface but hasn't moved since the previous event.
    UITouchPhaseEnded,     (结束)      // whenever a finger leaves the surface.
    UITouchPhaseCancelled, (取消)      // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
};
  • (CGPoint)locationInView:(UIView *)view:函数返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的。调用时传入的view参数为空的话,返回的时触摸点在整个窗口的位置。

  • (CGPoint)previousLocationInView:(UIView *)view:该方法记录了前一个坐标值,函数返回也是一个CGPoint类型的值, 表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的。调用时传入的view参数为空的话,返回的时触摸点在整个窗口的位置。

如何绘图:

  • 首先在- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法里添加曲线的起点;
    UITouch *touch = [touches anyObject];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, [touch locationInView:self].x, [touch locationInView:self].y);
    [self.totalPathPoints addObject:(__bridge_transfer id)path];

可以不使用CGMutablePathRef,可以用数组然后再将self.totalPathPoints这个数组存在一个一个的小数组;然后遍历也可以,方法有很多种;

__bridge_transfer 让非Objective-C对象转换为Objective-C对象
  • 然后在- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event与- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event两个方法里添加移动的路径;
 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self drawLineWithTouches:touches];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self drawLineWithTouches:touches];
}
-(void)drawLineWithTouches:(NSSet *)touches {
    UITouch *touch = [touches anyObject];
    CGMutablePathRef path = (__bridge_retained CGMutablePathRef)self.totalPathPoints.lastObject;
    CGPathAddLineToPoint(path, nil, [touch locationInView:self].x, [touch locationInView:self].y);
    [self setNeedsDisplay];
}
注意:用[self setNeedsDisplay]调用- (void)drawRect:(CGRect)rect才有效;不可以手动直接调用。
-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (id path in self.totalPathPoints) {
        CGMutablePathRef ref = (__bridge_retained CGMutablePathRef)path;
        CGContextAddPath(context, ref);
    }
    // 线段的宽度
    CGContextSetLineWidth(context, 5);
    [[UIColor blackColor] setStroke];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextStrokePath(context);
}
__bridge_retained 是将Objective-C对象转换为非Objective-C对象;Objective-C 和 Core Foundation 对象之间可以轻松的转换
效果图

相关文章

  • UITouch绘图,不使用贝塞尔曲线

    首先简单介绍一下UITouch吧! 属相与方法: window:触摸产生时所处的窗口。由于窗口可能发生变化,当前所...

  • iOS 绘图

    1.Quart2D绘图 绘图(CGcontext) 2.贝塞尔曲线 3. 贝塞尔曲线 与 CGcontext 结...

  • 初试 贝塞尔曲线

    Android 绘图贝塞尔曲线简单使用 在Android中某些自定义View的时候需要绘制某些曲线,这时候贝塞尔曲...

  • 贝塞尔曲线

    贝塞尔曲线 用途 贝塞尔曲线奠定了计算机绘图的基础 所有的弧线都是由贝塞尔曲线的运用 原理 依次连接开始数据点 控...

  • Sketch·每日一临摹·2016/01

    160116·BayMax 钢笔工具注意事项 Straight:不使用贝塞尔曲线。Mirrored:使用贝塞尔曲线...

  • 曲线之美 --贝塞尔曲线

    今天在学习绘图类:UIBezierPath时,发现一个方法用于绘制贝塞尔曲线 //绘制三次贝塞尔曲线 - (voi...

  • UIView的常用属性和方法

    1、绘制和更新视图的方法 -drawRect:(CGRect)rect 可使用贝塞尔曲线来绘图 -setNeed...

  • 画线的问题

    二 、绘图贝塞尔曲线&UIBezierPath//创建画笔_bezierPen = [UIBezierPath b...

  • 购物车动画

    使用贝塞尔曲线实现购物车抛物线动画关键步骤 使用path 构建贝塞尔曲线 使用PathMeasure计算曲线上每个...

  • Android绘图之贝塞尔曲线简介(15)

    Android 绘图学习 1贝塞尔曲线简介 贝塞尔曲线被发明用来进行汽车的主体设计(流线型),具体就是通过起始点和...

网友评论

    本文标题:UITouch绘图,不使用贝塞尔曲线

    本文链接:https://www.haomeiwen.com/subject/rczpkttx.html