我有一个名为Slate的平板电脑,我可以处理x,y点。我可以将这些x,y点传递给我的“drawShape”函数,相当于一个touchesMoved函数。我有一个为此目的而设计的自定义UIView类,具有4个主要功能。前三个是touchesbegan / move / ends,还有一个drawRect。
这是UIView子类的源代码......
//
// DrawingLayerView.m
//
//
// Created by Monica Kachlon on 12/3/17.
//
#import "DrawingLayerView.h"
#include "UIBezierPath+Interpolation.h"
@interface StrokeA : NSObject
@property (assign, nonatomic) CGPoint startPoint;
- (id)initWithStartPoint:(CGPoint)point;
@property (strong, nonatomic) NSMutableArray * points;
@end
@implementation StrokeA
- (id)initWithStartPoint:(CGPoint)point {
self = [super init];
self.startPoint = point;
self.points = [NSMutableArray array];
return self;
}
@end
@implementation DrawingLayerView
NSMutableArray * strokes;
NSMutableArray * points;
StrokeA * currentStroke;
UIBezierPath * currentPath;
UIBezierPath *path;
UIBezierPath *pathTwo;
UIBezierPath *Newpath;
CAShapeLayer * currentLayer;
- (void) noodlez
{
path = [UIBezierPath bezierPath];
currentPath = [UIBezierPath bezierPath];
pathTwo = [UIBezierPath bezierPath];
Newpath = [UIBezierPath bezierPath];
strokes = [NSMutableArray array];
points = [NSMutableArray array];
}
- (void)startTouch: (CGPoint)point
{
currentStroke = [[StrokeA alloc] initWithStartPoint:point];
[strokes addObject:currentStroke];
}
- (UIBezierPath *)createPath {
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
for (StrokeA * stroke in strokes) {
[bezierPath moveToPoint:stroke.startPoint];
for (NSValue * value in stroke.points) {
[bezierPath addLineToPoint:[value CGPointValue]];
}
}
return bezierPath;
}
- (void)endTouch: (CGPoint)point
{
[points removeAllObjects];
[self setNeedsDisplay];
}
- (void)drawShape: (CGPoint)point
{
if (!currentStroke) {
[self startTouch:point];
}
[currentStroke.points addObject:[NSValue valueWithCGPoint:point]];
pathTwo = [self createPath];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] setStroke];
[pathTwo stroke];
}
@end
这是截图。
我试着写“好吗?”只是为了说明这个问题。
答案 0 :(得分:1)
尽管代码缺少一些细节并且问题措辞不当,但屏幕截图清楚地表明路径的初始点在每种情况下都是(0,0)。将路径中的点转移到控制台,您将看到它。弄清楚那些(0,0)是如何进入的,你将解决这个问题。