如何在贝塞尔曲线路径内检查彼此重叠显示的多个UIView子视图?

时间:2018-08-26 12:09:53

标签: ios swift uiview

我已经编写了一些代码,这些代码在UIViews中生成轮子的楔形,将这些UIView存储在一个数组中,并将这些UIView彼此重叠地显示在容器UIView中。这基本上画了一个分段的轮子。

我现在想要的是让每个UIView在其定义的bezier路径内分别接收触摸事件,并返回唯一的标识符。实际上,这意味着滚轮的每个楔块都是单独的按钮。

我现在遇到的问题是,只有添加到数组中的最后一个UIView会响应单个触摸事件(我将其设置为现在打印贝塞尔曲线路径中的触摸位置),然后UIView在响应单个触摸事件之前添加,直到我遍历所有楔块/ UIView并且没有一个响应触摸事件为止。

这里是绘制楔形的班级;我尝试实现一些方法,这些方法应允许在代码末尾将触摸事件转移到其他UIView,但是我无法使其正常工作。

import UIKit

class WedgeDraw: UIView {

var wedgeNumber:CGFloat = 4
var wedgeLocation: CGFloat = 2

var wedgeIdentifier = 0
var wedgePath = UIBezierPath()
var touchPosition = CGPoint()


override func draw(_ rect: CGRect) {

    let startAngle = (360 / wedgeNumber - 360) * (wedgeLocation - 1) * CGFloat.pi / 180
    let endangle = (360 / wedgeNumber - 360) * wedgeLocation * CGFloat.pi / 180


    let center = CGPoint(x: self.bounds.midX, y: self.bounds.midY)

    let outerRadius = self.bounds.midX - 3
    let innerRadius = self.bounds.width / 8

    //defines end points of simulated Bezier archs as a CGpoint
    let endPointInner = UIBezierPath(arcCenter: center, radius: innerRadius, startAngle: startAngle, endAngle: endangle, clockwise: false).currentPoint
    let endPointOuterCounterclockwise = UIBezierPath(arcCenter: center, radius: outerRadius, startAngle: endangle, endAngle: startAngle, clockwise: true).currentPoint

    //defines bezier arch curves
    let outerWedgeCurvePath = UIBezierPath(arcCenter: center, radius: outerRadius, startAngle: startAngle, endAngle: endangle, clockwise: true)
    let innerWedgeCurvePath = UIBezierPath(arcCenter: center, radius: innerRadius, startAngle: endangle, endAngle: startAngle, clockwise: false)

    //draw line path

    wedgePath.append(outerWedgeCurvePath)
    wedgePath.addLine(to: endPointInner)
    wedgePath.append(innerWedgeCurvePath)
    wedgePath.addLine(to: endPointOuterCounterclockwise)

    //sets stroke and color properties and draws the bezierpath.

    UIColor.black.setStroke()
    UIColor.white.setFill()
    wedgePath.lineWidth = 3
    wedgePath.stroke()
    wedgePath.fill()


}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let view = super.hitTest(point, with: event)
    if wedgePath.contains(touchPosition) {
        return nil
    } else {
        return view
    }
}

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

    if let touch = touches.first {
        touchPosition = touch.location(in: self)

        if wedgePath.contains(touchPosition){
            print(touchPosition)
            print(isUserInteractionEnabled)
        } else {

        }
    }
}
}

以下是创建数组并绘制轮子的Viewcontroller代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var boardContainer: UIView!

@IBAction func checkButton(_ sender: UIButton) {

}

var wedgeViewConstructorArray = [WedgeDraw]()


override func viewDidLoad() {
    super.viewDidLoad()

    //creates array of UNIQUE wedgedraw objects
    for _ in 1...6 {
        wedgeViewConstructorArray.append(WedgeDraw())
    }
    drawBoard()
}

func drawBoard() {

    for i in 0..<wedgeViewConstructorArray.count {
        wedgeViewConstructorArray[i].wedgeIdentifier = i

        wedgeViewConstructorArray[i].frame = CGRect(x: 0, y: 0, width: boardContainer.frame.width, height: boardContainer.frame.height)

        wedgeViewConstructorArray[i].wedgeNumber = CGFloat(wedgeViewConstructorArray.count)
        wedgeViewConstructorArray[i].wedgeLocation = CGFloat(i + wedgeViewConstructorArray.count + 1)
        wedgeViewConstructorArray[i].backgroundColor = UIColor.clear

        boardContainer.addSubview(wedgeViewConstructorArray[i])
    }
}  
}

这是转轮的外观以及视图层次结构:

Wheelimage

总体而言,我仍然对Swift和编码尚不陌生,我很高兴能找到自己的位置,但现在我真的很困,不知道如何进行。希望我能弄清楚我的问题所在! 预先感谢!

1 个答案:

答案 0 :(得分:0)

好的,花了我一段时间才能找到答案,但这实际上非常容易。 这就是我现在得到的,并且完全可以满足我的需求。

    // checks if point inside touch event is inside bezier path and passes on to next subview if false.
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            let location = point
            if wedgePath.contains(location) == true {
                print("\(wedgeColor)")
                return true


    }
    return false

}