How to animate material when using -drawRect :?

I didn't use subviews, but drew my stuff with -drawRect: inside a UIView subclass. Now I want to do some animations there.

I guess now I can't count on basic animation since I have no subplots. So how could I then revive? Would I set a timer that fires like 30 times per second? How do I know the animation step? Can I make an ivar that counts the animation frame so that I can do my stuff in -drawRect as it is called?

0


a source to share


2 answers


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



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

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

      

+1


a source


It works, but it sounds crazy. You are drawing the same content over time and this can cause performance issues in the case of the TableView. I would call the animation CALayer



+1


a source







All Articles