UIPanGestureRecognizer不记得平底锅的开始位置

时间:2018-01-31 13:31:42

标签: ios swift uipangesturerecognizer

我有一个UIView的自定义子类,它有一个UIPanGestureRecognizer以及处理它所需的所有相关内容。这个想法是,根据我的观点中的平移开始,不同的事情会发生。此外,我必须稍后跟踪平移手势的当前位置。我该如何正确设置呢?

这是我的代码现在的样子:

import UIKit

@IBDesignable class RangeSelectorView: UIView {

private let trackWidth: CGFloat = 3.0
private let thumbRadius: CGFloat = 10.0
private let trackOffset: CGFloat = 16

@IBInspectable let min: Int = 0
@IBInspectable let max: Int = 100
@IBInspectable var lowerThumbValue: Int = 0
@IBInspectable var upperThumbValue: Int = 100

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

func setup() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(trackTapped))
    addGestureRecognizer(tapGestureRecognizer)
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(thumbDragged))
    addGestureRecognizer(panGestureRecognizer)
}

override func draw(_ rect: CGRect) {
    if let trackContext = UIGraphicsGetCurrentContext() {
        trackContext.setLineWidth(trackWidth)
        trackContext.setStrokeColor(UIColor.lightGray.cgColor)
        trackContext.move(to: CGPoint(x: trackOffset, y: self.frame.height / 2))
        trackContext.addLine(to: CGPoint(x: self.frame.width
             - trackOffset, y: self.frame.height / 2))
        trackContext.strokePath()
    }
    if let thumbContext = UIGraphicsGetCurrentContext() {
        let lowerRect = CGRect(x: trackOffset + getRelativeThumbPosition(for: lowerThumbValue) - thumbRadius, y: self.frame.height / 2 - thumbRadius, width: thumbRadius * 2, height: thumbRadius * 2)
        let upperRect = CGRect(x: trackOffset + getRelativeThumbPosition(for: upperThumbValue) - thumbRadius, y: self.frame.height / 2 - thumbRadius, width: thumbRadius * 2, height: thumbRadius * 2)
        thumbContext.addEllipse(in: lowerRect)
        thumbContext.addEllipse(in: upperRect)
        thumbContext.setFillColor(Constants.Colors.titleBlue.cgColor)
        thumbContext.fillPath()
    }
    if let highlightedRangeContext = UIGraphicsGetCurrentContext() {
        highlightedRangeContext.setLineWidth(trackWidth)
        highlightedRangeContext.setStrokeColor(Constants.Colors.titleBlue.cgColor)
        highlightedRangeContext.move(to: CGPoint(x: trackOffset + getRelativeThumbPosition(for: lowerThumbValue) + thumbRadius, y: self.frame.height / 2))
        highlightedRangeContext.addLine(to: CGPoint(x: trackOffset + getRelativeThumbPosition(for: upperThumbValue) - thumbRadius, y: self.frame.height / 2))
        highlightedRangeContext.strokePath()
    }
}

private func getRelativeThumbPosition(for value: Int) -> CGFloat {
    let iterationDistance = (self.frame.width - trackOffset * 2) / CGFloat(max - min)
    return (CGFloat(value - min) * iterationDistance)
}

private func getTrackValue(for position: CGPoint) -> Int {
    let iterationDistance = (self.frame.width - trackOffset * 2) / CGFloat(max - min)
    return Int(position.x / iterationDistance)
}

@objc func trackTapped(recognizer: UITapGestureRecognizer) {
    var tappedValue = getTrackValue(for: recognizer.location(ofTouch: 0, in: self))
    if tappedValue > max {
        tappedValue = max
    } else if tappedValue < min {
        tappedValue = min
    }
    if tappedValue <= lowerThumbValue || tappedValue <= (upperThumbValue + lowerThumbValue) / 2 {
        lowerThumbValue = tappedValue
    } else {
        upperThumbValue = tappedValue
    }
    setNeedsDisplay()
}

@objc func thumbDragged(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .changed {
        if getTrackValue(for: recognizer.location(in: self)) <= (lowerThumbValue + upperThumbValue) / 2 {
            lowerThumbValue = getTrackValue(for: recognizer.translation(in: self))
        } else {
            upperThumbValue = getTrackValue(for: recognizer.translation(in: self))
        }
    }
    if recognizer.state == .ended {
        recognizer.setTranslation(recognizer.location(in: self), in: self)
    }
    setNeedsDisplay()
}

}

每次调用该函数时,只会设置lowerThumbValue。此外,当新的平移手势开始时,它会“跳”。回到初始值。我做错了什么呢?

1 个答案:

答案 0 :(得分:1)

translation(in:)返回自开始以来平移的相对更改,location(in:)应返回给定视图坐标系中触摸的位置 - 使用location(in:)获取位置在平移手势开始时的视图中。