这里的新程序员试图一步一步地做事。我试图找到一种方法在设备上每个当前触摸的位置周围画一个圆圈。两个手指在屏幕上,每个手指下一个圆圈。
我目前有在一个触摸位置画一个圆圈的工作代码,但是一旦我将另一根手指放在屏幕上,圆圈就会移动到第二个触摸位置,而第一个触摸位置是空的。当我添加第三个时,它会移动到那里等等。
理想情况下,我希望屏幕上最多可以有 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 的方法中setMultipleTouchEnabled:YES
声明了。didFinishLaunchingWithOptions
我试图在方法中使用一个 if 语句,该语句基于 adrawTouchCircle
更改为self.firstTouch.x
,但这似乎破坏了整个事情,让我在任何触摸位置都没有圆圈。self.secondTouch.x
self.tapCount
我很难找到我的问题,而且我知道这可能很简单。