我有一些视图和图像视图。
_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的自动调整遮罩更改为灵活边界,则在旋转后继续动画。但我需要灵活的尺寸。 有人可以解释为什么动画在旋转后会停止吗?
答案 0 :(得分:1)
我实际上不知道为什么-这是完全神秘的-但这是事实。
它在其他任何地方都无法使用,例如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()
})
}
删除秘密提示行,它根本行不通-试试吧!
当屏幕出现时您开始..
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会通过上述操作顺利处理它。)
答案 1 :(得分:0)
在播放动画时和播放动画后禁用设备旋转,如果在动画期间旋转了设备,则旋转屏幕。这应该可以解决问题。
答案 2 :(得分:0)
I have the same problem, maybe u use this void? Just set [UIView setAnimationsEnabled:YES] for activate animation again. I hope this solution will be help to you.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[UIView setAnimationsEnabled:NO];
}