简体   繁体   English

如何在UIView内部制作动画?

[英]How to animate inside UIView?

I have my custom UIView inside which I have a two dimensional array. 我有我的自定义UIView,其中有一个二维数组。 This array contains pixels that I draw on my custom UIView with drawRect method. 该数组包含我使用drawRect方法在自定义UIView上绘制的像素。

Now I want to change this pixels (my 2-dim array) with 100 steps (or just with the result array). 现在,我想以100步(或仅使用结果数组)更改此像素(我的2维数组)。 How do I make this effect animated? 如何使该效果动起来? It is no problem to display previous state and then actual state, but I need animation between this states. 先显示状态,然后显示实际状态是没有问题的,但是我需要在这些状态之间进行动画处理。

Thank You for any help. 感谢您的任何帮助。

Best regards, Wojtek 最好的问候,Wojtek

[UPDATE, same day, 14:15] [更新,当天14:15]

For now I did it in the following way: 现在,我通过以下方式做到了:

- (void)calculate:(NSTimer *)timer {

int result = [myView calc];
if(result > 0)
{
    [NSTimer scheduledTimerWithTimeInterval:0.00001f
                                     target:self selector:@selector(calculate:)
                                   userInfo:nil
                                    repeats:NO];
}
[myView setNeedsDisplay];}

but it is not so good, because refreshing makes it very slow. 但这不是很好,因为刷新会使它非常缓慢。 If I refresh every 2-4 calculations my animation is not smooth. 如果我每2-4次计算刷新一次,我的动画就会不流畅。

MY drawing method: 我的绘画方法:

- (void)drawRect:(CGRect)rect{
int i = 0, j = 0;

CGContextRef myContext = UIGraphicsGetCurrentContext();


// ****************** drawing ********************
for(i = 0; i < 768; i++)
{
    for(j = 0; j < 768; j++)
    {
         if(board[i][j] == 1)
         {
             CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);
             CGContextFillRect (myContext, CGRectMake (i, j, 1, 1));
         }
        else
        {
            CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);
            CGContextFillRect (myContext, CGRectMake (i, j, 1, 1));
        }
    }
}

} }

Drawing one pixel at a time with fill rect will take some time. 一次使用填充矩形绘制一个像素将需要一些时间。

I'd suggest that you setup a memory area as the image you want to draw and then use CGImageCreate to create an image you can draw in the UIView. 我建议您将存储区域设置为要绘制的图像,然后使用CGImageCreate创建可以在UIView中绘制的图像。

For example uses see 例如使用参见

http://www.iphonedevsdk.com/forum/iphone-sdk-development/23525-cgimagecreate-alpha.html http://www.iphonedevsdk.com/forum/iphone-sdk-development/23525-cgimagecreate-alpha.html

http://iphonedevelopment.blogspot.com/2009/04/creating-uiimages-from-tga-data.html http://iphonedevelopment.blogspot.com/2009/04/creating-uiimages-from-tga-data.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM