[How-To] Cycle through UIView background Colors with Timer.

Discussion in 'iOS Development' started by cocotutch, Jul 30, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Ok, this will be my last question before I do the rest myself, I know I shouldn't be asking here, but this is driving me bonkers.

    I have set up an NSTimer in Xcode. Non Repeating.

    I have my method that I want to fire the timer.

    I want it to randomly cycle through UIView Background Colors; For Example:

    [OBJC]- (IBAction) clearBall

    Please Register or Log in to view images

    id)sender {
    ball.color = [UIColor clearColor].CGColor;
    bounceField.backgroundColor = [UIColor greenColor];
    --timer fires here--
    --new color fades in gently--
    --timer fires again--
    --new color fades in gently--
    }[/OBJC]

    It just won't work. It gets stuck at the final colour (Green>Red>Chartreuse>Blue Violet>Baby Pink>Purple>Ice Blue>Yellow>Pale Blue)

    I want it to go from Green all the way to Pale Blue with the fades and all that or just set it and then start again. But it just doesn't seem to be working for me.

    cocotutch
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Use UIView animations (well documented in this forum) and set the animation callback. In the callback, call the original animation method again, but set the next color. No timers required.

    Better yet, use recursion. Have the animation method itself be the callback.
  3. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Please stop putting square brackets around How To when you make a thread. Very, very misleading.
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [OBJC]
    /* Somewhere in your code */
    [self animate:nil finished:nil context:NULL];

    /* Elsewhere in your code */

    - (void)animate

    Please Register or Log in to view images

    NSString *)animation finished

    Please Register or Log in to view images

    NSNumber *)finished context

    Please Register or Log in to view images

    void *)context {
    /* Do something that figures out what the next color should be */
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector

    Please Register or Log in to view images

    selector(animate:finished:context

    Please Register or Log in to view images

    ];
    //Set the color
    [UIView commitAnimations];
    }
    [/OBJC]


    Something along those lines is what I would do. Calling the first line causes the animation to begin. As one color is set, the callback method (the same as the initial animation method) spawns another animation cycle changing to the next color. It is all handled automatically for you. Usually, the color property of things can be animated, see this video for an example.

    I don't actually know how you're animating, if you're using an image, or if you're using CG and masking, etc, so I can't be of any help there.
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    It was just the UIView background colour that I was playing around with. But it ended up going all slow and stuttery. I don't want that, I want as much compatibility with all devices as I can.

    I am now adding RGB sliders to my project, I have been experimenting with these to replace the Clear Color. How did you get your sliders to apply a colour? I have tried:

    sliderR.value = redColorRGB; -- redColorRGB undeclared first use in this function?

    and then going in here:

    ball.color = [UIColor colorFromRGBIntegers:slider1value green:71 blue:171 alpha:1.0].CGColor;

    but it's not working. How can I use the Method I created in the Developer Tutorials section to make this?

    EDIT: I fixed it, RGB Sliders now work as a replacement for Clear.

Share This Page