Reputation: 297
I am a novice ios programmer & I am doing my first ios project. In my project I have several UIView
and UIViewController
. In each view I have overriden drawRect
method and drawn several circles. In one my view I need to draw a rectangle and when I touch it, it would draw another 3 rectangles beneath it with animation. I was wondering what would be the best way to do it! I was thinking to redraw the view after touch by calling setNeedsDisplay
and draw the other three rectangles with animation. How can I achieve this and what else method would be best to perform this particular thing with animation?
Upvotes: 1
Views: 1689
Reputation: 7102
Overriding drawRect may not be the best solution for you because to accomplish many types of animations you'll need to redraw many times under a timer. Beyond that, your drawing will be done with the CPU, you won't get to take advantage of any hardware acceleration.
I suggest that before the three new rectangles are visible you add three UIViews to the view hierarchy in the places those rectangles should be. Set the backgroundColor
property of each view to the color of rectangle you want and set the alpha
value of each view to 0. When it's time to animate the rectangles into view, you can then use code like this:
[UIView animateWithDuration:0.2 animations:^{
rectView1.alpha = 1.0;
rectView2.alpha = 1.0;
rectView3.alpha = 1.0;
}];
Of course change the duration of the animation fit your application, and you can change other properties of the rectViews as well. Suppose you want the rects to fade in and fly in from the sides. You can accomplish that by adjusting the frame
property of each rectView before the animation and then inside the animations block.
Upvotes: 1