[Objective-c IOS Xcode] How to animate inside UIView?

Discussion in 'iOS Programming' started by johnny11, Dec 29, 2010.

  1. johnny11 macrumors newbie

    Joined:
    Dec 29, 2010
    #1
    Hi,

    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. johnny11 thread starter macrumors newbie

    Joined:
    Dec 29, 2010
    #3
    For now I did it in the following way:

    Code:
    - (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:

    Code:
    - (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));
            }
        }
    }
    }

    I am looking for something as a replacement for drawing every single pixel every calculation.

    Calc method from MyView is generating steps of my animation (changing "board" array).
     

Share This Page