【问题标题】:UIImageView Disappears when UILabel is updatedUIImageView 在更新 UILabel 时消失
【发布时间】:2012-07-03 13:54:47
【问题描述】:

我有一系列 UIButtons 用于更新几个 UILabel 并启动对象从屏幕外到屏幕上的动画。进展是这样的。按钮 1 和 2 是可见的,当用户按下按钮时,UILabel 会更新,新的 UIImageView 会在屏幕上显示动画。这在第一次迭代中运行良好,在第二轮中,隐藏的按钮 3 和 4 变得可见。一旦用户选择按钮 3 或 4,另一个 UIImageView 应该在屏幕上动画(在其他图像的顶部......“堆叠”它们)。正在发生的事情是 UIImageView 在用户按下按钮 1 或 2 后动画,消失,并且没有动画。仅当 UILabel 必须根据按钮选择进行更新时才会发生这种情况。在 UILabel 中不需要更新,动画效果很好。

相关代码:

-(IBAction)didTapSizeButton:(id) sender {

if ([sender tag] == 1) {
    sizeLabel.text = @"12 oz";
}
else if ([sender tag] == 2) {
    sizeLabel.text = @"24 oz";
}

if (coasterTwo.center.x != 150) {

    [UIView animateWithDuration:0.35
                          delay:0
                        options:(UIViewAnimationOptionCurveEaseOut)
                     animations:^{
                         coasterTwo.center = CGPointMake(150, 220);

                     } completion:^(BOOL finished) {


                     }];

}

containerOne.hidden = NO;
containerTwo.hidden = NO;

}

-(IBAction)didTapContainerButton:(id)sender {

if ([sender tag] == 3) {
    containerLabel.text = @"Bottle";
}

else if ([sender tag] == 4) {
    containerLabel.text = @"Can";
}

if (coasterTwo.center.x == 150 && coasterThree.center.x != 150) {

    [UIView animateWithDuration:0.35
                          delay:0
                        options:(UIViewAnimationOptionCurveEaseOut)
                     animations:^{
                         CGPoint center = coasterThree.center;
                         center.x += 305;
                         coasterThree.center = center;

                     }
                     completion:^(BOOL finished){

                     }];

}


}

当我返回更新 UILabel 时,UIImageViews 显然会重置到它们的起始位置......我只是不知道如何防止这种情况发生。

【问题讨论】:

  • 很奇怪。您没有使用子类 UIView 或类似的东西,是吗?为什么说“UIImageViews 明显重置到它们的起始位置”?上面的代码中没有任何内容可以表明这一点。通常它们不会被重置为起始位置,除非您在某处有重置位置(框架、原点或中心)的代码,例如您自己的drawRect。如果您有任何代码可以做到这一点(viewDidLoad 除外),请分享。
  • 确实很奇怪...... .m 文件中除了处理按钮自定义和设置按钮上的 .hidden 和 .tag 属性之外没有其他代码。除了viewDidLoadshouldAutorotateToInterfaceOrientation 之外,整个.m 都会显示。重置 UIImageView 位置可能不是合适的词,但它最能代表按下按钮 3 或 4 时更新 UIlabel 时发生的操作。
  • 我认为问题不在这里。
  • 我在想 IB 中的某些东西搞砸了,但我已经两次和三次检查以确保一切都正确连接。 AppDelegate 中除了模板代码之外没有任何内容,从字面上看,这个和视图确实加载是视图控制器中唯一的东西。
  • 我重写了方法并尝试了许多选项,当我从第二组按钮更新 UILabel 时,这种行为明显发生。没有 UILabel 更新?没问题,动画按预期发生。一旦我更新一个标签,所有在屏幕上动画的 UIImageViews 都会消失。

标签: ios xcode uiimageview uibutton uilabel


【解决方案1】:

我建议使用静态 UIView commitAnimations 方法,因为您不需要完成块:

[UIView beginAnimations:nil context:nil];
//Code for changing frames / alpha / anything else that is animatable
[UIView commitAnimations];

试试这个,看看有没有帮助。

【讨论】:

  • 没有运气,仍然得到相同的行为。如果您注释掉有关标签的 if/else 语句并让按钮起作用而不更新 UILabel,则动画效果很好。我认为问题出在某个地方。
【解决方案2】:

必须有一些简单的东西。正如我所说,didTapContainerButton 的代码似乎很可疑(每次你点击它coasterThree 都会向右移动 another 305 个点),但上面没有显示任何内容会导致 coasterTwo消失。唯一会这样做的是,如果 (a) 某些 IB 链接被弄乱了(尽管尚不清楚当您按下按钮 3 或 4 时这会如何导致图像消失); (b) 您没有显示的代码可能会隐藏它;或(c)您的窗口上有一些隐藏它的其他控件(我肯定见过更新标签导致它被带到前面的情况,掩盖它后面的东西......你可以通过以下方式诊断它使标签具有透明背景,明确地将图像移动到前景,或者尝试将 alphas 设置为 0.5,以便在调试时可以看穿东西)。

要诊断第一种可能性,您必须将您的项目上传到 DropBox 或其他东西,我们可以查看它(如果您愿意这样做......如果不是,请复制您的项目,删除任何机密/专有内容,然后上传)。

要诊断第二种可能性,您可以使用视图控制器的 .m 文件的完整代码更新您的问题。

要诊断这第三种可能性,您可以确保在制作图像动画之前将其向前移动(在制作coasterThree 动画之前,请执行[self.view bringSubviewToFront:coasterThree];)。您还可以确保所有控件的backgroundColor(如果您在 IB 中添加了任何容器 UIView,它们也是)是透明的[UIColor clearColor]。您也可以,仅出于诊断目的,暂时降低 alpha 以便您可以看穿它们(因此,如果有任何东西阻塞了其他东西(您可以在 IB 中执行此操作,单击主 UIView 背景,按 command kbd>+A 选择所有控件,并将 alpha 更改为 0.5)。

最重要的是,我会选择选项 3,如果您没有运气,请分享您的项目或分享一个全面的代码示例。如果你觉得这样做不自在,我可能会建议你扔掉旧的 NIB 并从头开始重建它(以防万一你有什么奇怪的东西)或者开始构建一个全新的测试项目,你可以在其中逐步重新实现你的 UI,看看你能不能找出哪里出了问题。

但是没有什么与 UIImageViews 动画和设置 UILabel 文本值不一致的地方。下面我有两个按钮的代码,两个动画开和关的图像,以及一个更新的标签,一切都很好。 (我遇到了信任危机:我开始担心与 UILabel 和动画有一些奇怪的交互。)叹息。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGPoint center;

    self.label.text = @"";

    // move image 1 (dog image) off screen to the left

    center = self.image1.center;
    center.x = -0.5 * self.view.frame.size.width;
    self.image1.center = center;
    self.image1.hidden = YES;

    // move image 2 (cat image) off screen to the right

    center = self.image2.center;
    center.x = 1.5 * self.view.frame.size.width;
    self.image2.center = center;
    self.image2.hidden = YES;

    // Do any additional setup after loading the view.
}

// if you hit button 1 (dog) button, change label, and animate the dog image on and cat image off

- (IBAction)button1:(id)sender 
{
    self.label.text = @"Dog";

    self.image1.hidden = NO;

    [UIView animateWithDuration:0.5 
                     animations:^{
                         CGPoint center;

                         // move image 1 (dog image) onscreen

                         center = self.image1.center;
                         center.x = 0.5 * self.view.frame.size.width;
                         self.image1.center = center;

                         // move image 2 (cat image) off screen to the right (if it's not there already)

                         center = self.image2.center;
                         center.x = 1.5 * self.view.frame.size.width;
                         self.image2.center = center;
                     }];
}

// if you hit button 2 (cat) button, change label, and animate the dog image off and cat image on

- (IBAction)button2:(id)sender 
{
    self.label.text = @"Cat";

    self.image2.hidden = NO;

    [UIView animateWithDuration:0.5 
                     animations:^{
                         CGPoint center;

                         // move image 1 (dog image) off screen to the left

                         center = self.image1.center;
                         center.x = -0.5 * self.view.frame.size.width;
                         self.image1.center = center;

                         // move image 2 (cat image) on screen

                         center = self.image2.center;
                         center.x = 0.5 * self.view.frame.size.width;
                         self.image2.center = center;
                     }];
}

【讨论】:

    【解决方案3】:

    我的一个学生也发生了同样的事情。为了证明你不会发疯......尝试在 iOS 5.x 模拟器中运行它(并希望观察到所需的行为)。

    对于 iOS 6,您要么必须为 UIView 编写一些状态保存代码 - 或者 - 如果您还没有成长为爱上它们并认为没有它们就活不下去 - 摆脱通过取消选中文件检查器中的“使用自动布局”来限制 xib 文件中的约束。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多