【问题标题】:move UIView not responding in the viewDidAppear method在 viewDidAppear 方法中移动 UIView 没有响应
【发布时间】:2013-11-11 21:09:50
【问题描述】:

我有一个 UIView,当父视图控制器出现在屏幕上时,我正试图向上移动一点。我一直在阅读这篇文章,我看到的大多数人似乎都在说使用 viewDidAppear 方法对布局进行任何视觉调整。我已经尝试过了,它似乎不起作用。什么都没有发生,然后我 nslog 了 origin.y 我得到了 -47,000,然后我可能假设某些东西还没有初始化。这是我尝试过的。

  - (void) viewDidAppear:(BOOL)animated
    {
        // set the save view y postion
       saveData.center = CGPointMake( 0.0f, 0.5f );
        NSLog(@"This is the y %f", saveData.frame.origin.y);
        NSLog(@"This is the center points on load %@", NSStringFromCGPoint(optionalData.center));
    }

但是如果我在 viewDidLoad 方法中添加一个延迟的方法调用这样的事情:

[self performSelector:@selector(moveSaveView) withObject:nil afterDelay:0.7f];

有了这个,就可以了

- (void) moveSaveView
{
    // set the save buttons y postion


    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        // Animate the alpha value of your imageView from 1.0 to 0.0 here
        optionalData.alpha = 0.0f;
    } completion:^(BOOL finished) {
        // Once the animation is completed and the alpha has gone to 0.0, hide the view for good
        optionalData.hidden = YES;
    }];

    // move the save button up
    [UIView animateWithDuration:0.5
                     animations:^{saveData.center = CGPointMake( 160.0f, 280.5f );}];


    saveData.center = CGPointMake( 160.0f, 280.5f );
}

这也是我使用自动布局的问题吗?我只是希望我的视图从我需要的地方开始,而不是使用一些延迟调用来实现它。

编辑: 所以我试了一下,想出了这个来尝试移动我的 UIView:

- (void) viewDidAppear:(BOOL)animated
    {   
        NSLog(@"this is the constraing %f",     saveData.saveButtomConstraint.constant);  // gives me 93 which is here its at.
        saveData.saveButtomConstraint.constant = 32;
        [saveData setNeedsUpdateConstraints];
        [saveData layoutIfNeeded];
        NSLog(@"this is the constraing %f", saveData.saveButtomConstraint.constant); // gives me 32 which is here its at.   
    }

问题是视图永远不会在屏幕上移动。我错过了什么?当它与同一个问题相关时,也可以像这样发布和编辑吗?我仍在努力掌握这个表格。

【问题讨论】:

    标签: uiview delay


    【解决方案1】:

    是的,您的问题是由于您使用的是自动布局。帧动画与自动布局不兼容,因此您需要为视图上的约束设置动画。查看this answer 了解详细信息,this might also help too。祝你好运!

    编辑

    所以看起来您已经向您的saveData UIView 添加了一个名为saveButtomConstraint 的属性。这很好,因为它使您可以访问该约束。但是,您确定该约束实际上是 [saveData constraints] 数组的成员吗?通常在 Interface Builder 中的约束被添加到父 UIView。我认为问题很可能是在错误的视图上调用 layoutIfNeeded,您需要在 saveData 的父视图上调用它,或者可能在视图控制器的根视图上调用它,[self view]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多