William Falcon
William Falcon

Reputation: 9813

nstimer dimming button

I am attempting to cycle four images that have slightly more brightness, I'm attempting to make it appear like it is glowing, however I don't think the code is working how I thought it would

  -(void)loadRefreshButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    refreshButton = button;
    [button addTarget:self
               action:@selector(refreshButtonPressedResult)
     forControlEvents:UIControlEventTouchDown];    [refreshButton setTitle:@"" forState:UIControlStateNormal];
    refreshButton.frame = CGRectMake(80.0, 370.0, 76, 76);
    [self.view addSubview:refreshButton];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                      target: self
                                                    selector: @selector(toggleButtonImage:)
                                                    userInfo: nil
                                                     repeats: YES];
}


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


     if(toggle)
        {
        [refreshButton setImage:[UIImage imageNamed:@"button_zero.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_one.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_two.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_three.png"] forState: UIControlStateNormal];
        }
    else
        {
        [refreshButton setImage:[UIImage imageNamed:@"button_three.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_two.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_one.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_zero.png"] forState: UIControlStateNormal];
        }
    toggle = !toggle;
}

Thank you!

Upvotes: 0

Views: 194

Answers (3)

Rob
Rob

Reputation: 437552

Perhaps the UIView class method animateWithDuration will achieve the desired effect. It's simply:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     refreshButton.alpha = 0.75;
                 }
                 completion:nil];

Obviously, adjust duration and alpha accordingly to achieve the desired effect.

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20541

just add this code

- (void)toggleButtonImage:(NSTimer*)timer
{
        ///write your code above which define above after at last just add this code..
        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];   
        [animation setType:kCATransitionFade];
        [animation setDuration:0.5];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                      kCAMediaTimingFunctionEaseInEaseOut]];
        [[refreshButton layer] addAnimation:animation forKey:@"transitionViewAnimation"];
}

Upvotes: 0

Ramaraj T
Ramaraj T

Reputation: 5230

You don't need to have four images. You can have single image with more brighness and you can use the following code for blinking animation.

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0.3]];
    [animation setDuration:0.8f];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setAutoreverses:YES];
    [animation setRepeatCount:INFINITY];
    [[aButton layer] addAnimation:animation forKey:@"opacity"];

Upvotes: 0

Related Questions