I've drawn a red circle. Then I would like to delete it. How can I do that?
class Red: NSView {
var red = 255
var green = 0
var blue = 0
override func draw(_ dirtyRect: NSRect) {
let circleFillColor = NSColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)
let cPath: NSBezierPath = NSBezierPath(ovalIn: dirtyRect)
circleFillColor.set()
cPath.fill()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let signal = Red(frame: NSRect(x: 146, y: 18, width: 25, height: 25))
self.view.addSubview(signal)
}
You can use removeFromSuperview method of the view to get if removed, as such:
class ViewController: NSViewController {
var signal: Red?
override func viewDidLoad() {
super.viewDidLoad()
self.signal = Red(frame: NSRect(x: 146, y: 18, width: 25, height: 25))
self.view.addSubview(signal!)
}
func deleteCircle() {
self.signal?.removeFromSuperview()
self.signal = nil
}
}
Related
I am a newbie to Swift coding
Below is a custom view class:
class MyView: NSView {
var xloc = 0.0
var yloc = 0.0
var width = 400.0
var height = 200.0
var myRect = NSRect(x: 0.0, y: 0.0, width: 400.0, height: 200.0)
...
override func draw(_ dirtyRect: NSRect) {
let myColor = NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: self.alpha)
myColor.setFill()
dirtyRect.fill()
self.myRect = NSRect(x: self.xloc, y: self.yloc, width: self.width, height: self.height)
self.setNeedsDisplay(myRect)
self.myRect.fill(using: NSCompositingOperation.clear)
}
The view controller has a keyDown handler as follows:
override func viewDidLoad() {
super.viewDidLoad()
view.window?.backgroundColor = NSColor.white
NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
self.keyDown(with: $0)
return $0
}
}
override func keyDown(with event: NSEvent) {
let theView = self.view as! MyView
if event.keyCode == 126 {
NSLog("Increasing Y coordinate")
theView.yloc = theView.yloc + 10
self.view.display()
}
}
Below is the AppDelegate.swift's content:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let window = NSApplication.shared.windows.first
window?.isOpaque = false
window?.backgroundColor = NSColor.clear
window?.ignoresMouseEvents = true
window?.toggleFullScreen(self)
window?.collectionBehavior = NSWindow.CollectionBehavior.fullScreenPrimary
let screenFrame = NSScreen.main?.frame
window?.setFrame(screenFrame!, display: true)
let myView = MyView()
window?.contentView = myView
}
The rectangular hole is seen on the screen initially
The above handler is called whenever I press the up arrow key. The self.view.display() in turn calls the draw() method on the view. But the NSRect is not drawn in the new position
Not sure what is wrong
Trying to create a custom NSSlider. Overriding the drawKnob() method of the NSSliderCell changes the knob's appearance but doing this somehow disconnects the link between the knob's position and user interactions with the slider.
In the objective C example that is often referenced (https://github.com/lucasderraugh/LADSlider) it looks like when you override drawKnob() you then need to explicitly deal with the startTracking method, but I haven't found a solution that works for me - at the moment I am just setting the cell's startTracking 'at' property to the current value of the slider, but not sure what the right approach is.
Quite a few examples include an NSCoder argument in the cell's custom initialiser - I don't understand why, but maybe this has something to do with ensuring a connection between the display of the knob and the actual slider value?
import Cocoa
class ViewController: NSViewController {
var seekSlider = NSSlider()
var seekSliderCell = SeekSliderCell()
override func viewDidLoad() {
super.viewDidLoad()
seekSlider = NSSlider(target: self, action: #selector(self.seek(_:)))
seekSlider.cell = seekSliderCell
seekSlider.cell?.target = self
seekSlider.cell?.action = #selector(self.seek(_:))
seekSlider.isEnabled = true
seekSlider.isContinuous = true
view.addSubview(seekSlider)
}
#objc func seek(_ sender: NSObject) {
let val = seekSlider.cell?.floatValue
let point = NSPoint(x: Double(val!), y: 0.0)
seekSlider.cell?.startTracking(at: point, in: self.seekSlider)
}
}
class SeekSliderCell: NSSliderCell {
// required init(coder aDecoder: NSCoder) {
// super.init(coder: aDecoder)
// }
override func drawKnob() {
let frame = NSRect(x: 0.0, y: 6.0, width: 20.0, height: 10.0)
let c = NSColor(red: 0.9, green: 0.0, blue: 0.6, alpha: 1.0)
c.setFill()
NSBezierPath.init(roundedRect: frame, xRadius: 3, yRadius: 3).fill()
}
override func startTracking(at startPoint: NSPoint, in controlView: NSView) -> Bool {
return true
}
}
The documentation of drawKnob() states:
Special Considerations
If you create a subclass of NSSliderCell, don’t override this method. Override drawKnob(_:) instead.
Instead of
func drawKnob()
override
func drawKnob(_ knobRect: NSRect)
Example:
class ViewController: NSViewController {
var seekSlider = NSSlider()
var seekSliderCell = SeekSliderCell()
override func viewDidLoad() {
super.viewDidLoad()
seekSlider = NSSlider(target: self, action: #selector(self.seek(_:)))
seekSlider.cell = seekSliderCell
seekSlider.cell?.target = self
seekSlider.cell?.action = #selector(self.seek(_:))
seekSlider.isEnabled = true
seekSlider.isContinuous = true
view.addSubview(seekSlider)
}
#objc func seek(_ sender: NSObject) {
let val = seekSlider.cell?.floatValue
print("\(String(describing: val))")
}
}
class SeekSliderCell: NSSliderCell {
override func drawKnob(_ knobRect: NSRect) {
var frame = NSRect(x: 0.0, y: 6.0, width: 20.0, height: 10.0)
frame.origin.x = knobRect.origin.x + (knobRect.size.width - frame.size.width) / 2
frame.origin.y = knobRect.origin.y + (knobRect.size.height - frame.size.height) / 2
let c = NSColor(red: 0.9, green: 0.0, blue: 0.6, alpha: 1.0)
c.setFill()
NSBezierPath.init(roundedRect: frame, xRadius: 3, yRadius: 3).fill()
}
}
If I call self.layer? in a draw() method to control the content of an NSView, the view's properties can be updated by the view controller. However, if I create a graphics context for drawing into, this relationship breaks and the view controller no longer updates the view's properties. Why?
In the simple example I provide, if you comment out the guard statement in TestView and call drawBackgroundWithoutContext(), the view is given a background colour when viewDidLoad() is called, and the colour is updated when the mouseDown event occurs.
As soon as a context is declared this breaks, even if you are still calling drawBackgroundWithoutContext().
import Cocoa
class ViewController: NSViewController {
var newR: CGFloat?
var newG: CGFloat?
var newB: CGFloat?
var tview = TestView(frame: NSRect(x: 20, y: 30, width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
tview.r = 1.0
tview.g = 0.5
tview.b = 0.8
self.view.addSubview(tview)
}
override func mouseDown(with event: NSEvent) {
newR = 0.1
newG = 0.9
newB = 0.0
tview.r = newR
tview.g = newG
tview.b = newB
tview.draw(NSRect(x: 20, y: 30, width: 100, height: 100))
// tview.setNeedsDisplay(NSRect(x: 20, y: 30, width: 100, height: 100))
}
}
class TestView: NSView {
var r: CGFloat?
var g: CGFloat?
var b: CGFloat?
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let context = NSGraphicsContext.current?.cgContext else {
return
}
// drawBackgroundWithoutContext(red: r, green: g, blue: b)
drawBackgroundWithContext(context: context, red: r, green: g, blue: b)
}
func drawBackgroundWithoutContext(red: CGFloat?, green: CGFloat?, blue: CGFloat?) {
self.layer?.backgroundColor = CGColor(red: red ?? 0.0, green: green ?? 0.0, blue: blue ?? 0.0, alpha: 1.0)
}
func drawBackgroundWithContext(context: CGContext, red: CGFloat?, green: CGFloat?, blue: CGFloat?) {
context.setFillColor(CGColor(red: red ?? 0.0, green: green ?? 0.0, blue: blue ?? 0.0, alpha: 1.0))
context.fill(CGRect(x: 20, y: 30, width: 100, height: 100))
}
}
I read that you should not actually call .draw() in the view controller and the correct method is setNeedsDisplay(), however this had no affect. I have left this call as a comment in the code so you can see what I tried.
Trying to make .saver in swift for MacOS, But don't know how to add custom text or Label on ScreenSaverView class which is the subclass from NSView.
Below is my piece of code, but doesn't work.
private var text: CATextLayer!
override func draw(_ rect: NSRect) {
// Draw a single frame in this function
self.layer?.backgroundColor = .init(red: 142/255, green: 167/255, blue: 125/255, alpha: 1)
text.removeFromSuperlayer()
text.string = "Test"
text.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
text.foregroundColor = .white
text.backgroundColor = .clear
self.layer?.addSublayer(text)
}
I've found this code for drawing the line for MacOS app.
class Drawing: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let context = NSGraphicsContext.current?.cgContext;
context!.beginPath()
context!.move(to: CGPoint(x: 0.0, y: 0.0))
context!.addLine(to: CGPoint(x: 100.0, y: 100.0))
context!.setStrokeColor(red: 1, green: 0, blue: 0, alpha: 1)
context!.setLineWidth(1.0)
context!.strokePath()
}
override func viewDidLoad() {
super.viewDidLoad()
let dr = Drawing(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
self.view.addSubview(dr)
}
How to change this code for circle? It's difficult for me to solve this problem. Help me, please.
The circle equivalent is
class Drawing: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let context = NSGraphicsContext.current!.cgContext
context.saveGState()
context.setFillColor(NSColor.red.cgColor)
context.fillEllipse(in: dirtyRect)
context.restoreGState()
}
}
or the classic NSBezierPath way
class Drawing: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let fillColor = NSColor.red
let path = NSBezierPath(ovalIn: dirtyRect)
fillColor.setFill()
path.fill()
}
}
Depending on the side of the circle, you can also do this:
class YourParentView: NSView {
// Create it as a view of its own
let circleView = NSView()
circleView.wantsLayer = true
circleView.layer?.cornerRadius = 7
//depending on the size, adjust this
//and that's it. Now it's a circle.
//Then just addict the style
circleView.layer?.backgroundColor = NSColor.green.cgColor
circleView.layer?.borderColor = NSColor.white.cgColor
//Be sure to add it to the parent view
self.view.addSubview(circleView)
}