tags:

views:

153

answers:

1

I didn't use subviews but painted my things with -drawRect: inside an UIView subclass. Now I want to do some animations in there.

I guess that I can't count on core animation now since I have no subviews. So how would I animate then? Would I set up a timer which fires like 30 times per second? How would I know the animation step? Would I make an ivar which counts the frame of the animation so that I can do my stuff in -drawRect as it gets called?

+2  A: 

You can do exactly that: have an instance variable with the frame number and increment it in the timer. If you don't care about easing, you can then just multiply the frame number by a speed and then set that to the property which you want to animate.

-(void)timerfunction{
    ++frame;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake((2*frame)+5,5,20,20));
}
Maz