约束对象以防止对象更改大小时移动它们

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了约束对象以防止对象更改大小时移动它们相关的知识,希望对你有一定的参考价值。

正如您在下面的gif中所看到的,当幻灯片上深绿色对象的大小增加时,浅绿色对象将向南移动。我想做的是,当增加深绿色的对象大小时,浅绿色的对象不会移动,它保持在原位置。即使图像不动,也会发生相同的情况。

enter image description here

import UIKit

class ViewController: UIViewController {



    var picWidth: NSLayoutConstraint!
    var picHeight: NSLayoutConstraint!
    var pic = UIImageView()
    var slider = UISlider()
    var pic2 = UIImageView()
    var existingTransition : CGAffineTransform?

    var currentView: UIView?

    var g2 = UIPanGestureRecognizer()



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        pic.isUserInteractionEnabled = true

        g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
        pic.addGestureRecognizer(g2)

        pic.backgroundColor = .systemGreen
        pic2.backgroundColor = .green

        picWidth =  pic.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.2)
        picHeight =  pic.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.20)
        [pic,pic2,slider].forEach {

            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false

        }
        NSLayoutConstraint.activate ([
            pic.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
            picWidth,
            picHeight,
            pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0),

            pic2.topAnchor.constraint(equalTo: pic.bottomAnchor, constant : 0),
            pic2.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5, constant: 0),
            pic2.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4, constant: 0),
            pic2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),


            slider.topAnchor.constraint(equalTo: pic2.bottomAnchor, constant : 0),
            slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.20, constant: 0),
            slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.20, constant: 0),
            slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),


        ])

        slider.addTarget(self, action: #selector(hhh), for: .allEvents)


    }
    @objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
        currentView = gesture.view
    }

    @objc func g1Method(_ sender: UIPanGestureRecognizer){

        let subview = pic2
        guard let child = sender.view else{return}
        let transitionPoint = sender.translation(in: self.view)
        let newTransition = CGAffineTransform(translationX: transitionPoint.x, y: transitionPoint.y)
        switch sender.state {

        case .ended,.cancelled:// on End
            if let existing = existingTransition{
                self.existingTransition = newTransition.concatenating(existing)
            }else{
                self.existingTransition = newTransition
            }
        default://on change and other states
            if let existing = existingTransition{
                child.transform = newTransition
                    .concatenating(existing)
            }else{
                child.transform = newTransition
            }
        }
        self.view.layoutIfNeeded()


        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
        subview.addGestureRecognizer(tapGesture)


    }
    @objc func hhh() {


        picWidth.constant = CGFloat(slider.value) * view.frame.size.width * 0.25
        picHeight.constant = CGFloat(slider.value) * view.frame.size.height * 0.25

    }

}
答案

您的问题在这里:

pic2.topAnchor.constraint(equalTo: pic.bottomAnchor, constant : 0)

您正在告诉布局将pic2形状的顶部与pic形状的底部保持相等。

[要解决此问题,将pic2限制在顶部安全空间,然后将pic的高度限制为常数,以将pic2的顶部直接置于pic之下。

pic2.topAnchor.constraint(equalTo: view.topAnchor, constant : pic.bounds.height)

编辑:pic.bounds.height无法加载,因为您正在同时加载所有viewDidLoad中的约束,并且ViewController尚未放置子视图,因此,当调用pic时,您的pic.bounds.height为null 。为了解决此问题,您需要执行以下操作:

•在viewDidLoad中隔离图片初始化:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    pic.isUserInteractionEnabled = true

    g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
    pic.addGestureRecognizer(g2)

    pic.backgroundColor = .systemGreen
    pic2.backgroundColor = .green

    picWidth =  pic.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.2)
    picHeight =  pic.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.20)
    [pic,pic2,slider].forEach {

        view.addSubview($0)
        $0.translatesAutoresizingMaskIntoConstraints = false

    }
    NSLayoutConstraint.activate([pic.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
    picWidth,
    picHeight,
    pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0)])
    slider.addTarget(self, action: #selector(hhh), for: .allEvents)

}

•将其余的所有init放入viewDidLayoutSubviews:

override func viewDidLayoutSubviews() {
    NSLayoutConstraint.activate ([

        pic2.topAnchor.constraint(equalTo: view.topAnchor, constant : pic.bounds.height),
        pic2.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5, constant: 0),
        pic2.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4, constant: 0),
        pic2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),


        slider.topAnchor.constraint(equalTo: pic2.bottomAnchor, constant : 0),
        slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.20, constant: 0),
        slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.20, constant: 0),
        slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),


    ])
}

作为一个旁注,如果要使浅绿色方块移动,则滑块仍然会被弄乱,因为您将其限制在方块上。尝试根据视图的锚点而不是其他UI元素来限制对位于屏幕外部的对象的约束。

以上是关于约束对象以防止对象更改大小时移动它们的主要内容,如果未能解决你的问题,请参考以下文章

屏幕尺寸更改时保持图形轮廓

防止在更改字体粗细时移动文本[重复]

自动布局的 Xcode 故事板问题不允许我移动对象 origin.y 值

在解锁 oracle 上更改表约束

使用jQuery防止元素“捕获”鼠标?

在 ActionScript 中移动数组中的对象以产生体育场波浪效果