在触摸物镜c上绘制东西

时间:2013-10-04 09:39:47

标签: objective-c

HY,

当我在物镜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

2 个答案:

答案 0 :(得分: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 Class Reference

答案 1 :(得分:0)

也许您要更改drawRect:方法。插入这两行:

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

,您应在[self setNeedsDisplay];

中添加一行后添加touchesMoved:

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