所以我对objective-c有一点经验,但不是很多。我正在尝试通过 TouchEvents 渲染 UIBezierPath (换句话说,用手指画一条线)。此外,我想将所有 bezierPaths 保存到 NSMutableArray(路径)中,以便以后可以移动或修改它们。这是最简单的应用程序。我的标题如下所示:
@interface ViewController : UIViewController{
UIBezierPath* path;
NSMutableArray* paths;
int curThickness;
}
@property (strong, nonatomic) IBOutlet UIView *scene;
@end
场景是占据整个屏幕的视图,也是我试图绘制路径的地方。
相关实现如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
NSLog(@"Starting Path");
path = [UIBezierPath bezierPath];
path.lineWidth = 15.0f;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
path.flatness = 2.0;
path.miterLimit = 4.0;
[path moveToPoint:[touch locationInView:scene]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
[path addLineToPoint:[touch locationInView:scene]];
[scene setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
NSLog(@"I'm supposed to be drawing something...");
[[UIColor redColor] set];
[path stroke];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"No more touchy feelies");
UITouch *touch = [touches anyObject];
[path addLineToPoint:[touch locationInView:scene]];
[paths addObject:path];
[scene setNeedsDisplay];
NSLog(@"number of paths: %i.", paths.count);
}
当我在模拟器上运行它时,所有事件都已注册,从不调用 DrawRect,屏幕上没有绘制任何内容(即使我在 touchesEnded 的末尾添加了 [path trace]),path 是 nonEmpty,并且 paths.count无论我追踪多少条路径,它都保持为零。
我错过了什么?一如既往,我们非常感谢您的帮助。