8 Commits

Author SHA1 Message Date
Oskar Groth 0523e2578c SetOn 2017-04-10 09:56:21 +02:00
Oskar Groth 2e199eed4d Initial coloring, IBVar reloads 2017-04-08 14:22:18 +02:00
Oskar Groth 4b50636ab8 Podspec 2017-04-08 14:17:09 +02:00
Oskar Groth 4cef47a199 Action correct status fix 2017-04-08 14:13:16 +02:00
Oskar Groth 3707a7070d Prints, states 2017-02-26 22:00:28 +11:00
Oskar Groth f9659ddd83 Podfile 2017-02-26 20:29:18 +11:00
Oskar Groth 4639c44b03 Ratio 2017-02-26 20:10:20 +11:00
Oskar Groth 5d29531a64 Image 2017-02-26 19:54:03 +11:00
6 changed files with 209 additions and 19 deletions
+9
View File
@@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright (c) 2016 Oskar Groth
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+24
View File
@@ -0,0 +1,24 @@
Pod::Spec.new do |s|
s.name = 'OGSwitch'
s.version = '1.5'
s.summary = 'Switch with icon'
s.homepage = 'https://github.com/OskarGroth/OGSwitch'
s.license = {
:type => 'MIT',
:file => 'LICENSE'
}
s.author = {
'Oskar Groth' => 'oskar@cindori.org'
}
s.source = {
:git => 'https://github.com/OskarGroth/OGSwitch.git',
:tag => s.version.to_s
}
s.platform = :osx, '10.9'
s.source_files = 'OGSwitch/OGSwitch.{swift}'
s.requires_arc = true
s.screenshot = ""
s.social_media_url = "https://twitter.com/cindoriapps"
end
@@ -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 = "513346877.889494"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "77"
endingLineNumber = "77"
landmarkName = "init(coder:)"
landmarkType = "7">
<Locations>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "OGSwitchDemo.OGSwitch.setupIcon () -&gt; ()"
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 () -&gt; ()"
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>
+91 -15
View File
@@ -11,19 +11,50 @@ import QuartzCore
public class OGSwitch : NSControl {
@IBInspectable public var tintColor: NSColor = NSColor(calibratedRed:0.27, green: 0.86, blue: 0.36, alpha: 1.0)
@IBInspectable public var knobBackgroundColor: NSColor = NSColor(calibratedWhite:1.0, alpha: 1.0)
@IBInspectable public var disabledBorderColor: NSColor = NSColor(calibratedWhite: 0.0, alpha: 0.2)
@IBInspectable public var disabledBackgroundColor: NSColor = NSColor.clear
@IBInspectable public var inactiveBackgroundColor: NSColor = NSColor(calibratedWhite: 0.0, alpha:0.3)
@IBInspectable public var tintColor: NSColor = NSColor(calibratedRed:0.27, green: 0.86, blue: 0.36, alpha: 1.0) {
didSet {
reloadLayer()
}
}
@IBInspectable public var knobBackgroundColor: NSColor = NSColor(calibratedWhite:1.0, alpha: 1.0) {
didSet {
reloadLayer()
}
}
@IBInspectable public var disabledBorderColor: NSColor = NSColor(calibratedWhite: 0.0, alpha: 0.2) {
didSet {
reloadLayer()
}
}
@IBInspectable public var disabledBackgroundColor: NSColor = NSColor.clear {
didSet {
reloadLayer()
}
}
@IBInspectable public var inactiveBackgroundColor: NSColor = NSColor(calibratedWhite: 0.0, alpha:0.3) {
didSet {
reloadLayer()
}
}
@IBInspectable public var animationDuration: TimeInterval = 0.4
@IBInspectable public var inactiveIcon: NSImage? {
didSet {
reloadLayer()
}
}
@IBInspectable public var activeIcon: NSImage? {
didSet {
reloadLayer()
}
}
@IBInspectable public var ignoreRatio: Bool = false
let kBorderLineWidth:CGFloat = 1.0
let kGoldenRatio:CGFloat = 1.6180339875
let kDecreasedGoldenRatio:CGFloat = 1.38
let kEnabledOpacity:Float = 1.0
let kDisabledOpacity:Float = 0.5
public var isOn:Bool = true
public var isOn:Bool = false
public var isActive:Bool = false
public var hasDragged:Bool = false
public var isDraggingTowardsOn:Bool = false
@@ -31,6 +62,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 +87,43 @@ public class OGSwitch : NSControl {
setupLayers()
}
public override func awakeFromNib() {
super.awakeFromNib()
reloadLayerSize()
setupIcon()
}
public func setOn(isOn: Bool, animated: Bool) {
if self.isOn != isOn {
self.isOn = isOn
reloadLayerAnimated(animated: animated)
}
}
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 +142,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 +159,7 @@ public class OGSwitch : NSControl {
knobInsideLayer!.shadowOpacity = 0.35
knobLayer!.addSublayer(knobInsideLayer!)
reloadLayerSize()
reloadLayer()
}
@@ -137,6 +207,7 @@ public class OGSwitch : NSControl {
knobInsideLayer!.frame = knobLayer!.bounds
CATransaction.commit()
animateImage()
}
public func reloadLayer() {
@@ -152,13 +223,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) {
@@ -217,13 +292,14 @@ public class OGSwitch : NSControl {
}
if isOn != on {
Swift.print("Now: \(on)")
isOn = on
if action != nil {
NSApp.sendAction(action!, to: target, from: self)
}
} else {
isOn = on
}
isOn = on
hasDragged = false
isDraggingTowardsOn = false
+10
View File
@@ -13,6 +13,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var switchButton: OGSwitch!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
@@ -22,6 +23,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Insert code here to tear down your application
}
@IBAction func switchPress(_ sender: Any) {
print("Trim is now: \(switchButton.isOn)")
perform(#selector(timer), with: nil, afterDelay: 3)
}
@objc func timer() {
print("Trim is after now: \(switchButton.isOn)")
switchButton.setOn(isOn: !switchButton.isOn, animated: false)
}
}
+20 -4
View File
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12118"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
@@ -15,6 +15,7 @@
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="OGSwitchDemo" customModuleProvider="target">
<connections>
<outlet property="switchButton" destination="M7z-uc-a7v" id="s1F-OI-IzO"/>
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
</connections>
</customObject>
@@ -689,12 +690,27 @@
<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"/>
<userDefinedRuntimeAttribute type="color" keyPath="disabledBackgroundColor">
<color key="value" red="1" green="0.40000000600000002" blue="0.40000000600000002" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="switchPress:" target="Voe-Tx-rLC" id="Mqi-G6-fmv"/>
</connections>
</customView>
</subviews>
</view>
</window>
</objects>
<resources>
<image name="NSBookmarksTemplate" width="17" height="18"/>
<image name="NSEnterFullScreenTemplate" width="15" height="15"/>
</resources>
</document>