Ios 通过滑动更改自动布局限制
屏幕上有两张图片,它们是通过编程设置的限制。在向左滑动时,图片的大小会发生变化,并变成它们应该的样子。如果在这之后,向右滑动,则什么也不会发生,图片仍保留在原来的位置。 下面是实现代码Ios 通过滑动更改自动布局限制,ios,swift,animation,resize,constraints,Ios,Swift,Animation,Resize,Constraints,屏幕上有两张图片,它们是通过编程设置的限制。在向左滑动时,图片的大小会发生变化,并变成它们应该的样子。如果在这之后,向右滑动,则什么也不会发生,图片仍保留在原来的位置。 下面是实现代码 func Swipe() { let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture)) swipeLeft.direction = .left s
func Swipe() {
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == .right {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations:{
self.setupLayoutImageOne()
}, completion: nil)
self.view.layoutIfNeeded()
} else if gesture.direction == .left {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations:{
self.setupLayoutImageTwo()
}, completion: nil)
self.view.layoutIfNeeded()
}
}
func setupLayoutImageOne()
{
imageOne.translatesAutoresizingMaskIntoConstraints = false
imageTwo.translatesAutoresizingMaskIntoConstraints = false
let layoutGuide = view
NSLayoutConstraint.activate([
imageOne.topAnchor.constraint(equalTo: layoutGuide!.topAnchor),
imageOne.leadingAnchor.constraint(equalTo: layoutGuide!.leadingAnchor),
imageOne.trailingAnchor.constraint(equalTo: layoutGuide!.trailingAnchor,constant: 170) ,
imageOne.bottomAnchor.constraint(equalTo: layoutGuide!.bottomAnchor,constant: 290),
imageTwo.bottomAnchor.constraint(equalTo: layoutGuide!.bottomAnchor,constant: 530),
imageTwo.leadingAnchor.constraint(equalTo: layoutGuide!.leadingAnchor,constant: 200),
imageTwo.trailingAnchor.constraint(equalTo: layoutGuide!.trailingAnchor,constant: 200),
imageTwo.topAnchor.constraint(equalTo: layoutGuide!.topAnchor,constant: 800),
])
}
func setupLayoutImageTwo()
{
imageOne.translatesAutoresizingMaskIntoConstraints = false
imageTwo.translatesAutoresizingMaskIntoConstraints = false
let layoutGuide = view
NSLayoutConstraint.activate([
imageOne.topAnchor.constraint(equalTo: layoutGuide!.topAnchor),
imageOne.leadingAnchor.constraint(equalTo: layoutGuide!.leadingAnchor),
imageOne.trailingAnchor.constraint(equalTo: layoutGuide!.trailingAnchor,constant: 299) ,
imageOne.bottomAnchor.constraint(equalTo: layoutGuide!.bottomAnchor,constant: 570),
imageTwo.bottomAnchor.constraint(equalTo: layoutGuide!.bottomAnchor),
imageTwo.leadingAnchor.constraint(equalTo: layoutGuide!.leadingAnchor),
imageTwo.trailingAnchor.constraint(equalTo: layoutGuide!.trailingAnchor),
imageTwo.topAnchor.constraint(equalTo: layoutGuide!.topAnchor),
])
}
当我向右滑动时,它会显示这样的错误
将尝试通过打破约束进行恢复
活动,名称:'|':UIView:0x10080e7a0>
在UIViewAlertForUnsatifiableConstraints处创建符号断点
要在调试器中捕获此信息。研究方法
中列出的UIView上的UIConstraintBasedLayoutDebugging类别
也可能有帮助。致命错误:意外
隐式展开可选值2020-01-28时发现nil
15:09:59.920450+0200 ThemeGame[27748:2857822]致命错误:
隐式展开可选值时意外发现nil
由于您已经发布了几个非常类似的问题,我强烈建议您花一些时间学习约束和自动布局是如何工作的 imageView框架看起来相当奇怪,例如这一行:
imageOne.bottomAnchor.constraint(equalTo: layoutGuide!.bottomAnchor,constant: 290)
将imageView的底部290点置于视图底部下方,因此290点像素将脱离屏幕
然而,这里有一种基于您发布的代码的方法。我们为每个imageView的左滑动位置/大小和右滑动位置/大小定义约束。我们将这些约束存储在数组中,然后根据需要激活/停用这些数组:
class SwipeViewController: UIViewController {
let imageOne: UIImageView = {
let v = UIImageView()
v.backgroundColor = .red
return v
}()
let imageTwo: UIImageView = {
let v = UIImageView()
v.backgroundColor = .green
return v
}()
// imageOne constraints when swiping Left
var imageOneLeftConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
// imageOne constraints when swiping Right
var imageOneRightConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
// imageTwo constraints when swiping Left
var imageTwoLeftConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
// imageTwo constraints when swiping Right
var imageTwoRightConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
imageOne.translatesAutoresizingMaskIntoConstraints = false
imageTwo.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageOne)
view.addSubview(imageTwo)
guard let layoutGuide = view else { fatalError("this should not fail") }
// local constraint var to reuse
var c: NSLayoutConstraint
// define constraints for imageOne when swiping left
c = imageOne.topAnchor.constraint(equalTo: layoutGuide.topAnchor)
imageOneLeftConstraints.append(c)
c = imageOne.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor)
imageOneLeftConstraints.append(c)
c = imageOne.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor,constant: 170)
imageOneLeftConstraints.append(c)
c = imageOne.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor,constant: 290)
imageOneLeftConstraints.append(c)
// define constraints for imageTwo when swiping left
c = imageTwo.topAnchor.constraint(equalTo: layoutGuide.topAnchor,constant: 800)
imageTwoLeftConstraints.append(c)
c = imageTwo.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor,constant: 200)
imageTwoLeftConstraints.append(c)
c = imageTwo.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor,constant: 530)
imageTwoLeftConstraints.append(c)
c = imageTwo.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor,constant: 200)
imageTwoLeftConstraints.append(c)
// define constraints for imageOne when swiping right
c = imageOne.topAnchor.constraint(equalTo: layoutGuide.topAnchor)
imageOneRightConstraints.append(c)
c = imageOne.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor)
imageOneRightConstraints.append(c)
c = imageOne.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor,constant: 299)
imageOneRightConstraints.append(c)
c = imageOne.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor,constant: 570)
imageOneRightConstraints.append(c)
// define constraints for imageTwo when swiping right
c = imageTwo.topAnchor.constraint(equalTo: layoutGuide.topAnchor)
imageTwoRightConstraints.append(c)
c = imageTwo.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor)
imageTwoRightConstraints.append(c)
c = imageTwo.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor)
imageTwoRightConstraints.append(c)
c = imageTwo.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor)
imageTwoRightConstraints.append(c)
// start with imageViews at "swiped left" positions
NSLayoutConstraint.activate(imageOneLeftConstraints + imageTwoLeftConstraints)
setupSwipe()
}
func setupSwipe() {
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == .right {
NSLayoutConstraint.deactivate(imageOneLeftConstraints + imageTwoLeftConstraints)
NSLayoutConstraint.activate(imageOneRightConstraints + imageTwoRightConstraints)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations:{
self.view.layoutIfNeeded()
}, completion: nil)
} else if gesture.direction == .left {
NSLayoutConstraint.deactivate(imageOneRightConstraints + imageTwoRightConstraints)
NSLayoutConstraint.activate(imageOneLeftConstraints + imageTwoLeftConstraints)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations:{
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
您不能继续添加不同的约束,因为您会遇到冲突。您可以创建一组约束并更改其.constant值,例如,或者您需要在不希望激活的约束上设置.isActive=false,在确实希望激活的约束上设置.isActive=true。顺便说一下。。。我们刚才不是讨论了如何正确地做到这一点吗:???是的,你是对的。现在我将重写一些代码,以便您可以禁用属性isActive@DonMag在分配新的限制之前,我尝试重置这些限制。但结果是sameThank你太多了!我会尽可能努力研究这个话题,以免出现类似的情况。