【发布时间】:2013-02-17 01:24:55
【问题描述】:
这里的新程序员试图一步一步来。我试图找到一种方法在设备上每个当前触摸的位置周围画一个圆圈。两个手指在屏幕上,每个手指下一个圆圈。
我目前有在一个触摸位置画一个圆圈的工作代码,但是一旦我将另一根手指放在屏幕上,圆圈就会移动到第二个触摸位置,而第一个触摸位置是空的。当我添加第三个时,它会移动到那里等等。
理想情况下,我希望屏幕上最多有 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 方法中使用 if 语句,它基于 self.tapCount 将 self.firstTouch.x 更改为 self.secondTouch.x 但这似乎破坏了整个事情,让我没有任何联系地点。
我很难找到我的问题,我知道这可能很简单。
【问题讨论】:
标签: ios multi-touch touchesbegan