animateWithDuration setFrame不起作用

时间:2013-09-22 17:29:16

标签: ios uiview uiviewanimation

嗨,我有一段代码只有一行不能正常工作......

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

我的想法已经用完了......

4 个答案:

答案 0 :(得分:2)

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

搜索后,我发现: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){
    }];
}

答案 1 :(得分:0)

帧必须是带有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的持续时间,您将看到标签正在移动。

答案 2 :(得分:0)

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

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

答案 3 :(得分:-1)

如果您的问题既不是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问题。一旦我删除了&#34; transitionFromView:...&#34;调用,帧动画工作正常。

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

相关问题