Swift - 检测UIView的触摸坐标

时间:2018-04-15 19:54:59

标签: swift

我正在尝试创建一个可以附加到UITableView的自定义跳转栏。

Jumpbar picture

我现在想要实现的是如果用户触摸A并滑过Z,我想打印出A,B,C,D ......,Z。目前,它只打印A,A,A ... A 。无论如何我能实现吗?

每个字母都是UIView的UIButton子视图。

class TableViewJumpBar{
let tableView: UITableView
let view: UIView
let jumpBar: UIView!
private var jumpIndexes: [Character]

init(tableView: UITableView, view: UIView){
    self.view = view

    self.tableView = tableView
    jumpBar = UIView(frame: .zero)
    jumpBar.backgroundColor = UIColor.gray

    let aScalars = "A".unicodeScalars
    let aCode = aScalars[aScalars.startIndex].value
    jumpIndexes = (0..<26).map {
        i in Character(UnicodeScalar(aCode + i)!)
    }
}

func setFrame(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat){
    guard jumpIndexes.count > 0 else{
        print("Jump indexes cannot be empty")
        return
    }

    //Remove jumpbar and remove all subviews
    jumpBar.removeFromSuperview()
    for subView in jumpBar.subviews{
        subView.removeFromSuperview()
    }

    jumpBar.frame = CGRect(x: x, y: y, width: width, height: height)

    let height = height/CGFloat(jumpIndexes.count)
    for i in 0..<jumpIndexes.count{
        let indexButton = UIButton(frame: CGRect(x:CGFloat(0.0), y: CGFloat(i)*height, width: width, height: height))
        indexButton.setTitle(String(jumpIndexes[i]), for: .normal)
        indexButton.addTarget(self, action: #selector(jumpIndexButtonTouched(_:)), for: .allEvents)
        jumpBar.addSubview(indexButton)
    }

    self.view.addSubview(jumpBar)
}

///Touch has been begun
@objc private func jumpIndexButtonTouched(_ sender: UIButton!){
    print(sender.titleLabel?.text)
}

1 个答案:

答案 0 :(得分:1)

此代码可以工作并打印开始和结束字符。

class ViewController: UIViewController {

    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var myView: UIView!
    var startChar = ""
    var finishChar = ""


    override func viewDidLoad() {
        super.viewDidLoad()
        let table = TableViewJumpBar(tableView: myTableView, view: myView)
        table.setFrame(x: 0, y: 0, width: 100, height: self.view.frame.size.height)
        print(myView.subviews[0].subviews)
        setPanGesture()
    }

    func setPanGesture() {
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panRecognized))
        self.myView.addGestureRecognizer(pan)
    }

    @objc func panRecognized(sender: UIPanGestureRecognizer) {
        let location = sender.location(in: myView)
        if sender.state == .began {
            for button in myView.subviews[0].subviews {
                if let button = button as? UIButton, button.frame.contains(location), let startCharacter = button.titleLabel?.text {
                    self.startChar = startCharacter
                }
            }
        } else if sender.state == .ended {
            for button in myView.subviews[0].subviews {
                if let button = button as? UIButton, button.frame.contains(location), let finishCharacter = button.titleLabel?.text {
                    self.finishChar = finishCharacter
                }
            }
            print("start with \(startChar), finish with \(finishChar)")
        }
    }

} 

在你的代码中我删除了Button动作。你可以使用标签而不是按钮。

class TableViewJumpBar {
    let tableView: UITableView
    let view: UIView
    let jumpBar: UIView!
    private var jumpIndexes: [Character]

    init(tableView: UITableView, view: UIView){
        self.view = view

        self.tableView = tableView
        jumpBar = UIView(frame: .zero)
        jumpBar.backgroundColor = UIColor.gray

        let aScalars = "A".unicodeScalars
        let aCode = aScalars[aScalars.startIndex].value
        jumpIndexes = (0..<26).map {
            i in Character(UnicodeScalar(aCode + i)!)
        }
    }

    func setFrame(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat){
        guard jumpIndexes.count > 0 else{
            print("Jump indexes cannot be empty")
            return
        }
        jumpBar.removeFromSuperview()
        for subView in jumpBar.subviews{
            subView.removeFromSuperview()
        }

        jumpBar.frame = CGRect(x: x, y: y, width: width, height: height)

        let height = height/CGFloat(jumpIndexes.count)
        for i in 0..<jumpIndexes.count{
            let indexButton = UIButton(frame: CGRect(x:CGFloat(0.0), y: CGFloat(i)*height, width: width, height: height))
            indexButton.setTitle(String(jumpIndexes[i]), for: .normal)
            jumpBar.addSubview(indexButton)
        }
        self.view.addSubview(jumpBar)
    }

}   

一些输出结果:
ScreenShot