使用-drawRect:时如何为东西设置动画? [英] How to animate stuff when using -drawRect:?

查看:35
本文介绍了使用-drawRect:时如何为东西设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用子视图,而是在UIView子类中使用-drawRect:绘制了我的东西.现在我要在那里做一些动画.

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

我想我现在不能指望核心动画,因为我没有子视图.那么我该如何制作动画呢?我会设置一个每秒触发30次的计时器吗?我怎么知道动画步骤?我会创建一个计算动画帧的ivar,以便在调用它时可以在-drawRect中完成我的工作吗?

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?

推荐答案

您可以精确地做到这一点:拥有一个带有帧号的实例变量,并在计时器中对其进行递增.如果您不在乎缓动,则可以将帧号乘以速度,然后将其设置为要设置动画的属性.

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));
}

这篇关于使用-drawRect:时如何为东西设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆