【问题标题】:animateWithDuration setFrame not workinganimateWithDuration setFrame 不起作用
【发布时间】:2013-09-22 17:29:15
【问题描述】:

您好,我有一段代码,只有一行不工作...

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
    [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
    [self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
    [self.currentLabel removeFromSuperview];
    self.currentLabel = label;
}];

我想不出什么问题了...

【问题讨论】:

  • 我也有同样的问题。
  • 我假设故事板或 xib 对吗?
  • 如果是这样,请转到您的 xib 或该视图的情节提要并取消选择“useAutolayout”。我在某个地方有图片,我一定要找到它。

标签: ios uiview uiviewanimation


【解决方案1】:

我发现:如果我关闭“使用自动布局”,那么它会神奇地工作,所以这个问题与自动布局有关。

搜索后发现:http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

为您的视图添加约束出口映射,然后不使用 setFrame,更新约束就可以解决问题!

下面是我的最终实现(_topConstraint 是表格视图的顶部垂直空间约束):

- (IBAction)switchButtonTouched:(id)sender
{
    if (_topConstraint.constant <= 0)
        _topConstraint.constant = _buttonView.frame.size.height;
    else
        _topConstraint.constant = 0;

    [_tableView setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.5f animations:^(void){
        [_tableView layoutIfNeeded];
    } completion:^(BOOL finished){
    }];
}

【讨论】:

    【解决方案2】:

    Frame 必须是带有 4 个参数的CGRect:(xPosition,yPosition,width,height)。使用CGRectMake 设置框架

    self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height)
    

    您将动画持续时间设置为 0.3 秒,并在结束时将其从视图中移除。这是太短了。为示例 2 设置持续时间,您会看到标签正在移动。

    【讨论】:

    • self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height)
    • 您设置了 0.3 秒的动画持续时间,当它结束时您将其从视图中移除。这是太短了。为示例 2 设置持续时间,您将看到您的标签正在移动。
    【解决方案3】:

    有时你应该在动画之后设置动画视图的帧,例如在完成块中,尝试类似的东西

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
        [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
        [self.currentLabel setAlpha:0.0f]; //working
    } completion:^(BOOL finished) {
        self.frame = currentLabelFrame;
        label.frame = label.frame;
        [self.currentLabel removeFromSuperview];
        self.currentLabel = label;
    }];
    

    【讨论】:

      【解决方案4】:

      如果您的问题既不是 Autolayout 相关的,也不是此处列出的其他问题,那么还有另一个可能是我的问题。我同时在占用相同屏幕空间的视图上运行 UIView 转换 (transitionFromView:toView:)(不同的超级视图,不相关,除了定位在总体视图控制器的超级视图中)。

      我的代码如下所示:

      [UIView transitionFromView:self.someView
                          toView:self.someOtherView
                        duration:0.6f 
                         options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionShowHideTransitionViews 
                      completion:NULL];
      
      [UIView animateWithDuration:0.3
                            delay:0.0 options:UIViewAnimationOptionCurveEaseOut 
                        animations:^{
                              [self.someThirdView setFrame:someFrame]; //not working 
                              [self.someThirdView setAlpha:1.0f]; //working
                      } completion:nil];
      

      帧发生了变化,但它弹出到新帧而不是动画。我猜这是一个内部iOS问题。一旦我删除了“transitionFromView:...”调用,帧动画就可以正常工作了。

      我目前的解决方案是将第二个动画移动到 transitionFromView 的完成块中。这不是完美的,因为两个动画应该排成一列,但它现在是一个可以通过的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        相关资源
        最近更新 更多