17 Commits

Author SHA1 Message Date
Alex.k 9c8d992237 Fixes #14 2016-05-31 12:48:46 +03:00
Alex.k e43d214be4 update podspec 2016-05-30 15:42:40 +03:00
Alex.k 7e010e5a0a Fixes #14 2016-05-30 14:47:22 +03:00
Alex 36d563d17b Update README.md 2016-03-23 09:44:48 +03:00
Alex.k 0b96f702e1 update podspec 2016-03-23 09:37:18 +03:00
Alex.k eedba1996f Fixed #13 2016-03-23 09:36:02 +03:00
Alex af5929b834 Update README.md 2016-03-14 09:35:49 +03:00
Alex.k d732302363 update podspec 2016-03-14 09:29:05 +03:00
Alex dc86cd2b03 Merge pull request #11 from joeblau/master
Make class public so it's accessable programmatically without Storyboards
2016-03-14 09:26:02 +03:00
Joe Blau 7b1159d718 Added color support on initialization 2016-03-13 13:49:20 -04:00
Joe Blau 3fcee77c2e Added view based initialization 2016-03-13 13:22:53 -04:00
Joe Blau 5b3d07a65f Make class public so it's accessable programmatically without Storyboards 2016-03-13 12:05:06 -04:00
Juri Vasylenko ef935a478c fixed typo in header.png name 2016-02-15 11:37:47 +03:00
Juri Vasylenko 865d501bca add header 2016-02-15 11:36:14 +03:00
Juri Vasylenko c866fb10f3 Update README.md 2016-02-01 12:14:12 +03:00
Juri Vasylenko 839fb5e072 Update README.md 2016-02-01 12:13:51 +03:00
Juri Vasylenko f3d5182872 Merge pull request #9 from Ramotion/docs_update
Docs update
2016-02-01 10:06:24 +03:00
5 changed files with 178 additions and 135 deletions
+170 -128
View File
@@ -22,137 +22,179 @@
import UIKit
class RAMPaperSwitch: UISwitch {
@IBInspectable var duration: Double = 0.35
var animationDidStartClosure = {(onAnimation: Bool) -> Void in }
var animationDidStopClosure = {(onAnimation: Bool, finished: Bool) -> Void in }
private var shape: CAShapeLayer! = CAShapeLayer()
private var radius: CGFloat = 0.0
private var oldState = false
public class RAMPaperSwitch: UISwitch {
override var on: Bool {
didSet(oldValue) {
oldState = on
}
}
struct Constants {
static let scale = "transform.scale"
static let up = "scaleUp"
static let down = "scaleDown"
}
override func setOn(on: Bool, animated: Bool) {
let changed:Bool = on != self.on
super.setOn(on, animated: animated)
if changed {
if animated {
switchChanged()
} else {
showShapeIfNeed()
}
}
}
override func layoutSubviews() {
let x:CGFloat = max(frame.midX, superview!.frame.size.width - frame.midX);
let y:CGFloat = max(frame.midY, superview!.frame.size.height - frame.midY);
radius = sqrt(x*x + y*y);
shape.frame = CGRectMake(frame.midX - radius, frame.midY - radius, radius * 2, radius * 2)
shape.anchorPoint = CGPointMake(0.5, 0.5);
shape.path = UIBezierPath(ovalInRect: CGRectMake(0, 0, radius * 2, radius * 2)).CGPath
}
override func awakeFromNib() {
let shapeColor: UIColor = onTintColor ?? UIColor.greenColor()
layer.borderWidth = 0.5
layer.borderColor = UIColor.whiteColor().CGColor;
layer.cornerRadius = frame.size.height / 2;
shape.fillColor = shapeColor.CGColor
shape.masksToBounds = true
superview?.layer.insertSublayer(shape, atIndex: 0)
superview?.layer.masksToBounds = true
showShapeIfNeed()
addTarget(self, action: "switchChanged", forControlEvents: UIControlEvents.ValueChanged)
super.awakeFromNib()
}
@IBInspectable var duration: Double = 0.35
private func showShapeIfNeed() {
shape.transform = on ? CATransform3DMakeScale(1.0, 1.0, 1.0) : CATransform3DMakeScale(0.0001, 0.0001, 0.0001)
}
var animationDidStartClosure = {(onAnimation: Bool) -> Void in }
var animationDidStopClosure = {(onAnimation: Bool, finished: Bool) -> Void in }
private var shape: CAShapeLayer! = CAShapeLayer()
private var radius: CGFloat = 0.0
private var oldState = false
internal func switchChanged() {
if on == oldState {
return;
}
oldState = on
if on {
CATransaction.begin()
shape.removeAnimationForKey("scaleDown")
let scaleAnimation:CABasicAnimation = animateKeyPath("transform",
fromValue: NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
toValue:NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
timing:kCAMediaTimingFunctionEaseIn);
shape.addAnimation(scaleAnimation, forKey: "scaleUp")
CATransaction.commit();
}
else {
CATransaction.begin()
shape.removeAnimationForKey("scaleUp")
let scaleAnimation:CABasicAnimation = animateKeyPath("transform",
fromValue: NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
toValue:NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
timing:kCAMediaTimingFunctionEaseOut);
shape.addAnimation(scaleAnimation, forKey: "scaleDown")
CATransaction.commit();
}
}
private func animateKeyPath(keyPath: String, fromValue from: AnyObject, toValue to: AnyObject, timing timingFunction: String) -> CABasicAnimation {
let animation:CABasicAnimation = CABasicAnimation(keyPath: keyPath)
animation.fromValue = from
animation.toValue = to
animation.repeatCount = 1
animation.timingFunction = CAMediaTimingFunction(name: timingFunction)
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.duration = duration;
animation.delegate = self
return animation;
}
//CAAnimation delegate
override func animationDidStart(anim: CAAnimation){
animationDidStartClosure(on)
}
override func animationDidStop(anim: CAAnimation, finished flag: Bool){
animationDidStopClosure(on, flag)
private var defaultTintColor: UIColor?
private var parentView: UIView?
override public var on: Bool {
didSet(oldValue) {
oldState = on
}
}
// MARK: - Initialization
public required init(view: UIView?, color: UIColor?) {
super.init(frame: CGRectZero)
onTintColor = color
self.commonInit(view)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func awakeFromNib() {
self.commonInit(superview)
super.awakeFromNib()
}
}
// MARK: public
public extension RAMPaperSwitch {
override public func setOn(on: Bool, animated: Bool) {
let changed:Bool = on != self.on
super.setOn(on, animated: animated)
if changed {
switchChangeWithAniatiom(animated)
}
}
}
// MARK: Helpers
extension RAMPaperSwitch {
private func commonInit(parentView: UIView?) {
guard let onTintColor = self.onTintColor else {
fatalError("set tint color")
}
self.parentView = parentView
self.defaultTintColor = parentView?.backgroundColor
layer.borderWidth = 0.5
layer.borderColor = UIColor.whiteColor().CGColor;
layer.cornerRadius = frame.size.height / 2;
shape.fillColor = onTintColor.CGColor
shape.masksToBounds = true
parentView?.layer.insertSublayer(shape, atIndex: 0)
parentView?.layer.masksToBounds = true
showShapeIfNeed()
addTarget(self, action: #selector(RAMPaperSwitch.switchChanged), forControlEvents: UIControlEvents.ValueChanged)
}
override public func layoutSubviews() {
let x:CGFloat = max(frame.midX, superview!.frame.size.width - frame.midX);
let y:CGFloat = max(frame.midY, superview!.frame.size.height - frame.midY);
radius = sqrt(x*x + y*y);
shape.frame = CGRectMake(frame.midX - radius, frame.midY - radius, radius * 2, radius * 2)
shape.anchorPoint = CGPointMake(0.5, 0.5);
shape.path = UIBezierPath(ovalInRect: CGRectMake(0, 0, radius * 2, radius * 2)).CGPath
}
// MARK: - Private
private func showShapeIfNeed() {
shape.transform = on ? CATransform3DMakeScale(1.0, 1.0, 1.0) : CATransform3DMakeScale(0.0001, 0.0001, 0.0001)
}
internal func switchChanged() {
switchChangeWithAniatiom(true)
}
}
// MARK: animations
extension RAMPaperSwitch {
private func animateKeyPath(keyPath: String, fromValue from: CGFloat?, toValue to: CGFloat, timing timingFunction: String) -> CABasicAnimation {
let animation:CABasicAnimation = CABasicAnimation(keyPath: keyPath)
animation.fromValue = from
animation.toValue = to
animation.repeatCount = 1
animation.timingFunction = CAMediaTimingFunction(name: timingFunction)
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.duration = duration;
animation.delegate = self
return animation;
}
private func switchChangeWithAniatiom(animation: Bool) {
guard let onTintColor = self.onTintColor else {
fatalError("set tint color")
}
if on == oldState {
return;
}
oldState = on
shape.fillColor = onTintColor.CGColor
if on {
let scaleAnimation:CABasicAnimation = animateKeyPath(Constants.scale,
fromValue: 0.01,
toValue: 1.0,
timing:kCAMediaTimingFunctionEaseIn);
if animation == false { scaleAnimation.duration = 0.0001 }
shape.addAnimation(scaleAnimation, forKey: Constants.up)
}
else {
let scaleAnimation:CABasicAnimation = animateKeyPath(Constants.scale,
fromValue: 1.0,
toValue: 0.01,
timing:kCAMediaTimingFunctionEaseOut);
if animation == false { scaleAnimation.duration = 0.0001 }
shape.addAnimation(scaleAnimation, forKey: Constants.down)
}
}
//MARK: - CAAnimation Delegate
override public func animationDidStart(anim: CAAnimation){
parentView?.backgroundColor = defaultTintColor
animationDidStartClosure(on)
}
override public func animationDidStop(anim: CAAnimation, finished flag: Bool){
print(flag)
if flag == true {
parentView?.backgroundColor = on == true ? onTintColor : defaultTintColor
}
animationDidStopClosure(on, flag)
}
}
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ViewController" customModule="PaperSwitchDemo" customModuleProvider="target">
+1 -1
View File
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = 'RAMPaperSwitch'
s.version = '0.0.1'
s.version = '1.0.0'
s.summary = 'Swift subclass of the UISwitch which paints over the parent view'
s.homepage = 'https://github.com/Ramotion/paper-switch'
s.license = 'MIT'
+5 -4
View File
@@ -1,3 +1,4 @@
![header](./header.png)
#RAMPaperSwitch
[![CocoaPods](https://img.shields.io/cocoapods/p/RAMPaperSwitch.svg)](https://cocoapods.org/pods/RAMPaperSwitch)
[![CocoaPods](https://img.shields.io/cocoapods/v/RAMPaperSwitch.svg)](http://cocoapods.org/pods/RAMPaperSwitch)
@@ -5,7 +6,7 @@
[![Twitter](https://img.shields.io/badge/Twitter-@Ramotion-blue.svg?style=flat)](http://twitter.com/Ramotion)
[![Travis](https://img.shields.io/travis/Ramotion/paper-switch.svg)](https://travis-ci.org/Ramotion/paper-switch)
Swift subclass of the UISwitch which paints over the parent view with the `onTintColor` when the switch is turned on. Implemented concept from [this Dribbble](https://dribbble.com/shots/1749645-Contact-Sync) shot by [Ramotion](http://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch).
Swift subclass of the UISwitch which paints over the parent view with the `onTintColor` when the switch is turned on. Implemented concept from [this Dribbble](https://dribbble.com/shots/1749645-Contact-Sync) shot by [Ramotion](https://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch).
#Screenshot
@@ -26,7 +27,7 @@ Just add the `RAMPaperSwitch` folder to your project.
or use [CocoaPods](https://cocoapods.org) with Podfile:
``` ruby
pod 'RAMPaperSwitch', '~> 0.0.1'
pod 'RAMPaperSwitch', '~> 0.0.3'
```
@@ -57,8 +58,8 @@ self.paperSwitch.animationDidStartClosure = {(onAnimation: Bool) in
```
## About
The project maintained by [app development agency](http://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch) [Ramotion Inc.](http://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch)
See our other [open-source projects](https://github.com/ramotion) or [hire](http://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch) us to design, develop, and grow your product.
The project maintained by [app development agency](https://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch) [Ramotion Inc.](https://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch)
See our other [open-source projects](https://github.com/ramotion) or [hire](https://ramotion.com?utm_source=gthb&utm_medium=special&utm_campaign=paper-switch) us to design, develop, and grow your product.
[![Twitter URL](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=https://github.com/ramotion/paper-switch)
[![Twitter Follow](https://img.shields.io/twitter/follow/ramotion.svg?style=social)](https://twitter.com/ramotion)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB