gpt4 book ai didi

ios - 如何检查彼此顶部显示的多个 UIView subview 是否有贝塞尔曲线路径内的触摸事件?

转载 作者:行者123 更新时间:2023-11-30 11:12:40 25 4
gpt4 key购买 nike

我编写了一些代码,这些代码在 UIView 中生成轮子的楔形,将这些 UIView 存储在一个数组中,并将这些 UIView 在容器 UIView 内一个一个地显示。这基本上绘制了一个分段的轮子。

我现在想要的是每个 UIView 在其定义的贝塞尔路径中单独接收触摸事件,并返回唯一的标识符。这实际上意味着轮子的每个楔子都是一个单独的按钮。

我现在遇到的问题是,只有添加到数组中的最后一个 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])
}
}
}

这就是轮子的渲染效果,以及 View 层次结构:

Wheelimage

总的来说,我对 swift 和编码还很陌生,我很高兴能达到现在的水平,但我现在真的陷入困境,并且不知道如何继续。我希望我已经弄清楚我的问题是什么!提前致谢!

最佳答案

好吧,我花了一段时间才找到答案,但实际上非常简单。这就是我现在得到的,它非常适合我的需要。

    // 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

}

关于ios - 如何检查彼此顶部显示的多个 UIView subview 是否有贝塞尔曲线路径内的触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com