【问题标题】:Is there a way to add subviews to Xcode without reseting the position of the last view added?有没有办法在不重置添加的最后一个视图的位置的情况下向 Xcode 添加子视图?
【发布时间】:2020-09-06 10:10:12
【问题描述】:

这是我的代码:

let newView = ImageView()
    newView.translatesAutoresizingMaskIntoConstraints = false
    newView.backgroundColor = .random()
    view.addSubview(newView)

    newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

    let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    panGR.delegate = self
    newView.addGestureRecognizer(panGR)

    let pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(pinch:)))
    pinchGR.delegate = self
    newView.addGestureRecognizer(pinchGR)

    let rotateGR = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(rotate:)))
    rotateGR.delegate = self
    newView.addGestureRecognizer(rotateGR)

每次按下按钮时,它都会运行此代码。但是,所有子视图都在不断重置它们的位置。

这是“handlePan”的代码:

@objc func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
        let translation = gestureRecognizer.translation(in: self.view)
        gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)

        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)


    }
}

【问题讨论】:

  • 可以把代码分享到handlePan吗?
  • 您也应该发布其他手势识别器操作方法的代码。
  • 我在上面加了

标签: ios swift xcode addsubview


【解决方案1】:

因为您在自定义视图创建时设置了约束。

将自定义视图添加到超级视图后,您可以通过拖动更改其位置 - 但您只需更改 center 属性即可。好的。

当您向您的超级视图添加新的自定义视图时 - 将执行布局子视图。并且您所有的子视图位置都将根据初始约束进行重置。

你需要:

  • 不要在子视图中使用约束
  • 在您的handlePan(_ gestureRecognizer : UIPanGestureRecognizer) 函数更改中 centerXAnchor,centerYAnchor 约束值,不是中心属性。因为在任何布局子视图之后,它们的位置都会被设置为原来的。

    let newView = UIImageView.init(frame: .init(x: 0, y: 0, width: 100, height: 100))
    newView.translatesAutoresizingMaskIntoConstraints = false
    newView.isUserInteractionEnabled = true
    newView.backgroundColor = UIColor.random
    view.addSubview(newView)
    
    // newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    // newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    // newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    // newView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    
    let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    panGR.delegate = self
    newView.addGestureRecognizer(panGR)
    

这段代码运行良好。

有约束的版本:

@IBAction func addView() {
    let newView = CustomView.init()
    newView.translatesAutoresizingMaskIntoConstraints = false
    newView.isUserInteractionEnabled = true
    newView.backgroundColor = UIColor.random
    view.addSubview(newView)

    newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    newView.centerXConstraint = newView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    newView.centerYConstraint = newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    newView.centerXConstraint.isActive = true
    newView.centerYConstraint.isActive = true

    let panGR = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    panGR.delegate = self
    newView.addGestureRecognizer(panGR)
}

@objc func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed,
        let view = gestureRecognizer.view as? CustomView,
        let centerXConstraint = view.centerXConstraint,
        let centerYConstraint = view.centerYConstraint {
        let translation = gestureRecognizer.translation(in: self.view)
        let x = centerXConstraint.constant + translation.x
        let y = centerYConstraint.constant + translation.y
        centerXConstraint.constant = x
        centerYConstraint.constant = y
        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
    }
}

在您的自定义类中添加约束变量

class CustomView: UIView {
    var centerXConstraint: NSLayoutConstraint!
    var centerYConstraint: NSLayoutConstraint!

}

【讨论】:

  • 移除约束并不能解决问题。所有视图都在左上角添加和重置。
  • 同时移除高度和宽度限制
  • 不过有一个问题。使用自定义视图,它根本不会显示。
  • 你的意思是,与 UIImageView 一起工作,但不与你的 ImageView 一起工作?或者您尝试将这些视图添加到另一个视图,而不是 self.view?
  • 它适用于 UIImageView 但不适用于我的自定义 ImageView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 2018-12-02
  • 2016-06-10
  • 1970-01-01
  • 2011-06-28
  • 2013-11-29
相关资源
最近更新 更多