2013-10-04 28 views
0

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 

回答

0

也許你會改變你的drawRect:方法。插入這兩條線:

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

,你應該添加[self setNeedsDisplay];您添加後內部touchesMoved:

我剛剛通過下面的教程一條線,它應該解決您的問題。它還解釋瞭如何改進代碼,以便在繪製更長的線時不會太懶: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/

+0

不工作:S,去看教程 –

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]; 
    } 
} 

在這裏找到所有的DOC:

UIBezierPath Class Reference

+1

'[path stroke]'call將不得不在'drawRect:'內它? – trojanfoe

+0

謝謝,我忘了指出 –

+0

什麼是路徑變量,在? –