2013-02-17 131 views
3

这里的新程序员试图一步一步地做事。我试图找到一种在设备上的每个当前触摸位置周围绘制圆圈的方法。屏幕上两个手指,每个手指下一圈。如何在设备上的每个触摸位置绘制一个圆圈?

我现在有工作在代码一个触摸位置画一个圈,但一旦我躺在另一手指在屏幕上,在圆内移动到第二触摸位置,离开第一触摸位置是空的。当我添加第三个,它的动作有等

理想我想能有到屏幕上5个有效圈,每一个手指。

这是我目前的代码。

@interface TapView() 
@property (nonatomic) BOOL touched; 
@property (nonatomic) CGPoint firstTouch; 
@property (nonatomic) CGPoint secondTouch; 
@property (nonatomic) int tapCount; 
@end 

@implementation TapView 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[super touchesBegan:touches withEvent:event]; 

NSArray *twoTouch = [touches allObjects]; 
    if(touches.count == 1) 
    { 
     self.tapCount = 1; 
     UITouch *tOne = [twoTouch objectAtIndex:0]; 
     self.firstTouch = [tOne locationInView:[tOne view]]; 
     self.touched = YES; 
     [self setNeedsDisplay]; 
    } 
    if(touches.count > 1 && touches.count < 3) 
    { 
     self.tapCount = 2; 
     UITouch *tTwo = [twoTouch objectAtIndex:1]; 
     self.secondTouch = [tTwo locationInView:[tTwo view]]; 
     [self setNeedsDisplay]; 
    } 
} 
-(void)drawRect:(CGRect)rect 
{ 
     if(self.touched && self.tapCount == 1) 
     { 
      [self drawTouchCircle:self.firstTouch :self.secondTouch]; 
     } 
} 
-(void)drawTouchCircle:(CGPoint)firstTouch :(CGPoint)secondTouch 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(ctx,0.1,0.1,0.1,1.0); 
    CGContextSetLineWidth(ctx,10); 
    CGContextAddArc(ctx,self.firstTouch.x,self.firstTouch.y,30,0.0,M_PI*2,YES); 
    CGContextStrokePath(ctx); 
} 

我有我的appDelegate.m didFinishLaunchingWithOptions方法声明setMultipleTouchEnabled:YES

我试图在drawTouchCircle方法改变self.firstTouch.xself.secondTouch.x根据self.tapCount但似乎打破了整个事情,留下我在任何触摸位置无界的if语句。

我有一个非常艰难的时间,试图找到我的问题,我知道,它可能是很简单的。

回答

3

我只是写一些代码,似乎工作。我已将NSMutableArray属性circles添加到视图中,其中包含每个圆圈的UIBezierPath。 在-awakeFromNib我设置的阵列,并设置self.multipleTouchEnabled = YES - (我认为你这样做是使用您的appDelegate.m到视图的引用)。

在我请在-touchesBegan-touchesMoved方法此方法的视图。

-(void)setCircles:(NSSet*)touches 
{ 
    [_circles removeAllObjects]; //clear circles from previous touch 

    for(UITouch *t in touches) 
    { 
     CGPoint pt= [t locationInView:self]; 
     CGFloat circSize = 200; //or whatever you need 
     pt = CGPointMake(pt.x - circSize/2.0, pt.y - circSize/2.0); 
     CGRect circOutline = CGRectMake(pt.x, pt.y, circSize, circSize); 
     UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circOutline]; 
     [_circles addObject:circle]; 
    } 
    [self setNeedsDisplay]; 
} 

倒是结束是:

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [_circles removeAllObjects]; 
    [self setNeedsDisplay]; 

} 

然后我循环遍历-drawRectcircles并呼吁每一个

+0

[circle stroke]原谅我,因为我真的还是很新的这一点,但我认为我设法解决了你所说的话,直到你说通过'-drawRect'中的'circles'循环。你是否想要遍历数组中的每个对象?我会怎么做呢? – BendE 2013-02-17 02:49:45

+0

我的'-drawRect'有循环:'for(UBezierPath * circle in _circles){[circle stroke]; }'。你可以做'圆圈填充';如果你想填充圆圈而不是绘制边界。您也可以将圆圈数据存储在CGRect中,并使用Core Graphics函数绘制,就像您上面所做的一样,但我认为这种方法更简单 - 因为您无法将CGRect放入“NSMutableArray”中,因为它是一个'typedef'而不是一个Objective-C对象。 – 2013-02-17 10:40:18

相关问题