gpt4 book ai didi

ios - UIView 自动旋转问题

转载 作者:行者123 更新时间:2023-11-28 23:38:14 25 4
gpt4 key购买 nike

我在 UIView 的自动旋转中遇到问题,它只是一条红线,宽度为 4,高度是横向模式下 superview 高度的一半,而在纵向模式下,高度是 4 点,宽度是 superview 宽度的一半。你可以在 gif 中看到这个问题。当我从横向旋转到纵向时,自动旋转的效果不平滑并且有扭曲。

enter image description here

这是代码。我做错了什么?

private var myView:UIView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

myView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width/2, height: 4))
myView.backgroundColor = UIColor.red
view.addSubview(myView)

myView.center = CGPoint(x: view.bounds.width/2, y: view.bounds.height/2)


}

private func layoutMyView() {

let orientation = UIApplication.shared.statusBarOrientation

if orientation == .landscapeLeft || orientation == .landscapeRight {
myView.frame = CGRect(x: 0, y: 0, width: view.bounds.width/2, height: 4)
} else {
myView.frame = CGRect(x: 0, y: 0, width: 4, height: view.bounds.height/2)
}

myView.center = CGPoint(x: view.bounds.width/2, y: view.bounds.height/2)
}


override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

let orientation = UIApplication.shared.statusBarOrientation

NSLog("View will transition to \(size), \(orientation.rawValue)")

if size.width < size.height {
NSLog("Portrait mode")
} else {
NSLog("Landscape mode")
}

super.viewWillTransition(to: size, with: coordinator)

coordinator.animate(alongsideTransition: { [unowned self] (_) in

let orient = UIApplication.shared.statusBarOrientation

self.layoutMyView()

}, completion: { [unowned self] (UIViewControllerTransitionCoordinatorContext) -> Void in

let orient = UIApplication.shared.statusBarOrientation
self.layoutMyView()
NSLog("rotation completed to \(orient.rawValue), \(self.myView.frame)")
})
}

最佳答案

有几个可能的选择:

  1. animate(alongsideTransition:completion:) 中,做一个关键帧动画来设置中间动画点,这样你就不会以那种奇怪的方形感觉结束动画。例如。这将它缩小到一个 4x4 的盒子

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

    这个主题有很多可能的变体。这仍然使 View 充满活力,但赋予它一种更“深思熟虑”的氛围,并且不会失去它的“线条”感觉。

    enter image description here

  2. 与其使用 UIView 渲染“线”,不如使用 CAShapeLayer并更新其 path .

  3. 你可以运行 CADisplayLink当过渡正在进行时,然后随着动画的进行将帧更新为您想要的任何内容。

  4. 您可以为该 View 的 transform 设置动画,而不是为 frame 设置动画,可以将其旋转到与 View 旋转相反的方向。

关于ios - UIView 自动旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389933/

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