【问题标题】:Get titleLabel during drag and drop拖放过程中获取titleLabel
【发布时间】:2019-05-04 07:36:50
【问题描述】:

UIButton 数组以编程方式生成。是否可以获得触发拖动的 UIButton 的 titleLabel ?或者有什么方法可以在拖动功能中获取 UIButton 的信息?

override func viewDidLoad() {
    super.viewDidLoad()
    for q in question{
       addButton(title:q)
    }
}

func addButton(title:String){
        var tbutton: UIButton = {

            let button = UIButton(frame: CGRect(x: 0, y: 0,
                                                width: buttonWidth,
                                                height: buttonHeight))
            button.center = self.view.center
            button.layer.cornerRadius = 5
            button.layer.masksToBounds = true
            button.backgroundColor = UIColor.yellow
            button.setTitleColor(.black, for: .normal)
            button.setTitle(title, for: .normal)             


            return button
        }()
        view.addSubview(tbutton)
        tbutton.addTarget(self,
                          action: #selector(drag(control:event:)),
                          for: UIControl.Event.touchDragInside)
        tbutton.addTarget(self,
                          action: #selector(drag(control:event:)),
                          for: [UIControl.Event.touchDragExit,
                                UIControl.Event.touchDragOutside]) 
        self.buttonArray.append(tbutton)

    }



    @objc func drag(control: UIControl, event: UIEvent) {
        //print(event)
        if let center = event.allTouches?.first?.location(in: self.view) {
            control.center = center
        }

        /////////////////////////////////////////  
        // Get titleLabel of the button here???????????
        /////////////////////////////////////////
    }

【问题讨论】:

    标签: ios swift uibutton uikit drag


    【解决方案1】:
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let button = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
            button.backgroundColor = .yellow
            button.center = CGPoint.init(x: view.bounds.width / 2, y: view.bounds.height / 2)
            view.addSubview(button)
    
            let pangesture = UIPanGestureRecognizer.init(target: self, action: #selector(ViewController.panning(_:)))
            button.addGestureRecognizer(pangesture)
    
        }
    
        @objc func panning(_ panGesture : UIPanGestureRecognizer){
            let button = panGesture.view as! UIButton
            switch panGesture.state{
            case .changed:
                button.center = panGesture.location(in: view)
                panGesture.setTranslation(.zero, in: view)
            default:
                break
            }
    
            // here you can get the button's properties too.
        }
    }
    

    【讨论】:

    • if let center = event.allTouches?.first?.location(in: self.view) { control.center = center } 拖拽功能中这部分应该怎么更新?
    • @Stephen 我更新了我的答案。这次我们使用平移手势来进行拖动而不是内部按钮中的事件。实际上是一个有趣的问题.. 因为我在尝试将 UIButton 解包到 UIControl 及其事件时实际上被卡住了.. 但是使用平移手势拖动按钮的解决方案更加优雅,并且可以将按钮的功能(即单击)留给其他执行。希望对您有所帮助。
    • @Stephen 只是一个旁注。您可以在手势切换功能中为 .began 实现一个逻辑,这样当您单击按钮时,您的按钮将移动到该位置。它看起来会更流畅。但是这取决于您自己去探索
    • 如果你拖动多个按钮..请记住设置按钮的属性,isSelector或其他方式来识别按钮..并使用此属性将选定的按钮拖到一起。
    猜你喜欢
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多