Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created June 12, 2019 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinGutowski/d87df0dd7ba5c75f583124f5842a7379 to your computer and use it in GitHub Desktop.
Save KevinGutowski/d87df0dd7ba5c75f583124f5842a7379 to your computer and use it in GitHub Desktop.
Getting a CALayer to animate implicitly
let threadDictionary = NSThread.mainThread().threadDictionary()
let panelID = "com.betterTypePanel.panel"
if (threadDictionary[panelID]) {
let panel = threadDictionary[panelID]
panel.close()
threadDictionary.removeObjectForKey(panelID)
}
var panelWidth = 312
var panelHeight = 210
let panel = NSPanel.alloc().init()
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true)
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask)
panel.title = "Testing"
panel.center()
panel.makeKeyAndOrderFront(null)
panel.setLevel(NSFloatingWindowLevel)
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true)
panel.standardWindowButton(NSWindowZoomButton).setHidden(true)
threadDictionary[panelID] = panel
//Setting up the panel's subview up to be 1/4 the size of the panel
let mySubview = NSView.new()
mySubview.setFrame(NSMakeRect(0,0,panelWidth/2,panelHeight/2))
panel.contentView().addSubview(mySubview)
mySubview.wantsLayer = true
// Attempting to make the CALayer bounds to be smaller than the subview
let myLayer = CALayer.layer()
mySubview.setLayer(myLayer)
myLayer.setBorderColor(NSColor.blueColor().CGColor())
myLayer.setBorderWidth(4)
myLayer.setCornerRadius(20.0)
let myLayer2 = CALayer.layer()
myLayer2.setBounds(CGRectMake(0,0,10,40))
myLayer2.setPosition(CGPointMake(20,20))
// Bounds and position are not ignored since its a sublayer
myLayer2.setBackgroundColor(NSColor.redColor().CGColor())
myLayer.addSublayer(myLayer2)
setTimeout(function(){
CATransaction.setAnimationDuration(1)
myLayer2.position = CGPointMake(80,40)
}, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment