I would like to ask if anyone could figure out what's wrong with my code, I can't seem to create a circle when there is a touch event.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGray
// Do any additional setup after loading the view, typically from a nib.
}
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches as? Set<UITouch> {
let circleCenter = touch.first!.location(in: view)
let circleWidth = CGFloat(25)
let circleHeight = circleWidth
let circleView = CircleView(frame: CGRect(x: circleCenter.x, y: circleCenter.y, width: circleWidth, height: circleHeight))
view.addSubview(circleView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
If you need my code for the subview CircleView
:
class CircleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func draw(rect: CGRect) {
var context = UIGraphicsGetCurrentContext();
context!.setLineWidth(5.0)
UIColor.red.set()
let center = CGPoint(x: round(frame.size.width)/(2), y: round(frame.size.height)/2)
let radius = (round(frame.size.width) - 10)/(2)
context!.addArc(center:center, radius:radius, startAngle:0, endAngle:CGFloat(Double.pi) * 2, clockwise:true)
context!.strokePath();
}
Two of your functions were incorrect (as of in Swift 3). Change
to
and change
to