ios - 如何使用自动布局移动标签并为更改设置动画?

标签 ios objective-c uiview autolayout nslayoutconstraint

我正在尝试将 UILabel 向左移动并为移动设置动画。 UILabel 是从 Storyboard创建的,并具有以下约束:

enter image description here

但是如果我试图使尾随空格常数等于 150 而不是 20 以将其向左移动,它会奇怪地向左弹跳一秒钟然后回到中间。所以我将左前导约束的优先级降低到 900,但同样的事情仍然发生。

所以我尝试了:

self.textToReadLabel.transform = CGAffineTransformMakeTranslation(-500.0, 0.0);

移动它,但如果我对它使用动画,它会滑出屏幕,滑回屏幕,然后停在它应该的位置......在所有这些戏剧之后。

我将如何实现这一点?

当我点击按钮移动标签时发生的全部事情:

        self.textHorizontalPlacementConstraint.constant = 150.0;
        self.textHorizontalPlacementConstraint2.constant = -110.0;
        [UIView animateWithDuration:1.0 animations:^{
            [self.view layoutIfNeeded];
        }];

        CATransition *animation = [CATransition animation];
        animation.duration = 0.5;
        animation.type = kCATransitionFade;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [self.textToReadLabel.layer addAnimation:animation forKey:@"changeTextTransition"];

        // Change the text
        self.textToReadLabel.text = @"text";
    }

外观视频:

http://cl.ly/3l2E401N3Q3h

最佳答案

您可以在 Interface Builder 中设置约束的导出,然后当您想要为更改设置动画时,您可以按如下方式修改代码:

    [UIView animateWithDuration:2.0 animations:^{
        //Change your constraints from code.
    }];

例如:如果我有一个 leftSpacing 约束,我可以直接如下更改它:

    [UIView animateWithDuration:2.0 animations:^{
        //Change your constraints from code.
        self.leftSpacing.constant = self.leftSpacing.constant+20;
        [self.view layoutIfNeeded];
    }];

以上代码将使您的 View 从左到右移动 20 点。

更新:

您是否尝试过添加多个约束:比如间距值应小于或等于 say:200 但大于或等于 20。我认为这可能会有所帮助。

关于ios - 如何使用自动布局移动标签并为更改设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164063/

相关文章:

ios - 当侧边栏处于事件状态时防止 TableView 滚动

ios - 设置 CGRect 的优先级 - Swift

ios - 如何使用指向MTLBuffer Swift的指针进行reinterpret_cast

c++ - openCV 2.4.3 iOS 框架编译器无法识别某些 C++ header

c# - 将图像从 iPhone 上传到 WCF 服务

ios - UILabel 的闪烁效果

ios - 为什么 UIView 会同时调用 init 和 initWithFrame?

iphone - 在录制 OpenGL View 时录制音频

javascript - SignInWithEmailAndPassword (Ionic/Vue/Firebase) 无法解决 iOS 中的 promise

ios - 当应用程序变为事件状态时,文件已加密或不是数据库消息