【问题标题】:UIView stops animation after Device rotationUIView 在设备旋转后停止动画
【发布时间】:2013-02-07 01:28:00
【问题描述】:

我有一些视图和图像视图。

_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 的自动调整大小掩码更改为灵活边界动画在旋转后继续。但我需要它具有灵活的尺寸。 谁能解释为什么动画在旋转后停止?

【问题讨论】:

    标签: ios objective-c uiview autoresizingmask


    【解决方案1】:

    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()
        })
    }
    

    删除秘密提示行,它根本不起作用 - 试试吧!

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

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

    【讨论】:

      【解决方案2】:

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

      -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
          [UIView setAnimationsEnabled:NO];
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我尝试通过正常的设备旋转找到解决方案或解释
        • 在设备旋转时尝试暂停,旋转完成后恢复。
        猜你喜欢
        • 2013-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多