这里的新程序员试图一步一步地做事。我试图找到一种在设备上的每个当前触摸位置周围绘制圆圈的方法。屏幕上两个手指,每个手指下一圈。如何在设备上的每个触摸位置绘制一个圆圈?
我现在有工作在代码一个触摸位置画一个圈,但一旦我躺在另一手指在屏幕上,在圆内移动到第二触摸位置,离开第一触摸位置是空的。当我添加第三个,它的动作有等
理想我想能有到屏幕上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.x
到self.secondTouch.x
根据self.tapCount
但似乎打破了整个事情,留下我在任何触摸位置无界的if语句。
我有一个非常艰难的时间,试图找到我的问题,我知道,它可能是很简单的。
[circle stroke]
原谅我,因为我真的还是很新的这一点,但我认为我设法解决了你所说的话,直到你说通过'-drawRect'中的'circles'循环。你是否想要遍历数组中的每个对象?我会怎么做呢? – BendE 2013-02-17 02:49:45我的'-drawRect'有循环:'for(UBezierPath * circle in _circles){[circle stroke]; }'。你可以做'圆圈填充';如果你想填充圆圈而不是绘制边界。您也可以将圆圈数据存储在CGRect中,并使用Core Graphics函数绘制,就像您上面所做的一样,但我认为这种方法更简单 - 因为您无法将CGRect放入“NSMutableArray”中,因为它是一个'typedef'而不是一个Objective-C对象。 – 2013-02-17 10:40:18