我有一个带有 subview 的 UIView,并且只想为某些 View 的特定属性设置动画。例如,有时我想调用 [self layoutIfNeeded]
并只为边界设置动画,而不是 View 或其 subview 的其他属性。
问题是 +[UIView animateWithDuration:animations]
跟踪 subview 和所有动画属性。对此有合理的解决方案吗?
最佳答案
看看 +[UIView performWithoutAnimation:]
。您指定希望在没有动画的情况下执行的更改 block ,它们会立即发生。
这适用于 iOS7 及更高版本,并且仅适用于 UIKit 动画。直接在图层对象上处理动画,或者支持老版本的iOS,可以使用如下代码:
[CATransaction begin];
[CATransaction setDisableActions:YES];
//Perform any changes that you do not want to be animated
[CATransaction commit];
有关performWithoutAnimation 的更多信息:
here和 setDisabledActions:
here .
如果你不想改变父级的代码,你可以实现你不想动画的属性的设置方法,并用 performWithoutAnimation:
包装 super 调用,如下所示:
- (void)setFrame:(CGRect)frame
{
[UIView performWithoutAnimation: ^ {
[super setFrame:frame];
}];
}
关于ios - 将 UIView 动画范围限定到特定 View 或属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627012/