3

I have some view and image view.

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

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

This imageView runs some animation.

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

}];

The problem is that after rotation animation stops. If i change autoresizing mask of the _contentView to flexible bounds animation continues after rotation. But i need it to be with flexible size. Can someone explain why animation stops after rotation?

4

3 回答 3

2

对于 2019 年..

这两个秘密是这样的:

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 将通过上述方式顺利处理。)

于 2019-07-25T19:53:06.410 回答
0

我有同样的问题,也许你使用这个空白?只需设置 [UIView setAnimationsEnabled:YES] 再次激活动画。我希望这个解决方案对您有所帮助。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [UIView setAnimationsEnabled:NO];
}
于 2018-03-08T22:11:05.117 回答
-1

在播放动画时禁用设备旋转,如果在动画期间旋转了设备,则在动画结束后旋转屏幕。这应该可以解决问题。

于 2013-02-21T16:24:56.373 回答