0

嘿,

当我在目标 c 中触摸它时如何制作改变颜色的东西,我希望整个屏幕都是可触摸的,并想用手指在上面画东西。我触摸的所有地方都应该改变颜色。最好的方法是什么?我已经实现了touchesMoved来获取触摸的坐标。

UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];

任何示例代码都会很好,谢谢。

先感谢您,

我有以下代码,但它不会打印我触摸的任何内容

-(void)setPaths:(NSMutableArray *)paths
{
    self.paths =[[NSMutableArray alloc]init];
}

-(void)setAPath:(UIBezierPath *)aPath
{
  self.aPath=[UIBezierPath bezierPath];
}

-(void) drawRect:(CGRect)rect{
    [super drawRect:rect];
    [[UIColor blackColor] set];
    for (UIBezierPath *path in paths) {
        [path stroke];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.paths addObject:self.aPath];

    //self.aPath=[UIBezierPath bezierPath];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch * touch = [touches anyObject];
    CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
    [self.aPath addLineToPoint:[touch locationInView:self]];
    // [self.aPath addLineToPoint:[touch locationInView:touch]];

    NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
}

@end
4

2 回答 2

2

当您开始触摸时,创建一个 UIBezierPath:

self.myPath = [UIBezierPath bezierPath];

然后,每次触摸移动时,为路径添加一个点:

 [self.myPath addLineToPoint:[touch locationInView:self]];

触摸结束后,在 drawRect 中绘制路径:

-(void) drawRect:(CGRect)rect{    
[super drawRect:rect];  
    [[UIColor blueColor] set];
    for (UIBezierPath *path in paths) {
          [path stroke];
    }
}

在这里找到所有文档:

UIBezierPath 类参考

于 2013-10-04T09:44:01.957 回答
0

也许你会改变你的drawRect:方法。插入这两行:

[[UIColor blackColor] setStroke];
[path stroke];

你应该[self setNeedsDisplay];在里面添加一行之后添加touchesMoved:

我刚刚通过了以下教程,它应该可以解决您的问题。它还解释了如何改进代码,以便在绘制较长的行时不会变得太懒:http: //mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/

于 2013-10-04T13:21:05.557 回答