我正在寻找最好的方法来反转两个UIView
的位置,如果可能的话动画(首先我需要改变位置,动画是可选的)。
viewController :
所以,我希望view1
用view2
来反转他的位置。 views
在init的故事板中使用autolayout设置。
if state == true {
view1 at the top
view2 at the bot
} else {
view 2 at the top
view 1 at the top
}
我试图从另一个视图中取出view.frame.(x, y, width, height)
并将其设置为视图,但它无法正常工作。
答案 0 :(得分:3)
我认为最好的方法是为连接到标题的两个视图设置topConstraint,然后更改它们的值并为过渡设置动画。你可以用类似的方式做到这一点:
class MyViewController: UIViewController {
var view1TopConstraint: NSLayoutConstraint!
var view2TopConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
view1TopConstraint = view1.topAnchor.constraintEqualToAnchor(header.bottomAnchor, constant: 0)
view1TopConstraint.isActive = true
view2TopConstraint = view2.topAnchor.constraintEqualToAnchor(header.bottomAnchor, constant: view1.frame.height)
view2TopConstraint.isActive = true
}
func changeView2ToTop() {
UIView.animateWithDuration(0.2, animations: { //this will animate the change between 2 and 1 where 2 is at the top now
self.view2TopConstraint.constant = 0
self.view1TopConstraint.constant = view2.frame.height
//or knowing that view2.frame.height represents 30% of the total view frame you can do like this as well
// self.view1TopConstraint.constant = view.frame.height * 0.3
self.view.layoutIfNeeded()
})
}
您还可以在故事板中创建NSLayoutConstraint并使用插座而不是我创建的变量,或者在故事板中设置两个视图的顶部约束,然后在构建时删除"如果你两个都做。这样你就不会有2个顶级约束而且没有约束变换
答案 1 :(得分:0)
我做了一个例子:https://dl.dropboxusercontent.com/u/24078452/MovingView.zip
我刚创建了一个包含3个视图,标题,视图1(红色)和视图2(黄色)的故事板。然后我添加了IBoutlets并在viewDid中显示它们动画,这里是代码:
import UIKit
class ViewController: UIViewController {
var position1: CGRect?
var position2: CGRect?
@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
position1 = view1.frame
position2 = view2.frame
UIView.animate(withDuration: 2.5) {
self.view1.frame = self.position2!
self.view2.frame = self.position1!
}
}
}
答案 2 :(得分:-1)
你好@Makaille我试着解决你的问题。 我已经做了一个例子,它将帮助您实现所需的实现。 点击此处:Source Code
我希望,它会帮助你。
@IBAction func toggle(_ sender: UIButton) {
sender.isUserInteractionEnabled = false
if(sender.isSelected){
let topPinConstantValue = layout_view2TopPin.constant
layout_view1TopPin.constant = topPinConstantValue
layout_view2TopPin.priority = 249
layout_view1BottomPin_to_View2Top.priority = 999
layout_view1TopPin.priority = 999
layout_view2BottomPin_ToView1Top.priority = 249
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
}, completion: { (value) in
if(value){
sender.isUserInteractionEnabled = true
}
})
} else {
let topPinConstantValue = layout_view1TopPin.constant
layout_view2TopPin.constant = topPinConstantValue
layout_view1TopPin.priority = 249
layout_view2BottomPin_ToView1Top.priority = 999
layout_view2TopPin.priority = 999
layout_view1BottomPin_to_View2Top.priority = 249
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
}, completion: { (value) in
if(value){
sender.isUserInteractionEnabled = true
}
})
}
sender.isSelected = !sender.isSelected
}