2012-11-20 42 views
0

我試圖週期有稍微的亮度四象,我試圖使它看起來就像是發光的,但是我不認爲代碼工作我怎麼想它會的NSTimer調光按鈕

-(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; 
} 

謝謝!

回答

1

也許UIView類方法animateWithDuration會達到預期的效果。這是簡單的:

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

顯然,調整durationalpha相應地達到預期的效果。

0

您不需要有四個圖像。您可以使單個圖像更明亮,並且您可以使用以下代碼閃爍動畫。

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"]; 
0

只需添加以下代碼

- (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"]; 
}