繁体   English   中英

animateWithDuration setFrame不起作用

[英]animateWithDuration setFrame not working

嗨,我有一段代码,只有一行不起作用...

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

我没什么主意了...

我发现的是:如果我关闭“使用自动版式”,那么它会神奇地起作用,因此此问题与自动版式有关。

搜索后,我发现了这一点: http : //weblog.sivecode.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){
    }];
}

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

有时,您还应该在动画后设置框架以使视图具有动画效果,例如在完成块中,尝试类似的操作

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

如果您的问题与“自动版式”都不相关,或者与此处列出的其他问题都不相关,那么还有另一个可能是我的问题。 我同时在占用相同屏幕空间的视图上运行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的完成块中。 这不是完美的,因为两个动画应该对齐,但这是目前可行的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM