richappz / mactoggle Goto Github PK
View Code? Open in Web Editor NEWMacToggle bringing design to OS X Applications
Home Page: richappz.com
License: MIT License
MacToggle bringing design to OS X Applications
Home Page: richappz.com
License: MIT License
How do I detect the event when the toggle changes? I'm trying to change some settings as the user interacts with the toggle.
Thanks!
Thanks for putting this together! It looks great.
I am instantiating my MacToggle in a storyboard, and I noticed that I can't set the width or height on it (nor can I use auto layout constraints).
Is there a way to set the height somehow when I'm not using the MacToggle(height: X)
initializer? I know I can hard-code the values in your class, but I'd rather not do that.
Thanks! :)
it's me again
I think I miss information/something
How to link MacToggle to storyboard / nib ??
and i have another problem
Detected missing constraints for <graphPG.MacToggle: 0x112ce18f0>. It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.
and
import Cocoa
@IBDesignable
class MacToggle: NSView {
//================================================================================
// MARK: Properties
//================================================================================
fileprivate var height: CGFloat
fileprivate let width: CGFloat
fileprivate var leftConstraint: NSLayoutConstraint?
fileprivate var heightConstraint: NSLayoutConstraint?
fileprivate var widthConstraint: NSLayoutConstraint?
fileprivate let backVw: NSView = {
let view = NSView()
view.wantsLayer = true
view.layer?.masksToBounds = false
return view
}()
fileprivate let circle: NSView = {
let view = NSView()
let shadow = NSShadow()
shadow.shadowColor = NSColor.black.withAlphaComponent(0.4)
shadow.shadowOffset = CGSize(width: 0, height: -2)
shadow.shadowBlurRadius = 2
view.backgroundColor = .white
view.wantsLayer = true
view.shadow = shadow
view.layer?.borderWidth = 2
view.layer?.borderColor = NSColor.white.cgColor
return view
}()
fileprivate var _radius: CGFloat?
fileprivate var backRadius: CGFloat {
get {
if let r = _radius { return r }
return height/2
}
}
fileprivate var circleRadius: CGFloat {
get {
if let r = _radius { return r-outlineWidth }
return (height-(outlineWidth*2))/2
}
}
fileprivate var toggleSize: CGFloat {
get { return height-(outlineWidth*2) }
}
//================================================================================
// MARK: Callback
//================================================================================
var callback: ((_ isOn: Bool) -> Void)?
//================================================================================
// MARK: Public Parameters
//================================================================================
// MARK: - IBInspectable
**@IBInspectable** var isOn = false {
didSet { animate() }
}
/// Change the toggle border on and off
**@IBInspectable** var hasToggleBorder = true {
didSet { circle.layer?.borderWidth = hasToggleBorder ? toggleBorderWidth : 0 }
}
/// Change the width of the outline border
@IBInspectable var outlineWidth: CGFloat = 2 {
didSet {
backVw.layer?.borderWidth = outlineWidth
layoutSwitch(resetingLayout: true)
}
}
/// Change the width of the border on the toggle
@IBInspectable var toggleBorderWidth: CGFloat = 2 {
didSet { circle.layer?.borderWidth = hasToggleBorder ? toggleBorderWidth : 0 }
}
/// Change the radius of the complete toggle
@IBInspectable var radius: CGFloat {
get {
if let r = _radius { return r }
return (height-(outlineWidth*2))/2
}
set {
_radius = newValue
layoutSwitch()
}
}
/// Change the color of the outline border
@IBInspectable var outlineColor: NSColor = .lightGray {
didSet { backVw.layer?.borderColor = outlineColor.cgColor }
}
/// Change the color of the fill when the toggle is on
@IBInspectable var fillColor: NSColor = .lightGray {
didSet { if isOn { backVw.layer?.borderColor = fillColor.cgColor } }
}
/// Change the color of the toggle center
@IBInspectable var toggleColor: NSColor = .white {
didSet { circle.backgroundColor = toggleColor }
}
/// Change the background color of the complete toggle (visible when switch is off)
@IBInspectable var backColor: NSColor = .white {
didSet { backVw.backgroundColor = backColor }
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.