Code Monkey home page Code Monkey logo

mactoggle's People

Contributors

ailinykh avatar richappz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mactoggle's Issues

Did Change Event

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!

Setting the Toggle Height

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! :)

MacToggle and storyboard

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.

capture d ecran 2017-01-26 a 10 55 46

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 }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.