UIView autorotation issue

烈酒焚心 提交于 2019-12-02 12:50:05

There are a few potential options:

  1. In animate(alongsideTransition:completion:), do a keyframe animation to set mid animation point so you don’t end up with that curious boxy feel mid animation. E.g. this shrink it down to a 4x4 box

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
    
        let origin = CGPoint(x: (view.bounds.midX + view.bounds.midY) / 2 - 2,
                             y: (view.bounds.midX + view.bounds.midY) / 2 - 2)
    
        coordinator.animate(alongsideTransition: { _ in
            UIView.animateKeyframes(withDuration: coordinator.transitionDuration, delay: 0, animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
                    self.myView.frame = CGRect(origin: origin, size: CGSize(width: 4, height: 4))
                }
    
                UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
                    self.layoutMyView()
                }
            })
        })
    }
    

    There are many possible variations on this theme. This still animates the view, but gives it a more “deliberate” sort of vibe and doesn’t lose the “line” feel of it.

  2. Rather than rendering a “line” with UIView, use CAShapeLayer and update its path.

  3. You could run a CADisplayLink while transition is in progress, and then update the frame to whatever you want as the animation progresses.

  4. Instead of animating the frame, you could animate the transform of this view, possibly rotating it in the opposite direction that the view is rotating.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!