【问题标题】:How do I turn my button animation back on after leaving the view controller?离开视图控制器后如何重新打开按钮动画?
【发布时间】:2011-08-13 06:05:41
【问题描述】:

我在视图控制器的自定义按钮上附加了一些动画:

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

这一切都很好,除非我点击后退按钮并返回父视图控制器,然后返回此视图控制器。在这些情况下,我的“view 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];

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

【问题讨论】:

    标签: iphone animation uiviewcontroller


    【解决方案1】:

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

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

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

    添加/删除调用的位置由您决定。你应该知道什么最适合你。

    【讨论】:

    • 谢谢,但是是否可以从已作为子视图添加到视图的按钮中添加/删除动画?当我尝试上面列出的命令时,它无法识别“natSenButton”。我在想,每次视图控制器消失/出现时,我都需要完全删除/添加整个 subView。
    • 如果您要从其他方法进行调用,您的按钮必须是实例变量。将按钮添加为子视图后,删除动画没有问题。
    • 对不起,但我不知道您所说的“按钮必须是实例变量”是什么意思。按钮是一个对象,对吧?怎么可能是实例变量?
    • 我的意思是按钮必须在@interface 部分中声明,以便它可以从您的实现中的任何地方接收消息。这使它成为一个实例变量,你不同意吗?
    • 好的,我明白你的意思了。您之前说过,应用程序进入后台也会导致动画停止。您如何捕捉这些情况,以便在应用程序从后台返回时重新启动动画。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2019-08-29
    • 2020-01-20
    • 2017-09-09
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多