在视图控制器上放置无限的 uiview 对象数组

Place infinite array of uiview objects on view controller

下面 swift 代码的目标是在调用 pressBtn 函数时将单个黑色视图添加到屏幕中央。这段代码中的uiview叫做box。每次将框添加到屏幕中央时,它都应该连接到一个 uipangesture,这样它就可以四处移动,您可以在下面的 gif 中看到我在寻找什么。 gif的黑框被x替换了。

您可以使用函数创建每个视图:

private func getBlackView() -> UIView {
    let view = UIView()
    view.backgroundColor = .black
    let sideLength: CGFloat = 100
    view.frame = .init(x: self.view.bounds.midX - sideLength / 2,
                       y: self.view.bounds.midY - sideLength / 2,
                       width: sideLength,
                       height: sideLength)
    let recognizer = UIPanGestureRecognizer(target: self,
                                            action: #selector(moveView(_:)))
    view.addGestureRecognizer(recognizer)
    return view
}

然后,在此函数中创建 UIPanGestureRecognizer 并将其添加到每个新创建的视图中。这个识别器的 #selector 应该是:

@objc
private func moveView(_ recognizer: UIPanGestureRecognizer) {
    switch recognizer.state {
    case .began:
        print("gesture began")
    case .changed:
        let translation = recognizer.translation(in: self.view)

        recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                        y: recognizer.view!.center.y + translation.y)
        recognizer.setTranslation(.zero, in: self.view)
    default:
        break
    }
}

在这里,每次您识别出一个手势且状态为 .changed 时,您应该按当前 translation 移动您的视图并重置它。


示例:

public class MyViewController : UIViewController {
    public override func viewDidLoad() {
        super.viewDidLoad()
        button.frame = .init(x: self.view.bounds.midX,
                             y: 0,
                             width: 100,
                             height: 100)
        self.view.addSubview(button)
    }
    
    private lazy var button: UIButton = {
        let button = UIButton()
        button.backgroundColor = .blue
        button.setTitleColor(.white, for: .normal)
        button.setTitle("add", for: .normal)
        button.addTarget(self,
                         action: #selector(addBlackView),
                         for: .touchUpInside)
        return button
    }()
    
    private func getBlackView() -> UIView {
        let view = UIView()
        view.backgroundColor = .black
        let sideLength: CGFloat = 100
        view.frame = .init(x: self.view.bounds.midX - sideLength / 2,
                           y: self.view.bounds.midY - sideLength / 2,
                           width: sideLength,
                           height: sideLength)
        let recognizer = UIPanGestureRecognizer(target: self,
                                                action: #selector(moveView(_:)))
        view.addGestureRecognizer(recognizer)
        return view
    }
    
    @objc 
    private func addBlackView() {
        let view = getBlackView()
        self.view.addSubview(view)
    }
    
    @objc
    private func moveView(_ recognizer: UIPanGestureRecognizer) {
        switch recognizer.state {
        case .began:
            print("gesture began")
        case .changed:
            let translation = recognizer.translation(in: self.view)

            recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                            y: recognizer.view!.center.y + translation.y)
            recognizer.setTranslation(.zero, in: self.view)
        default:
            break
        }
    }
}