I've written some code that generates wedges of a wheel in UIViews, stores these UIViews in an array, and displays these UIViews on top of one another, inside of a container UIView. This basically draws a segmented wheel.
What I now want is for every UIView to receive touch events individually within it's defined bezier path, and return a unique identifier. This would in practice mean that every wedge of the wheel is an individual button.
The problem I have right now is that only the last UIView that was added to the array responds to a single touch event (I have it set up to print the touch location within the bezier path right now), after which the UIView added before that responds to a single touch event, until I've cycled through all the wedges/UIViews and none respond to touch events anymore.
Here's the class that draws the wedges; I've tried implementing some methods that should allow touch events to be transferred to other UIViews at the end of the code, but I can't get it to work.
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 {
}
}
}
}
Here's the Viewcontroller code that creates the array and draws the wheel:
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])
}
}
}
This is what the rendering of the wheel looks like, and the view hierarchy:
I'm still very new to swift and coding in general, and I am pretty happy to get where I am, but I'm really stuck now, and have no real idea how to proceed. I hope I've managed to make it clear what my problem is! Thanks in advance!
Alright, took me a while to find an answer, but it's actually surprisingly easy. This is what I got right now, and it works perfectly for what I need.
// 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.