gpt4 book ai didi

ios - UIView 在设备旋转后停止动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:05 25 4
gpt4 key购买 nike

我有一些 View 和 ImageView 。

_contentView = [[UIView alloc] initWithFrame:self.bounds];
_contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
....

UIImageView *imageView = [[UIImageView alloc] initWithImage:someImage];
[_contentView addSubview:imageView];

这个 imageView 运行一些动画。

[UIView animateWithDuration:1.6f delay:0.0 options:UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
imageView.center = somePoint;
} completion:^(BOOL finished) {

}];

问题是旋转动画停止后。如果我将 _contentView 的自动调整大小掩码更改为灵活边界,则动画在旋转后继续。但我需要它具有灵活的大小。有人可以解释为什么动画在旋转后停止吗?

最佳答案

2019年..

两个 secret 是:

1。您必须在重新启动动画之前重置该值

我其实不知道为什么 - 这完全是个谜 - 但这是事实。

2。您必须在协调器完成时重新启动它。

它在其他任何地方都不起作用,例如在 willTransition 的主体中。

所以...

你有一个动画约束..

@IBOutlet var slider: NSLayoutConstraint!

(假设初始值为“55”)

你不断地为它制作动画以制作一个环绕卷轴,比方说:

func slide() {
let w = sliderView.frame.width

////////// SECRET TIP ------->
slider.constant = 55
////////// <--------SECRET TIP

view.layoutIfNeeded()
UIView.animate(withDuration: 10, delay: 0,
options: [.repeat, .curveLinear, .beginFromCurrentState],
animations:{ [weak self] in
self?.slider.constant = w
self?.view.layoutIfNeeded()
})
}

删除 Secret Tip 行,它根本不起作用 - 试试吧!

当屏幕出现时你就开始了..

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
slide()
}

当屏幕旋转时,你继续动画:

override func willTransition(to newCollection: UITraitCollection,
with coordinator: UIViewControllerTransitionCoordinator) {
super.willTransition(to: newCollection, with: coordinator)
coordinator.animate(alongsideTransition: { _ in
},completion: { [weak self] _ in
self?.slide()
})
}

就是这样!

(请注意,实际上您不需要取消、暂停或以其他方式操作您的动画:iOS 将通过上述操作顺利处理。)

关于ios - UIView 在设备旋转后停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15007073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com