开发者

How do I turn my button animation back on after leaving the view controller?

I have some animation attached to a custom button in a view controller:

- (void)viewDidLoad {
    [super viewDidLoad];    

    UIButton *natSenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    natSenButton.frame = CGRectMake(114, 4, 93, 30);
    [natSenButton setTitle:@"Comment" forState:UIControlStateNormal];
    [natSenButton setTitleColor: [UIColor colorWithRed: 255 / 255.0 green:0 blue: 0 alpha:1.0] forState: UIControlStateNormal];
    // text: color with dark red

    [natSenButton addTarget:self action:@selector(displayNativeSentence) forControlEvents:UIControlEventTouchUpInside];
    natSenButton.tag = 333;

    [[natSenButton layer] setCornerRadius:8.0f];
    [[natSenButton layer] setMasksToBounds:YES];

    natSenButton.layer.borderWidth = 2.0; 
    natSenButton.layer.borderColor = 
    [UIColor colorWithRed: 25 / 255.0 green:255/ 255.0 blue: 255/255.0 alpha:1.0].CGColor; 
    // above is light开发者_开发技巧 blue color
    natSenButton.layer.backgroundColor = [UIColor colorWithRed: 255 / 255.0 green:255/ 255.0 blue: 255/255.0 alpha:1.0].CGColor;
    // background is white
    //  natSenButton.center = self.center;
    natSenButton.titleLabel.font = [UIFont systemFontOfSize:15.0];

   //Create an animation with pulsating effect
   CABasicAnimation *theAnimation;

   theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
   theAnimation.duration=1.0;   
   theAnimation.repeatCount= 999;
   theAnimation.autoreverses=YES;   
   theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
   theAnimation.toValue=[NSNumber numberWithFloat:0.2];

   [natSenButton.layer addAnimation:theAnimation 
                          forKey:@"animateOpacity"]; 
   [self.view addSubview:natSenButton];}

However, sometimes I need to hide this button:

for( UIView *view in self.view.subviews ) {  
        if( [view isKindOfClass:[UIButton class]] ) {  
            if( view.tag == 333 )
                [view setHidden:YES];
        }  
    }

...and then after awhile I need to turn it back on:

for( UIView *view in self.view.subviews ) {  
        if( [view isKindOfClass:[UIButton class]] ) {  
            if( view.tag == 333 )
                [view setHidden:NO];
                [view setNeedsDisplay];  
        }  
    }

This all works fine except when I tap the back button and go back to the parent view controller and then return to this viewController. In those cases, my "view setNeedsDisplay" no longer works and my animation is not on.

Here is where I create my back bar item in the parent view controller:

- (id)init {
    [super initWithStyle:UITableViewStylePlain];

    [[self navigationItem] setTitle:@"Wordplay"];
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];      
    temporaryBarButtonItem.title = @"<"; 

    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    [temporaryBarButtonItem release];  

    return self;
}

....and here is where the parent view controller pushes back to the problem view controller which has lost its animation:

[[self navigationController] pushViewController:sentenceViewController animated:YES];

Can anyone tell me why I'm losing my animation and how I can turn it back on? Thanks.


Your view controller's view in being kept in memory by the navigation controller. I don't fully understand why but animations are not resumed automatically when you return to a cached view controller. The same thing happens as well after your app goes into the background when implementing multitasking - when the app returns to the foreground nothing is animating.

You need to remove the animation when you press the back button to go back to your parent view controller and add the animation again when you you return to your view controller with the animating button.

 [natSenButton.layer removeAnimationForKey:@"animateOpacity"];

Where to place the add/remove calls is up to you. You should know what suits you best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜