How to animate inside UIView?
I have my custom UIView inside which I have a two dimensional array. This array contains pixels that I draw on my custom UIView with drawRect method.
Now I want to change this pixels (my 2-dim array) with 100 steps (or just with the result array). 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
[UPDATE, same day, 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.
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)); } } }
}
Answers
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.
For example uses see
http://www.iphonedevsdk.com/forum/iphone-sdk-development/23525-cgimagecreate-alpha.html
http://iphonedevelopment.blogspot.com/2009/04/creating-uiimages-from-tga-data.html