【问题标题】:UISegmentedControl selection not updating before animationUISegmentedControl 选择在动画之前未更新
【发布时间】:2013-01-08 15:03:31
【问题描述】:

我有一个 UISegmentedControl 像模态窗口一样显示/隐藏。最初它没有选定的段。在 IB 中,Value Changed 事件连接到方法 - (IBAction)cardClassificationChanged:(UISegmentedControl *)sender。这是那个方法:

- (IBAction)cardClassificationChanged:(UISegmentedControl *)sender
{
    NSLog(@"%d", sender.selectedSegmentIndex);
    // block for updating the categorization of current card asynchronously
    [self.cardActionSheet hideWithAnimation];
}

如果我注释掉最后一行(对 -hideWithAnimation 的调用),选择会按预期更改,一切正常。但是,通过调用该动画方法,UISegmentedControl 选择在动画之前不会在视觉上发生变化。这是 hideWithAnimation 方法:

- (void)hideWithAnimation
{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    animation.duration = globalAnimationLength;
    [self.layer addAnimation:animation forKey:nil];

    self.hidden = YES;
}

下次出现此视图时(通过触摸手势),UISegmentedControl 将选择正确的段。

似乎我不应该为 UISegmentedControl 调用 setNeedsDisplay,但即使我在 cardClassificationChanged 方法或 hideWithAnimation 方法中试验它,它也不会刷新。

我显然遗漏了与 UI 更新相关的内容,我需要调用什么来在动画之前更新 UISegmentedControl 选择?

【问题讨论】:

  • -hideWithAnimation 方法永远不会在 -cardClassificationChanged: 处调用,而是调用 -hideCardActionSheet:。你能检查一下复制的代码并修复它吗?
  • 你是对的,很抱歉造成混乱。 -hideCardActionSheet 有一些其他的逻辑/处理,然后调用 -hideWithAnimation。当我能够回到那台机器时,我会更新。目前,cardClassificationChanged 内部的动画或逻辑没有任何问题,我的问题是 UISegmentedControl 没有在视觉上更新以反映选择。
  • @A-Live 已编辑,感谢您指出这一点。

标签: ios


【解决方案1】:

我建议你应该使用 UIKit 动画来淡出你的控制。试试下面的代码

- (void)hideWithAnimation
{
    [UIView animateWithDuration:0.2
                 animations:^{
                     self.alpha = 0.;
                 } completion:^(BOOL finished) {
                     self.alpha = 1.;
                     self.hidden = YES;
                 }];
}

【讨论】:

  • 使用这个基于块的动画解决了这个问题,并且是更现代的代码:) 非常感谢。您可以提供任何关于为什么 UISegmentedControl 不使用 CATransition 方法更新的参考或评论将不胜感激(出于我自己的理解)。
  • @afalls 一定是线程相关的,如果你用[self performSelector:@selector(hideWithAnimation:) withObject:sender afterDelay:0];调用动画(当然从主线程,关键是延迟,即使它是零)它可以正常工作CATransition
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多