离开视图控制器后,如何重新打开按钮动画?

时间:2011-08-12 08:28:00

标签: iphone animation uiviewcontroller

我有一些动画附加到视图控制器中的自定义按钮:

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

但是,有时我需要隐藏此按钮:

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

...然后过了一段时间我需要重新打开它:

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

除非我点击后退按钮并返回到父视图控制器然后返回到此viewController,否则一切正常。在这些情况下,我的“视图setNeedsDisplay”不再有效,我的动画没有打开。

这是我在父视图控制器中创建后栏项目的地方:

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

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

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

    return self;
}

....这里是父视图控制器推回到丢失动画的问题视图控制器的地方:

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

谁能告诉我为什么我会丢失动画以及如何将其重新打开?感谢。

1 个答案:

答案 0 :(得分:0)

您的视图控制器视图由导航控制器保存在内存中。我不完全理解为什么但是当您返回到缓存的视图控制器时,动画不会自动恢复。在您的应用程序在实现多任务处理后进入后台时也会发生同样的事情 - 当应用程序返回到前台时,没有任何动画。

当您按下后退按钮返回到父视图控制器并在使用动画按钮返回视图控制器时再次添加动画时,需要删除动画。

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

添加/删除电话的位置取决于您。你应该知道什么最适合你。