How can I draw one circle around each touch location on the device?
The new programmer here is trying to go step by step. I am trying to find a way to draw a circle around each currently located location on the device. Two fingers on the screen, one circle under each finger.
I currently have working code to draw a circle with one touch, but as soon as I put another finger on the screen, the circle moves to that second touch location, leaving an empty spot for the first touch. and when I add the third one, it moves there, etc.
Ideally, I would like to have up to 5 active circles on the screen, one for each finger.
Here is my current code.
@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);
}
I have it setMultipleTouchEnabled:YES
declared in my method didFinishLaunchingWithOptions
in appDelegate.m.
I've tried using an if statement in a method drawTouchCircle
that modifies self.firstTouch.x
based self.secondTouch.x
on self.tapCount
, but that seems to break everything, leaving me without any circles of places.
I am having a really tough time trying to find my problem and I know it could be something very simple.
source to share
I just wrote some code that seems to work. I added a property NSMutableArray
named circles
to the view that contains UIBezierPath
for each circle. In -awakeFromNib
I am setting up an array and setting self.multipleTouchEnabled = YES
- (I think you did this using the view reference in appDelegate.m).
In view, I am calling this method in methods -touchesBegan
and -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];
}
Touches completed:
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
[_circles removeAllObjects];
[self setNeedsDisplay];
}
Then I iterate over circles
in -drawRect
and call [circle stroke]
on each
source to share