Ios 旋转事件发生时无法修复自动布局动画
不要害怕后面的庞大代码。您可以将代码段复制并粘贴到新的单视图应用程序中,以查看其行为。该问题位于与旋转动画一起执行的动画的完成块内的某个位置Ios 旋转事件发生时无法修复自动布局动画,ios,swift,animation,autolayout,Ios,Swift,Animation,Autolayout,不要害怕后面的庞大代码。您可以将代码段复制并粘贴到新的单视图应用程序中,以查看其行为。该问题位于与旋转动画一起执行的动画的完成块内的某个位置 import UIKit let sizeConstant: CGFloat = 60 class ViewController: UIViewController { let topView = UIView() let backgroundView = UIView() let stackView = UIStackVie
import UIKit
let sizeConstant: CGFloat = 60
class ViewController: UIViewController {
let topView = UIView()
let backgroundView = UIView()
let stackView = UIStackView()
let lLayoutGuide = UILayoutGuide()
let bLayoutGuide = UILayoutGuide()
var bottomConstraints = [NSLayoutConstraint]()
var leftConstraints = [NSLayoutConstraint]()
var bLayoutHeightConstraint: NSLayoutConstraint!
var lLayoutWidthConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
print(UIScreen.main.bounds)
// self.view.layer.masksToBounds = true
let views = [
UIButton(type: .infoDark),
UIButton(type: .contactAdd),
UIButton(type: .detailDisclosure)
]
views.forEach(self.stackView.addArrangedSubview)
self.backgroundView.backgroundColor = UIColor.red
self.backgroundView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.backgroundView)
self.topView.backgroundColor = UIColor.green
self.topView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.topView)
self.stackView.axis = isPortrait() ? .horizontal : .vertical
self.stackView.distribution = .fillEqually
self.stackView.translatesAutoresizingMaskIntoConstraints = false
self.backgroundView.addSubview(self.stackView)
self.topView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true
self.topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.topView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
self.topView.heightAnchor.constraint(equalToConstant: 46).isActive = true
self.view.addLayoutGuide(self.lLayoutGuide)
self.view.addLayoutGuide(self.bLayoutGuide)
self.bLayoutGuide.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.bLayoutGuide.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.bLayoutGuide.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
self.bLayoutHeightConstraint = self.bLayoutGuide.heightAnchor.constraint(equalToConstant: isPortrait() ? sizeConstant : 0)
self.bLayoutHeightConstraint.isActive = true
self.lLayoutGuide.topAnchor.constraint(equalTo: self.topView.bottomAnchor).isActive = true
self.lLayoutGuide.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.lLayoutGuide.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.lLayoutWidthConstraint = self.lLayoutGuide.widthAnchor.constraint(equalToConstant: isPortrait() ? 0 : sizeConstant)
self.lLayoutWidthConstraint.isActive = true
self.stackView.topAnchor.constraint(equalTo: self.backgroundView.topAnchor).isActive = true
self.stackView.bottomAnchor.constraint(equalTo: self.backgroundView.bottomAnchor).isActive = true
self.stackView.leadingAnchor.constraint(equalTo: self.backgroundView.leadingAnchor).isActive = true
self.stackView.trailingAnchor.constraint(equalTo: self.backgroundView.trailingAnchor).isActive = true
self.bottomConstraints = [
self.backgroundView.topAnchor.constraint(equalTo: self.bLayoutGuide.topAnchor),
self.backgroundView.leadingAnchor.constraint(equalTo: self.bLayoutGuide.leadingAnchor),
self.backgroundView.trailingAnchor.constraint(equalTo: self.bLayoutGuide.trailingAnchor),
self.backgroundView.heightAnchor.constraint(equalToConstant: sizeConstant)
]
self.leftConstraints = [
self.backgroundView.topAnchor.constraint(equalTo: self.lLayoutGuide.topAnchor),
self.backgroundView.bottomAnchor.constraint(equalTo: self.lLayoutGuide.bottomAnchor),
self.backgroundView.trailingAnchor.constraint(equalTo: self.lLayoutGuide.trailingAnchor),
self.backgroundView.widthAnchor.constraint(equalToConstant: sizeConstant)
]
if isPortrait() {
NSLayoutConstraint.activate(self.bottomConstraints)
} else {
NSLayoutConstraint.activate(self.leftConstraints)
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let willBePortrait = size.width < size.height
coordinator.animate(alongsideTransition: {
context in
let halfDuration = context.transitionDuration / 2.0
UIView.animate(withDuration: halfDuration, delay: 0, options: .overrideInheritedDuration, animations: {
self.bLayoutHeightConstraint.constant = 0
self.lLayoutWidthConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: {
_ in
// HERE IS THE ISSUE!
// Putting this inside `performWithoutAnimation` did not helped
if willBePortrait {
self.stackView.axis = .horizontal
NSLayoutConstraint.deactivate(self.leftConstraints)
NSLayoutConstraint.activate(self.bottomConstraints)
} else {
self.stackView.axis = .vertical
NSLayoutConstraint.deactivate(self.bottomConstraints)
NSLayoutConstraint.activate(self.leftConstraints)
}
self.view.layoutIfNeeded()
UIView.animate(withDuration: halfDuration) {
if willBePortrait {
self.bLayoutHeightConstraint.constant = sizeConstant
} else {
self.lLayoutWidthConstraint.constant = sizeConstant
}
self.view.layoutIfNeeded()
}
})
})
super.viewWillTransition(to: size, with: coordinator)
}
func isPortrait() -> Bool {
let size = UIScreen.main.bounds.size
return size.width < size.height
}
}
导入UIKit
设sizeConstant:CGFloat=60
类ViewController:UIViewController{
让topView=UIView()
让backgroundView=UIView()
让stackView=UIStackView()
让lLayoutGuide=UILayoutGuide()
让bLayoutGuide=UILayoutGuide()
var bottomConstraints=[NSLayoutConstraint]()
var leftConstraints=[NSLayoutConstraint]()
var bLayoutHeightConstraint:NSLayoutConstraint!
var lLayoutWidthConstraint:NSLayoutConstraint!
重写func viewDidLoad(){
super.viewDidLoad()
打印(UIScreen.main.bounds)
//self.view.layer.masksToBounds=true
让视图=[
UIButton(类型:.infoDark),
UIButton(类型:。contactAdd),
UIButton(类型:。详细信息披露)
]
views.forEach(self.stackView.addArrangedSubview)
self.backgroundView.backgroundColor=UIColor.red
self.backgroundView.translates自动调整大小gmaskintoConstraints=false
self.view.addSubview(self.backgroundView)
self.topView.backgroundColor=UIColor.green
self.topView.translatesAutoresizingMaskIntoConstraints=false
self.view.addSubview(self.topView)
self.stackView.axis=isPortrait()?。水平:。垂直
self.stackView.distribution=.fillview
self.stackView.translatesAutoResizengMaskintoConstraints=false
self.backgroundView.addSubview(self.stackView)
self.topView.topAnchor.constraint(等式:self.topLayoutGuide.bottomAnchor).isActive=true
self.topView.leadingAnchor.constraint(等式:self.view.leadingAnchor).isActive=true
self.topView.trailingAnchor.constraint(equalTo:self.view.trailingAnchor).isActive=true
self.topView.heightAnchor.constraint(equalToConstant:46)。isActive=true
self.view.addLayoutGuide(self.layoutGuide)
self.view.addLayoutGuide(self.bLayoutGuide)
self.bLayoutGuide.bottomAnchor.constraint(equalTo:self.view.bottomAnchor).isActive=true
self.bLayoutGuide.leadingAnchor.constraint(等式:self.view.leadingAnchor).isActive=true
self.bLayoutGuide.trailingAnchor.constraint(equalTo:self.view.trailingAnchor).isActive=true
self.bLayoutHeightConstraint=self.bLayoutGuide.heightAnchor.constraint(equalToConstant:isPortrait()?sizeConstant:0)
self.bLayoutHeightConstraint.isActive=true
self.lLayoutGuide.topAnchor.constraint(等式:self.topView.bottomAnchor).isActive=true
self.lLayoutGuide.bottomAnchor.constraint(equalTo:self.view.bottomAnchor).isActive=true
self.lLayoutGuide.leadingAnchor.constraint(等式:self.view.leadingAnchor).isActive=true
self.lLayoutWidthConstraint=self.lLayoutGuide.widthAnchor.constraint(equalToConstant:isPortrait()?0:sizeConstant)
self.lLayoutWidthConstraint.isActive=true
self.stackView.topanch.constraint(equalTo:self.backgroundView.topanch).isActive=true
self.stackView.bottomAnchor.constraint(equalTo:self.backgroundView.bottomAnchor).isActive=true
self.stackView.leadingAnchor.constraint(等式:self.backgroundView.leadingAnchor).isActive=true
self.stackView.trailingAnchor.constraint(等式:self.backgroundView.trailingAnchor).isActive=true
self.bottom约束=[
self.backgroundView.topAnchor.constraint(等式:self.bLayoutGuide.topAnchor),
self.backgroundView.leadingAnchor.constraint(等式:self.bLayoutGuide.leadingAnchor),
self.backgroundView.trailingAnchor.constraint(等式:self.bLayoutGuide.trailingAnchor),
self.backgroundView.heightAnchor.constraint(equalToConstant:sizeConstant)
]
self.leftConstraints=[
self.backgroundView.topAnchor.constraint(等式:self.lLayoutGuide.topAnchor),
self.backgroundView.bottomAnchor.constraint(等式:self.lLayoutGuide.bottomAnchor),
self.backgroundView.trailingAnchor.constraint(等式:self.lLayoutGuide.trailingAnchor),
self.backgroundView.widthAnchor.constraint(equalToConstant:sizeConstant)
]
如果isPortrait(){
NSLayoutConstraint.activate(self.bottomConstraints)
}否则{
NSLayoutConstraint.activate(self.leftConstraints)
}
}
重写func viewWillTransition(到大小:CGSize,带协调器:UIViewControllerTransitionCoordinator){
让willbe纵向=size.widthoverride func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let willBePortrait = size.width < size.height
if willBePortrait {
self.stackView.axis = .horizontal
NSLayoutConstraint.deactivate(self.leftConstraints)
NSLayoutConstraint.activate(self.bottomConstraints)
} else {
self.stackView.axis = .vertical
NSLayoutConstraint.deactivate(self.bottomConstraints)
NSLayoutConstraint.activate(self.leftConstraints)
}
self.view.layoutIfNeeded()
coordinator.animate(alongsideTransition: { context in
let halfDuration = context.transitionDuration / 2
UIView.animate(withDuration: halfDuration, animations: {
if willBePortrait {
self.lLayoutWidthConstraint.constant = 0
} else {
self.bLayoutHeightConstraint.constant = 0
}
})
UIView.animate(withDuration: halfDuration, delay: halfDuration, animations: {
if willBePortrait {
self.bLayoutHeightConstraint.constant = sizeConstant
} else {
self.lLayoutWidthConstraint.constant = sizeConstant
}
self.view.layoutIfNeeded()
})
})
super.viewWillTransition(to: size, with: coordinator)
}