简体   繁体   English

关于UIScrollView子视图上的动画

[英]about the animation on the subview of UIScrollView

When I add some animations on the subview of the scrollview that like extending or contract a textfield,it just not work. 当我在滚动视图的子视图上添加一些动画,例如扩展或收缩文本字段时,它根本无法工作。 But it worked well when the subview add to a simple UIView. 但是,当子视图添加到简单的UIView时,效果很好。

How did it happen? 这是怎么发生的?

There should be no problem animating the frame of a UITextField within a UIScrollView . UIScrollView中为UITextFieldframe设置动画应该没有问题。

The most common source of "my animation isn't animating" in iOS 6 and later is the attempt to animate a control by adjusting the frame when employing autolayout (because the adjustment of the frame can be thwarted by the constraints being reapplied by the most innocuous of actions). 在iOS 6中,“我的动画没有动画”的最常见来源是后来在使用自动布局时尝试通过调整frame来使控件动画化(因为frame的调整会因大多数人重新应用的约束而受挫。无害的行动)。 You can tell if you're using autolayout or not by selecting the File Inspector (the first tab) of the rightmost panel in Interface Builder, and seeing if "Use Autolayout" is checked: 您可以通过在Interface Builder中选择最右边面板的File Inspector(第一个选项卡),然后查看是否选中了“ Use Autolayout”,来判断您是否正在使用自动布局:

在此处输入图片说明

For example, to animate the frame when not using autolayout, assuming your UITextField has an IBOutlet called textField , you might do something like: 例如,要在不使用自动布局时对frame进行动画处理,假设您的UITextField具有一个名为textFieldIBOutlet ,则可以执行以下操作:

[UIView animateWithDuration:0.5 animations:^{
    CGRect frame = self.textField.frame;
    frame.size.width = 280;
    self.textField.frame = frame;
}];

But the equivalent when using autolayout is not achieved by adjusting the frame , but rather by changing the constraints' constant values. 但是,使用自动布局时的等效方法不是通过调整frame来实现,而是通过更改约束的constant量值来实现。 For example, assuming you created an IBOutlet for the width constraint called textFieldWidthConstraint on your UITextField , you would animate the changing of the width with something like: 例如,假设您为UITextField上的名为textFieldWidthConstraintwidth约束创建了IBOutlet ,则可以使用类似以下内容的宽度变化动画:

self.textFieldWidthConstraint.constant = 280;
[UIView animateWithDuration:0.5 animations:^{
    [self.view layoutIfNeeded];
}];

If you believe you are using the appropriate animation technique that corresponds to your choice of autolayout or non-autolayout, but it's still not working, you should show us some code and describe the situation in greater detail. 如果您认为自己正在使用与选择的自动布局或非自动布局相对应的适当动画技术,但仍然无法正常工作,则应向我们展示一些代码并详细描述这种情况。

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

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