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

Posted

技术标签:

【中文标题】在视图控制器上放置无限的uiview对象数组[关闭]【英文标题】:Place infinite array of uiview objects on view controller [closed] 【发布时间】:2020-11-03 05:04:48 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

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
        
    

【讨论】:

以上是关于在视图控制器上放置无限的uiview对象数组[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

关闭当前视图控制器时 UIView 框架发生变化

旋转放置和移动视图 (UIView)

iPhone UIView:是不是可以仅在子视图上启用用户交互?

子类化 UIView/UIViewController

如何以编程方式创建控制器和 UIView 而不使用界面构建器? [关闭]

nib 中的 UIView 在 iPad 上是 nil,在 iPhone 上不是 nil