I have a custom UIView on which I would like to show simple animated shapes in response to touches. For instance:
- whenever the user taps the screen a circle appears, with an animation, under the finger
- if the user ends touching the circle should disappear with an animation
- if the user reaches a certain part of the screen the circle should increase its size (smoothly)
What I am doing now (see code below) is to use touchesBegan, touchesMoved, etc to have a set with all the touches. Then in drawRect I draw a circle for each of the touches. Everything is super static.
What is the best way to animate?
Thanks!
PS: for completeness here is part of my code
- (void)drawRect:(CGRect)rect
{
for (UITouch *touch in ts) { //ts is the set with all touches
CGPoint location = [touch locationInView:touch.view];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectangle = CGRectMake(location.x-circleSize/2, location.y-circleSize/2+correctionY,
circleSize*1.15, circleSize*1.15);
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(context, rectangle);
/* etc etc */
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/* etc etc */
ts = [[NSMutableSet setWithSet: [event touchesForView:self]] retain];
/* etc etc */
}
Rather than using drawRect in your main view, I would create a new subview for each object. Those subviews could be UIImageViews, or standard UIViews that you then continue to draw by subclassing the subview’s drawRect.
Then, it would be trivial to animate those subviews using UIView’s animation methods:
Clearly you’d then need to add code to manage the touches and remove the subviews when needed, but that shouldn’t be too onerous.