Runkeeper design switch control

Overview

DGRunkeeperSwitch

Runkeeper design switch control (two part segment control) developed in Swift 2.0

Preview 1 Preview 2

Requirements

  • Xcode 7-beta or higher
  • iOS 8.0 or higher (May work on previous versions, just did not test it. Feel free to edit it).
  • ARC
  • Swift 3.0 (old versions are in different releases)

Demo

Open and run the DGRunkeeperSwitchExample project in Xcode to see DGRunkeeperSwitch in action.

Installation

Manual

All you need to do is drop DGRunkeeperSwitch.swift file into your project

CocoaPods

pod "DGRunkeeperSwitch", "~> 1.1.4"

Example usage

Using DGRunkeeperSwitch as a titleView

let runkeeperSwitch = DGRunkeeperSwitch(titles: ["Feed", "Leaderboard"])
runkeeperSwitch.backgroundColor = UIColor(red: 229.0/255.0, green: 163.0/255.0, blue: 48.0/255.0, alpha: 1.0)
runkeeperSwitch.selectedBackgroundColor = .white
runkeeperSwitch.titleColor = .white
runkeeperSwitch.selectedTitleColor = UIColor(red: 255.0/255.0, green: 196.0/255.0, blue: 92.0/255.0, alpha: 1.0)
runkeeperSwitch.titleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
runkeeperSwitch.frame = CGRect(x: 30.0, y: 40.0, width: 200.0, height: 30.0)
runkeeperSwitch.addTarget(self, action: #selector(ViewController.switchValueDidChange(sender:)), for: .valueChanged)
navigationItem.titleView = runkeeperSwitch

Using as a stand alone control

let runkeeperSwitch2 = DGRunkeeperSwitch()
runkeeperSwitch2.titles = ["Daily", "Weekly", "Monthly", "Yearly"]
runkeeperSwitch2.backgroundColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
runkeeperSwitch2.selectedBackgroundColor = .white
runkeeperSwitch2.titleColor = .white
runkeeperSwitch2.selectedTitleColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
runkeeperSwitch2.titleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
runkeeperSwitch2.frame = CGRect(x: 50.0, y: 20.0, width: view.bounds.width - 100.0, height: 30.0)
runkeeperSwitch2.autoresizingMask = [.flexibleWidth] // This is needed if you want the control to resize
view.addSubview(runkeeperSwitch2)

Contribution

You are welcome to fork and submit pull requests!

Contact

Danil Gontovnik

License

The MIT License (MIT)

Copyright (c) 2015 Danil Gontovnik

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.

Comments
  • Border radius not working when using autolayout in code

    Border radius not working when using autolayout in code

    In the example app, modify the ViewController.swift's runkeeperSwitch2 to be laid out using autolayout programmatically, like:

            let runkeeperSwitch2 = DGRunkeeperSwitch()
            runkeeperSwitch2.leftTitle = "Weekly"
            runkeeperSwitch2.rightTitle = "Monthly"
            runkeeperSwitch2.backgroundColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
            runkeeperSwitch2.selectedBackgroundColor = .whiteColor()
            runkeeperSwitch2.titleColor = .whiteColor()
            runkeeperSwitch2.selectedTitleColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
            runkeeperSwitch2.titleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
            //runkeeperSwitch2.frame = CGRect(x: 50.0, y: 20.0, width: view.bounds.width - 100.0, height: 30.0)
            //runkeeperSwitch2.autoresizingMask = [.FlexibleWidth]
            runkeeperSwitch2.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(runkeeperSwitch2)
    
            view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[switch]-20-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["switch":runkeeperSwitch2]))
            view.addConstraint(NSLayoutConstraint(item: runkeeperSwitch2, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 10))
            runkeeperSwitch2.addConstraint(NSLayoutConstraint(item: runkeeperSwitch2, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 30.0))
    

    And this is what happens in the iOS simulator:

    simulator screen shot dec 9 2015 12 27 03 am

    Do you have any idea why the border radius is not working using the above code?

    opened by king7532 6
  • add protocol

    add protocol

    I think add two protocol methods to communicate with the outside, so that before the switch state changes, give the user an opportunity to choose whether or not to change.

    opened by zmarvin 5
  • More than two options

    More than two options

    Really like what I see, but I would be interested in using DGRunkeeperSwitch with more than two options, more like a Segmented Control. Would be a great enhancement in my opinion!

    Best, Josef

    enhancement 
    opened by caloon 5
  • Pod install using 'use_frameworks!' option

    Pod install using 'use_frameworks!' option

    Hi! I'm trying to integrate your project with obj-c code, so i'm using option 'use_frameworks!' for pod install. When i import this module, none of classes are visible. That is because classes are not public. Could you please provide public access level for main classes?

    P.S. i know that there is already objective-c implementation, but i want to be sure that i'd have your last updates just in time. Thanks

    opened by dbulaienko 5
  • Can't create switch without titles

    Can't create switch without titles

    It fails with error *** Terminating app due to uncaught exception ‘CALayerInvalidGeometry’, reason: ‘CALayer position contains NaN: [nan 15.5]’ As a workaround - create with empty strings ("")

    opened by yakubbaev 2
  • Fix Compilation issues with XCode 6.4 and update settings to make the sample app universal.

    Fix Compilation issues with XCode 6.4 and update settings to make the sample app universal.

    Couple of recommendations:

    1. The project should have .gitignore file to ignore files generated during compilation.
    2. With XCode 6.4, the compilation of the app fails. a. DGRunkeeperSwitch/UIKit.UIControl:25:18: Non-failable initializer 'init(coder:)' overridden here b. DGRunkeeperSwitch/DGRunkeeperSwitch/DGRunkeeperSwitch.swift:207:20: Cannot invoke 'animateWithDuration' with an argument list of type '(NSTimeInterval, delay: Double, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: [UIViewAnimationOptions], animations: () -> Void, completion: (_) -> Void)' c. DGRunkeeperSwitch/DGRunkeeperSwitchExample/ViewController.swift:43:47: 'AnyObject.Protocol' does not have a member named 'FlexibleWidth'
    3. Also, it might be more convenient to set the target as Universal for the sample app and let auto layout do its job, rather than having 1X and 2X indicators while running on iPad.

    Cheers.

    opened by arjunkchr 2
  • Add a toggle mode like in UISwitch

    Add a toggle mode like in UISwitch

    It would be great to replicate UISwitch's behaviour. What do you think? The toggle mode flag is off by default so the existing projects will be untouched.

    opened by vvit 1
  • add ability to add another pan gesture recognizer

    add ability to add another pan gesture recognizer

    The idea of additional pan gesture recognizer is ability to pan on the other view to control the switch. i.e. if Switch is the navigation bar, this pull request allows to pan on the main view to switch between the too :)

    opened by romk1n 1
  • Two compiler Errors

    Two compiler Errors

    I am running this on lates xcode 7 beta 6.

    1. changed this super.init(frame: CGRect.zeroRect) to super.init(frame: CGRect.zero) 2.The compiler was unable to solve the state at line 239 (rightTitleLabelFrame). i had to break this into two lines let tempPoint = CGPoint(x: floor(bounds.size.width / 2.0 + (bounds.width / 2.0 - rightTitleLabelSize.width) / 2.0), y: floor((bounds.height - rightTitleLabelSize.height) / 2.0)) let rightTitleLabelFrame = CGRect(origin: tempPoint, size: rightTitleLabelSize)
    opened by irfanlone 1
  • Did you mean to put another version number

    Did you mean to put another version number

    I saw that you added swift support but when we try to add this in our pod file it gives an error.

    pod 'DGRunkeeperSwitch', '~> 1.1.4'

    did you mean to put version 1.1.3?

    opened by nevinjethmalani 0
  • Background color IB setting is overwritten

    Background color IB setting is overwritten

    Any setting of the background color in IB is ignored. This is because and IB-View is instantiated with initWithCoder: which sets the background color. After that finishInit is called which overwrites the setting.

    opened by sebastianludwig 0
  • KVO doesn't work on iOS 11

    KVO doesn't work on iOS 11

    On iOS 11 there are increased warnings related to KVO and the KVO code in DGRunkeeperSwitch.swift doesn't work. I get this assertion failure [MyTests.TableViewCellTest testOutlets] : failed: caught "NSUnknownKeyException", "[<DGRunkeeperSwitch.DGRunkeeperSwitch 0x7feec8220100> addObserver:<DGRunkeeperSwitch.DGRunkeeperSwitch 0x7feec8220100> forKeyPath:@"selectedBackgroundView.frame" options:1 context:0x0] was sent to an object that is not KVC-compliant for the "selectedBackgroundView" property."

    I fixed this problem as follows:

    In finishInit I changed to this

    selectedBackgroundView.addObserver(self, forKeyPath: "frame", options: .new, context: nil)

    In observeValue forKeyPath I changed to this

    override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let view = object as? UIView { if keyPath == "frame" && view == selectedBackgroundView { titleMaskView.frame = selectedBackgroundView.frame } } }

    and

    deinit {
        selectedBackgroundView.removeObserver(self, forKeyPath: "frame")
    }
    
    bug 
    opened by phoney 5
  • Add support for empty selection

    Add support for empty selection

    This PR closes #45.

    With this PR is it possible to do:

    runkeeperSwitch.setSelectedIndex(-1, animated: true)
    

    to unselect all the selected items.

    I don't know if this is the best approach, please let me know what do you think.

    opened by SandroMachado 0
  • Highlight the need for setting the autoresizingMask in the README

    Highlight the need for setting the autoresizingMask in the README

    In order for the control to have a flexible width and work with autolayout. You need to add

    runkeeperSwitch2.autoresizingMask = [.flexibleWidth]

    This is documented in the example project but not in the README. I can't the only one that missed this first time around.

    Happy to make a PR updating the README

    opened by jackcolley 0
Releases(1.1.4)
Owner
Danil Gontovnik
Ex SWE @ FB, MSQRD 🇬🇧 London
Danil Gontovnik
AKASegmentedControl is a fully customizable Segmented Control for iOS

#AKASegmentedControl AKASegmentedControl is a fully customizable Segmented Control for iOS ##Preview ##Usage Installation CocoaPods You can use CocoaP

Ali Karagoz 389 Sep 1, 2022
A segmented control with custom appearance and interactive animations. Written in Swift 3.0.

SJFluidSegmentedControl About If you are bored with using the default UISegmentedControl, this might save your day. SJFluidSegmentedControl is a custo

Sasho Jadrovski 904 Dec 30, 2022
A customizable Segmented Control for iOS. Supports text and image.

YUSegment 中文文档 A customizable segmented control for iOS. Features Supports both (Attributed)text and image Supports show separator Supports hide indic

YyGgQq 112 Jun 10, 2022
A Pinterest-like segment control with masking animation.

PinterestSegment A Pinterest-like segment control with masking animation. Requirements iOS 8.0+ Xcode 9.0 Swift 4.0 Installation CocoaPods You can use

TBXark 672 Dec 20, 2022
RCalendarPicker A date picker control, Calendar calendar control, select control, calendar, date selection, the clock selection control.

RCalendarPicker RCalendarPicker Calendar calendar control, select control, calendar, date selection, the clock selection control. 日历控件 ,日历选择控件,日历,日期选择

杜耀辉 131 Jul 18, 2022
A Customizable Switch UI for iOS, Inspired from Google's Material Design in Swift

MJMaterialSwitch Overview MJMaterialSwitch is google's material design like switch UI with animation features. This library has cool and sophisticated

Jaleel Nazir 64 Jul 29, 2022
A Swift material design UI module which paints over the parent view when the switch is on.

A Swift material design UI module which paints over the parent view when the switch is on. We specialize in the designing and coding of c

Ramotion 2.9k Dec 29, 2022
A Swift material design UI module which paints over the parent view when the switch is on.

:octocat: ?? RAMPaperSwitch is a Swift material design UI module which paints over the parent view when the switch is turned on. iOS library by @Ramotion

Ramotion 2.9k Dec 29, 2022
A Customizable Switch UI for iOS, Inspired from Google's Material Design

Overview JTMaterialSwitch is google's material design like switch UI with animation features. This library has cool and sophisticated animations, ripp

Junichi Tsurukawa 319 Nov 28, 2022
Nicely animated flat design switch alternative to UISwitch

AIFlatSwitch A smooth, nice looking and IBDesignable flat design switch for iOS. Can be used instead of UISwitch. Inspired by Creativedash's Dribbble

null 963 Jan 5, 2023
💊 An iOS switch control implemented in Swift with full Interface Builder support

' :::=== ::: === === ::: :::==== :::===== ::: === ' ::: ::: === === ::: :::==== ::: ::: === ' ===== === === === === ===

Thanh Pham 145 Dec 7, 2022
This "Calculator" application is a simple one screen design of calculator screen i have made this single screen design application just to practice AutoLayout concepts.

Calculator Layout This "Calculator" application is a simple one screen design of calculator screen i have made this single screen design application j

Chetan Parate 1 Oct 29, 2021
Control Room : a macOS app that lets you control the simulators for iOS, tvOS, and watchOS

Control Room is a macOS app that lets you control the simulators for iOS, tvOS, and watchOS – their UI appearance, status bar configuration, and more.

null 0 Nov 30, 2021
Instant font size adjustment with control – / control +

Plugins manipulating UI (like this one) are not supported by Xcode 8 UPDATE: to avoid conflict with Interface Builder hotkeys are changed to Control -

Sash Zats 271 Jul 30, 2022
XLPagerTabStrip is a Container View Controller that allows us to switch easily among a collection of view controllers

XLPagerTabStrip is a Container View Controller that allows us to switch easily among a collection of view controllers. Pan gesture can be used to move on to next or previous view controller. It shows a interactive indicator of the current, previous, next child view controllers.

xmartlabs 6.8k Dec 27, 2022
The best command-line tool to install and switch between multiple versions of Xcode.

The best command-line tool to install and switch between multiple versions of Xcode.

Robots and Pencils 2.3k Jan 9, 2023
Switshot is a game media manager helps you transfer your game media from Nintendo Switch to your phone, and manage your media just few taps.

Switshot is a game media manager helps you transfer your game media from Nintendo Switch to your phone, and manage your media just few taps.

Astrian Zheng 55 Jun 28, 2022
The easiest way to install and switch between multiple versions of Xcode - with a mouse click.

Xcodes.app The easiest way to install and switch between multiple versions of Xcode. If you're looking for a command-line version of Xcodes.app, try x

Robots and Pencils 4.5k Dec 26, 2022
μ-library enabling if/else and switch statements to be used as expressions.

swift-expression Many languages such as Scala, Rust and Kotlin support using if/else and switch statements as expressions – meaning that they can by t

Nikita Mounier 1 Nov 8, 2021