在UITableviewscell中使用UITouch,Touchbegan无法正常工作

时间:2018-05-24 15:02:13

标签: ios swift uitableview

我正在使用this库进行colorpicker,我已将其添加到UITableviewcell的子视图中,

这里的问题是当我开始移动颜色指示器时,也会更改单元格内的HUE级别滑块滚动UITableView。

我现在正在做的是,在TableViewScrollDelegate中添加了2个函数

protocol BrightnessViewDelegate: class {
 func brightnessSelected(_ brightness: CGFloat)
}

protocol TableViewScrollDelegate: class {
  func tableViewScrollStart()
  func tableViewScrollPause()
}

class BrightnessView: UIView {

weak var delegate: BrightnessViewDelegate?
weak var delegateScroll: TableViewScrollDelegate?
var colorLayer: CAGradientLayer!

var point: CGPoint!
var indicator = CAShapeLayer()
var indicatorColor: CGColor = UIColor.lightGray().cgColor
var indicatorBorderWidth: CGFloat = 2.0

init(frame: CGRect, color: UIColor) {
    super.init(frame: frame)

    // Init the point at the correct position
    point = getPointFromColor(color)

    // Clear the background
    backgroundColor = UIColor.clear()

    // Create a gradient layer that goes from black to white
    // Create a gradient layer that goes from black to white
    var hue: CGFloat = 0.0, saturation: CGFloat = 0.0, brightness: CGFloat = 0.0, alpha: CGFloat = 0.0
    let ok: Bool = color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
    if (!ok) {
        print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
    }
    colorLayer = CAGradientLayer()
    colorLayer.colors = [
        UIColor.white().cgColor,
        UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor
    ]
    colorLayer.locations = [0.0, 1.0]
    colorLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    colorLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
    colorLayer.frame = CGRect(x: 0, y: 2, width: self.frame.size.width, height: 24)
    // Insert the colorLayer into this views layer as a sublayer
    self.layer.insertSublayer(colorLayer, below: layer)

    // Add the indicator
    indicator.strokeColor = indicatorColor
    indicator.fillColor = indicatorColor
    indicator.lineWidth = indicatorBorderWidth
    self.layer.addSublayer(indicator)

    drawIndicator()
}

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

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    delegateScroll?.tableViewScrollPause()
    touchHandler(touches)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    delegateScroll?.tableViewScrollStart()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    touchHandler(touches)
}

func touchHandler(_ touches: Set<UITouch>) {
    // Set reference to the location of the touchesMoved in member point
    if let touch = touches.first {
        point = touch.location(in: self)
    }

    point.y = self.frame.height/2
    point.x = getXCoordinate(point.x)
    // Notify delegate of the new brightness
    delegate?.brightnessSelected(getBrightnessFromPoint())

    drawIndicator()
}

func getXCoordinate(_ coord: CGFloat) -> CGFloat {
    // Offset the x coordinate to fit the view
    if (coord < 1) {
        return 1
    }
    if (coord > frame.size.width - 1 ) {
        return frame.size.width - 1
    }
    return coord
}

func drawIndicator() {
    // Draw the indicator
    if (point != nil) {
        indicator.path = UIBezierPath(roundedRect: CGRect(x: point.x-3, y: 0, width: 6, height: 28), cornerRadius: 3).cgPath
    }
}

func getBrightnessFromPoint() -> CGFloat {
    // Get the brightness value for a given point
    return point.x/self.frame.width
}

func getPointFromColor(_ color: UIColor) -> CGPoint {
    // Update the indicator position for a given color
    var hue: CGFloat = 0.0, saturation: CGFloat = 0.0, brightness: CGFloat = 0.0, alpha: CGFloat = 0.0
    let ok: Bool = color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
    if (!ok) {
        print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
    }

    return CGPoint(x: brightness * frame.width, y: frame.height / 2)
}

func setViewColor(_ color: UIColor!) {
    // Update the Gradient Layer with a given color
    var hue: CGFloat = 0.0, saturation: CGFloat = 0.0, brightness: CGFloat = 0.0, alpha: CGFloat = 0.0
    let ok: Bool = color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
    if (!ok) {
        print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
    }
    colorLayer.colors = [
        UIColor.white().cgColor,
        UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor
    ]
}
}
  

My ViewController with Tableview

 func tableViewScrollStart() {
    self.tableView.isScrollEnabled = true
}

func tableViewScrollPause() {
    self.tableView.isScrollEnabled = false
}

当我在这个单元格中更改颜色时,我不希望tableview滚动。

希望我能够清楚地解释我的问题。

谢谢

0 个答案:

没有答案