I am trying to make a simple single view app for my own practice, but I ran into a problem and haven't got around this problem for past couple of days.
Screenshot of the current program
Basically, when I press any of these style buttons or adjust the slider, I want it to redraw the CGContext that's on the bottom of the screen (red dotted line). For example, if I press the STYLE 1 button, it should call the function called drawStyleOne() and apply these two different changes:
context?.setLineDash(phase: 0, lengths: [2,3])
context?.setStrokeColor(UIColor.green.cgColor)
My goal is to re-draw the dotted line (smaller dot) and change the color of the context to green. (context is an instance of CGContext)
I have everything drawn on the override func draw(_ rect: CGRect), but I cannot get it to re-draw the CGContext to the way I want it to.
Here's part of my code to help explain this situation bit better.
import UIKit
class VectorView: UIView {
private var context: CGContext? = nil
override init(frame: CGRect) {
super.init(frame: frame)
//contentMode = .redraw //tried this, didn't work.
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
// lets us make calls
context = UIGraphicsGetCurrentContext()!
context?.move(to: CGPoint(x: 0.0, y: 10.0))
context?.setLineDash(phase: 0, lengths: [2,3])
context?.setLineJoin(CGLineJoin.bevel)
context?.setLineCap(CGLineCap.square)
context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
context?.setLineWidth(1.0)
context?.setStrokeColor(UIColor.red.cgColor)
context?.drawPath(using: CGPathDrawingMode.stroke)
}
func drawStyleOne () {
context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: 0.0, y: 10.0))
context?.setLineDash(phase: 0, lengths: [2,3])
context?.setLineJoin(CGLineJoin.bevel)
context?.setLineCap(CGLineCap.square)
context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
context?.setStrokeColor(UIColor.green.cgColor)
context?.drawPath(using: CGPathDrawingMode.stroke)
self.setNeedsDisplay()
//contentMode = .redraw //Tried this and didn't work either..
NSLog("drawStyleOne got called")
}
and the drawStyleOne function from class above gets called inside the AppDelegate.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let screenSize = UIScreen.main.bounds
private var _vectorView: VectorView? = nil
private var _buttonStylesView: ButtonStyleView? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.rootViewController = ViewController()
window?.rootViewController?.view.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
_vectorView = VectorView()
_vectorView?.frame = CGRect(x: 0.0, y: 650, width: 414, height: 70)
_vectorView?.backgroundColor = UIColor.lightGray
window?.rootViewController?.view.addSubview(_vectorView!)
_buttonStylesView = ButtonStyleView()
_buttonStylesView?.frame = CGRect (x: screenSize.width / 10, y: screenSize.height * 6 / 10, width: 414, height: 100)
_buttonStylesView?.backgroundColor = UIColor.clear
window?.rootViewController?.view.addSubview(_buttonStylesView!)
_buttonStylesView?.styleOne?.addTarget(self, action: #selector(styleButtonPressed), for: UIControlEvents.touchDown)
return true
}
func styleButtonPressed() {
NSLog("STYLE BUTTON PRESSED")
_vectorView?.drawStyleOne()
}
}
I even put NSLog("drawStyleOne got called") at the end just to verify that the function is getting called (and it does), however it will not redraw the line no matter what I try.
Any help/advice would be much appreciated.