7 Commits

Author SHA1 Message Date
Oskar 79209bca05 Merge branch 'master' of https://github.com/OskarGroth/OGSwitch 2018-08-08 21:32:10 +02:00
Oskar 2004091f18 Improve border and add size classes 2018-08-08 21:32:00 +02:00
Oskar Groth be0bd0211b Update README.md 2018-08-05 11:59:30 +02:00
Oskar 3f6129964e Extend NSControl 2018-08-05 11:56:14 +02:00
Oskar Groth 352f7c8ad2 Update README.md 2018-07-14 19:23:56 +02:00
Oskar Groth 52781c2335 Update README.md 2018-07-14 19:23:47 +02:00
Oskar 6adaab83d6 Title 2018-07-14 19:22:38 +02:00
5 changed files with 62 additions and 22 deletions
+27 -9
View File
@@ -11,7 +11,26 @@ import QuartzCore
//Can not be NSControl due to 10.9 issues: https://github.com/iluuu1994/ITSwitch/issues/21
@IBDesignable public class OGSwitch : NSView {
@IBDesignable public class OGSwitch: NSControl {
public enum Size {
case small
case medium
case large
public var size: CGSize {
switch self {
case .small:
return CGSize(width: 46, height: 28)
case .medium:
return CGSize(width: 56, height: 34)
case .large:
return CGSize(width: 69, height: 41)
}
}
}
@IBInspectable public var tintColor: NSColor = NSColor(calibratedRed:0.27, green: 0.86, blue: 0.36, alpha: 1.0) {
didSet {
@@ -54,20 +73,19 @@ import QuartzCore
reloadLayer()
}
}
@IBInspectable public var isEnabled: Bool = true {
override public var isEnabled: Bool {
didSet {
reloadLayerAnimated(animated: true)
}
}
let kBorderLineWidth:CGFloat = 1.0
lazy var kBorderLineWidth: CGFloat = bounds.height*(2/31)
let kGoldenRatio:CGFloat = 1.6180339875
let kDecreasedGoldenRatio:CGFloat = 1.38
let kEnabledOpacity:Float = 1.0
let kDisabledOpacity:Float = 0.5
var dragEvents = 0
@objc public var action: Selector?
@objc public var target: AnyObject?
public var isOn:Bool = false
public var isActive:Bool = false
public var hasDragged:Bool = false
@@ -94,7 +112,7 @@ import QuartzCore
super.init(frame: frame);
setup()
}
override public func awakeFromNib() {
super.awakeFromNib()
reloadLayerSize()
@@ -153,9 +171,9 @@ import QuartzCore
knobLayer!.autoresizingMask = CAAutoresizingMask.layerHeightSizable
knobLayer!.backgroundColor = knobBackgroundColor.cgColor
knobLayer!.shadowColor = NSColor.black.cgColor
knobLayer!.shadowOffset = CGSize(width:0.0, height:-2.0)
knobLayer!.shadowRadius = 1.0
knobLayer!.shadowOpacity = 0.3
knobLayer!.shadowOffset = CGSize(width:0.0, height:-6.5)
knobLayer!.shadowRadius = 2.5
knobLayer!.shadowOpacity = 0.15
knobLayer?.masksToBounds = false
rootLayer!.addSublayer(knobLayer!)
+1 -1
View File
@@ -24,7 +24,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
@IBAction func switchPress(_ sender: Any) {
NSLog("Switch is now: \(switchButton.isOn)")
NSLog("Switch is now: \((sender as! OGSwitch).isOn)")
// perform(#selector(timer), with: nil, afterDelay: 3)
}
+30 -10
View File
@@ -681,7 +681,7 @@
</menuItem>
</items>
</menu>
<window title="OGSwitch-DemoDemo" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<window title="OGSwitch-Demo" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="335" y="390" width="480" height="360"/>
@@ -696,35 +696,55 @@
<userDefinedRuntimeAttributes>
<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"/>
<color key="value" name="systemGreenColor" catalog="System" colorSpace="catalog"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="isEnabled" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="switchPress:" target="Voe-Tx-rLC" id="Mqi-G6-fmv"/>
</connections>
</customView>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Brb-kf-Oxi" customClass="OGSwitch" customModule="OGSwitch">
<rect key="frame" x="159" y="63" width="163" height="96"/>
<rect key="frame" x="140" y="60" width="46" height="28"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="activeIcon" value="checkmark"/>
<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>
<userDefinedRuntimeAttribute type="image" keyPath="inactiveIcon" value="checkmark"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="switchPress:" target="Voe-Tx-rLC" id="tTg-hX-q3I"/>
</connections>
</customView>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dPq-0j-nKi" customClass="OGSwitch" customModule="OGSwitch">
<rect key="frame" x="272" y="53" width="69" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<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="buu-wq-UpG"/>
</connections>
</customView>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1VD-pu-2Ej" customClass="OGSwitch" customModule="OGSwitch">
<rect key="frame" x="200" y="57" width="56" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<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="wRK-Gm-efR"/>
</connections>
</customView>
</subviews>
</view>
<point key="canvasLocation" x="86" y="-105"/>
<point key="canvasLocation" x="198" y="-127"/>
</window>
</objects>
<resources>
<image name="checkmark" width="35" height="34"/>
</resources>
</document>
+4 -2
View File
@@ -11,7 +11,7 @@ This is a `NSView` subclass that lets you create beautiful iOS-inspired switches
`OGSwitch` now also supports `IBDesignable` and renders directly in Interface Builder:
![IBDesignable](https://i.imgur.com/F86YoKv.png "IBDesignable")
![IBDesignable](https://i.imgur.com/BKUyWGg.png "IBDesignable")
## Installation (Carthage)
@@ -19,6 +19,8 @@ Configure your Cartfile to use `OGSwitch`:
```github "OskarGroth/OGSwitch" ~> 2.0```
`OGSwitch` requires Swift 4.2.
## Usage
Create a custom `NSView` in Interface Builder and set it's class to `OGSwitch`.
@@ -35,7 +37,7 @@ Inspired by ITSwitch by Ilija Tovilo.
## License
The MIT License (MIT)
Copyright (c) 2017 Oskar Groth
Copyright (c) 2018 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