Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4639c44b03 | |||
| 5d29531a64 |
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Bucket
|
||||
type = "1"
|
||||
version = "2.0">
|
||||
<Breakpoints>
|
||||
<BreakpointProxy
|
||||
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
|
||||
<BreakpointContent
|
||||
shouldBeEnabled = "No"
|
||||
ignoreCount = "0"
|
||||
continueAfterRunningActions = "No"
|
||||
filePath = "OGSwitch/OGSwitch.swift"
|
||||
timestampString = "509792812.217359"
|
||||
startingColumnNumber = "9223372036854775807"
|
||||
endingColumnNumber = "9223372036854775807"
|
||||
startingLineNumber = "69"
|
||||
endingLineNumber = "69"
|
||||
landmarkName = "setupIcon()"
|
||||
landmarkType = "7">
|
||||
<Locations>
|
||||
<Location
|
||||
shouldBeEnabled = "No"
|
||||
ignoreCount = "0"
|
||||
continueAfterRunningActions = "No"
|
||||
symbolName = "OGSwitchDemo.OGSwitch.setupIcon () -> ()"
|
||||
moduleName = "OGSwitchDemo"
|
||||
usesParentBreakpointCondition = "Yes"
|
||||
urlString = "file:///Users/oskar/OGSwitch/OGSwitch/OGSwitch.swift"
|
||||
timestampString = "509430041.079686"
|
||||
startingColumnNumber = "9223372036854775807"
|
||||
endingColumnNumber = "9223372036854775807"
|
||||
startingLineNumber = "69"
|
||||
endingLineNumber = "69"
|
||||
offsetFromSymbolStart = "59">
|
||||
</Location>
|
||||
<Location
|
||||
shouldBeEnabled = "No"
|
||||
ignoreCount = "0"
|
||||
continueAfterRunningActions = "No"
|
||||
symbolName = "OGSwitchDemo.OGSwitch.setupIcon () -> ()"
|
||||
moduleName = "OGSwitchDemo"
|
||||
usesParentBreakpointCondition = "Yes"
|
||||
urlString = "file:///Users/oskar/OGSwitch/OGSwitch/OGSwitch.swift"
|
||||
timestampString = "509430041.081101"
|
||||
startingColumnNumber = "9223372036854775807"
|
||||
endingColumnNumber = "9223372036854775807"
|
||||
startingLineNumber = "69"
|
||||
endingLineNumber = "69"
|
||||
offsetFromSymbolStart = "124">
|
||||
</Location>
|
||||
</Locations>
|
||||
</BreakpointContent>
|
||||
</BreakpointProxy>
|
||||
</Breakpoints>
|
||||
</Bucket>
|
||||
+47
-7
@@ -17,6 +17,9 @@ public class OGSwitch : NSControl {
|
||||
@IBInspectable public var disabledBackgroundColor: NSColor = NSColor.clear
|
||||
@IBInspectable public var inactiveBackgroundColor: NSColor = NSColor(calibratedWhite: 0.0, alpha:0.3)
|
||||
@IBInspectable public var animationDuration: TimeInterval = 0.4
|
||||
@IBInspectable public var inactiveIcon: NSImage?
|
||||
@IBInspectable public var activeIcon: NSImage?
|
||||
@IBInspectable public var ignoreRatio: Bool = false
|
||||
|
||||
let kBorderLineWidth:CGFloat = 1.0
|
||||
let kGoldenRatio:CGFloat = 1.6180339875
|
||||
@@ -31,6 +34,7 @@ public class OGSwitch : NSControl {
|
||||
public var backgroundLayer:CALayer?
|
||||
public var knobLayer: CALayer?
|
||||
public var knobInsideLayer: CALayer?
|
||||
public var iconLayer = CALayer()
|
||||
|
||||
override public var acceptsFirstResponder: Bool {
|
||||
get {
|
||||
@@ -55,10 +59,36 @@ public class OGSwitch : NSControl {
|
||||
setupLayers()
|
||||
}
|
||||
|
||||
public override func awakeFromNib() {
|
||||
super.awakeFromNib()
|
||||
reloadLayerSize()
|
||||
setupIcon()
|
||||
}
|
||||
|
||||
internal func setupIcon() {
|
||||
guard let icon = inactiveIcon, let bounds = knobLayer?.bounds else {
|
||||
return
|
||||
}
|
||||
let size = icon.size
|
||||
iconLayer.frame = NSMakeRect((bounds.width-size.width)/2, (bounds.height-size.height)/2, size.width, size.height)
|
||||
iconLayer.contents = icon
|
||||
}
|
||||
|
||||
internal func animateImage() {
|
||||
let animation = CABasicAnimation(keyPath: "contents")
|
||||
animation.toValue = isOn ? activeIcon : inactiveIcon
|
||||
animation.fromValue = iconLayer.contents
|
||||
animation.duration = 0.5
|
||||
animation.isRemovedOnCompletion = false
|
||||
animation.fillMode = kCAFillModeForwards
|
||||
iconLayer.add(animation, forKey: "contents")
|
||||
iconLayer.setValue(animation.toValue, forKey: "contents")
|
||||
}
|
||||
|
||||
internal func setupLayers() {
|
||||
rootLayer = CALayer()
|
||||
layer = rootLayer
|
||||
|
||||
|
||||
wantsLayer = true
|
||||
|
||||
backgroundLayer = CALayer()
|
||||
@@ -77,12 +107,16 @@ public class OGSwitch : NSControl {
|
||||
knobLayer!.shadowOffset = CGSize(width:0.0, height:-2.0)
|
||||
knobLayer!.shadowRadius = 1.0
|
||||
knobLayer!.shadowOpacity = 0.3
|
||||
|
||||
rootLayer!.addSublayer(knobLayer!)
|
||||
|
||||
knobInsideLayer = CALayer()
|
||||
knobInsideLayer!.frame = knobLayer!.bounds
|
||||
knobInsideLayer!.autoresizingMask = [.layerHeightSizable, .layerWidthSizable]
|
||||
|
||||
iconLayer = CALayer()
|
||||
knobInsideLayer?.addSublayer(iconLayer)
|
||||
|
||||
knobInsideLayer!.shadowColor = NSColor.black.cgColor
|
||||
knobInsideLayer!.shadowOffset = CGSize(width:0.0, height:0.0)
|
||||
knobInsideLayer!.backgroundColor = NSColor.white.cgColor
|
||||
@@ -90,6 +124,7 @@ public class OGSwitch : NSControl {
|
||||
knobInsideLayer!.shadowOpacity = 0.35
|
||||
knobLayer!.addSublayer(knobInsideLayer!)
|
||||
|
||||
|
||||
reloadLayerSize()
|
||||
reloadLayer()
|
||||
}
|
||||
@@ -137,6 +172,7 @@ public class OGSwitch : NSControl {
|
||||
knobInsideLayer!.frame = knobLayer!.bounds
|
||||
|
||||
CATransaction.commit()
|
||||
animateImage()
|
||||
}
|
||||
|
||||
public func reloadLayer() {
|
||||
@@ -152,13 +188,17 @@ public class OGSwitch : NSControl {
|
||||
var width = 0.0
|
||||
|
||||
let bounds: CGRect = backgroundLayer!.bounds
|
||||
|
||||
if isActive {
|
||||
width = Double((bounds.width - 2.0 * kBorderLineWidth) / kGoldenRatio)
|
||||
}
|
||||
else {
|
||||
width = Double((bounds.width - 2.0 * kBorderLineWidth) / kDecreasedGoldenRatio)
|
||||
if ignoreRatio {
|
||||
width = Double(height)
|
||||
} else {
|
||||
if isActive {
|
||||
width = Double((bounds.width - 2.0 * kBorderLineWidth) / kGoldenRatio)
|
||||
}
|
||||
else {
|
||||
width = Double((bounds.width - 2.0 * kBorderLineWidth) / kDecreasedGoldenRatio)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var x:CGFloat = 0
|
||||
if (!hasDragged && !isOn) || (hasDragged && !isDraggingTowardsOn) {
|
||||
|
||||
@@ -689,12 +689,21 @@
|
||||
<rect key="frame" x="0.0" y="0.0" width="480" height="360"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ME7-To-eFM" customClass="OGSwitch" customModule="OGSwitchDemo" customModuleProvider="target">
|
||||
<rect key="frame" x="159" y="144" width="163" height="96"/>
|
||||
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="M7z-uc-a7v" customClass="OGSwitch" customModule="OGSwitchDemo" customModuleProvider="target">
|
||||
<rect key="frame" x="129" y="161" width="163" height="96"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
|
||||
<userDefinedRuntimeAttributes>
|
||||
<userDefinedRuntimeAttribute type="image" keyPath="inactiveIcon" value="NSEnterFullScreenTemplate"/>
|
||||
<userDefinedRuntimeAttribute type="image" keyPath="activeIcon" value="NSBookmarksTemplate"/>
|
||||
<userDefinedRuntimeAttribute type="boolean" keyPath="ignoreRatio" value="YES"/>
|
||||
</userDefinedRuntimeAttributes>
|
||||
</customView>
|
||||
</subviews>
|
||||
</view>
|
||||
</window>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="NSBookmarksTemplate" width="17" height="18"/>
|
||||
<image name="NSEnterFullScreenTemplate" width="15" height="15"/>
|
||||
</resources>
|
||||
</document>
|
||||
|
||||
Reference in New Issue
Block a user