0

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?

dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

2 Answers2

1

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
  • 3,375
  • 1
  • 22
  • 27
1

This works but sounds like crazy.YOu draw the same content hunderts of times and it could cause performance issues in case of TableView.I would refer to CALayer animations

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79