Skip to content

Commit

Permalink
Bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Feb 1, 2017
1 parent 1ceb179 commit fbdbb18
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 148 deletions.
4 changes: 2 additions & 2 deletions SwitcherCollection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = TBXark.TKSwitcherCollection;
PRODUCT_BUNDLE_IDENTIFIER = com.TBXark.TKSwitcherCollection;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -456,7 +456,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = TBXark.TKSwitcherCollection;
PRODUCT_BUNDLE_IDENTIFIER = com.TBXark.TKSwitcherCollection;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion SwitcherCollection/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3.0</string>
<string>1.3.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
3 changes: 2 additions & 1 deletion SwitcherCollection/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ViewController: UIViewController {
}

func animateSwitch(_ timer:Timer){
switchArray[count].changeValue()
let switcher = switchArray[count]
switcher.setOn(switcher.isOn, animate: true)
count += 1
if count == (switchArray.count){
count = 0
Expand Down
2 changes: 1 addition & 1 deletion TKSwitcherCollection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TKSwitcherCollection"
s.version = "1.3.1"
s.version = "1.3.3"
s.summary = "A animate switcher cllection in Swift."
s.license = { :type => 'MIT License', :file => 'LICENSE' } # 协议
s.homepage = "https://github.com/TBXark/TKSwitcherCollection"
Expand Down
2 changes: 1 addition & 1 deletion TKSwitcherCollection/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.0</string>
<string>1.3.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
55 changes: 31 additions & 24 deletions TKSwitcherCollection/TKBaseSwitcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

import UIKit

public typealias ValueChangeHook = (_ value: Bool) -> Void


protocol TKViewScale {}
extension TKViewScale where Self: UIView {
var sizeScale: CGFloat {
return min(self.bounds.width, self.bounds.height)/100.0
}
}
public typealias TKSwitchValueChangeHook = (_ value: Bool) -> Void

// 自定义 Switch 基类
open class TKBaseSwitch: UIControl {

// MARK: - Property
open var valueChange : ValueChangeHook?
open var valueChange : TKSwitchValueChangeHook?
open var animateDuration : Double = 0.4

open var isOn : Bool { return on }
internal var on : Bool = true
internal var sizeScale: CGFloat {
return min(self.bounds.width, self.bounds.height)/100.0
}


open override var frame: CGRect {
didSet {
guard frame.size != oldValue.size else { return }
Expand All @@ -40,21 +38,32 @@ open class TKBaseSwitch: UIControl {
}

// MARK: - Getter
open var isOn : Bool{
return on
}
internal var on : Bool = true


func setOn(_ on: Bool, animate: Bool) {
if on != isOn {
changeValue()
}
final public func setOn(_ on: Bool, animate: Bool = true) {
guard on != isOn else { return }
changeValue()

}


// MARK: - Init
convenience public init() {
self.init(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
}

override public init(frame: CGRect) {
super.init(frame: frame)
setUpView()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpView()
}


// MARK: - Internal

internal func resetView() {
gestureRecognizers?.forEach(self.removeGestureRecognizer)
Expand All @@ -67,12 +76,10 @@ open class TKBaseSwitch: UIControl {
self.addGestureRecognizer(tap)
}

public func changeValue(){
if valueChange != nil{
valueChange!(isOn)
}
sendActions(for: UIControlEvents.valueChanged);
internal func changeValue(){
on = !on
valueChange?(!isOn)
sendActions(for: UIControlEvents.valueChanged);
}

}
Expand Down
30 changes: 11 additions & 19 deletions TKSwitcherCollection/TKExchangeSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import UIKit
// Dedign by Oleg Frolov
//https://dribbble.com/shots/2238916-Switcher-VI

open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
open class TKExchangeSwitch: TKBaseSwitch {

// MARK: - Property
fileprivate var swichControl : TKExchangeCircleView!
fileprivate var swichControl : TKExchangeCircleView?
fileprivate var backgroundLayer = CAShapeLayer()

open var color = (background: UIColor(white: 0.95, alpha: 1),
Expand All @@ -37,15 +37,6 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
}

// MARK: - Init
override public init(frame: CGRect) {
super.init(frame: frame)
setUpView()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpView()
}

// MARK: - Private Func
override internal func setUpView(){
Expand All @@ -56,21 +47,22 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {

let backLayerPath = UIBezierPath()
backLayerPath.move(to: CGPoint(x: lineWidth, y: 0))
backLayerPath.addLine(to: CGPoint(x: self.bounds.width - 4 * lineWidth, y: 0))
backLayerPath.addLine(to: CGPoint(x: bounds.width - 4 * lineWidth, y: 0))

backgroundLayer.position = position
backgroundLayer.fillColor = color.background.cgColor
backgroundLayer.strokeColor = color.background.cgColor
backgroundLayer.lineWidth = self.bounds.height
backgroundLayer.lineCap = kCALineCapRound
backgroundLayer.path = backLayerPath.cgPath
self.layer.addSublayer(backgroundLayer)
layer.addSublayer(backgroundLayer)

let swichRadius = self.bounds.height - lineWidth
swichControl = TKExchangeCircleView(frame: CGRect(x: lineWidth/2, y: lineWidth/2, width: swichRadius, height: swichRadius))
let swichRadius = bounds.height - lineWidth
let swichControl = TKExchangeCircleView(frame: CGRect(x: lineWidth/2, y: lineWidth/2, width: swichRadius, height: swichRadius))
swichControl.onLayer.fillColor = color.on.cgColor
swichControl.offLayer.fillColor = color.off.cgColor
self.addSubview(swichControl)
addSubview(swichControl)
self.swichControl = swichControl
}


Expand All @@ -83,7 +75,7 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
open func changeValueAnimate(_ turnOn:Bool, duration:Double){

let keyTimes = [0,0.4,0.6,1]
var frame = self.swichControl.frame
guard var frame = self.swichControl?.frame else { return }
frame.origin.x = turnOn ? lineWidth/2 : (self.bounds.width - self.bounds.height + lineWidth/2)

let swichControlStrokeStartAnim = CAKeyframeAnimation(keyPath:"strokeStart")
Expand All @@ -105,10 +97,10 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
swichControlChangeStateAnim.duration = duration

backgroundLayer.add(swichControlChangeStateAnim, forKey: "SwitchBackground")
swichControl.exchangeAnimate(turnOn, duration: duration)
swichControl?.exchangeAnimate(turnOn, duration: duration)

UIView.animate(withDuration: duration, animations: { () -> Void in
self.swichControl.frame = frame
self.swichControl?.frame = frame
})
}
}
Expand Down
46 changes: 12 additions & 34 deletions TKSwitcherCollection/TKLiquidSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,18 @@ import UIKit
// Dedign by Oleg Frolov
//https://dribbble.com/shots/2028065-Switcher-lll

open class TKLiquidSwitch: TKBaseSwitch, TKViewScale {
open class TKLiquidSwitch: TKBaseSwitch {


fileprivate var bubbleLayer = CAShapeLayer()
fileprivate var lineLayer = CAShapeLayer()
fileprivate var color = (on: UIColor(red:0.373, green:0.843, blue:0.596, alpha:1),
off: UIColor(red:0.871, green:0.871, blue:0.871, alpha:1)) {
off: UIColor(red:0.871, green:0.871, blue:0.871, alpha:1)) {
didSet {
setUpView()
}
}

override public init(frame: CGRect) {
super.init(frame: frame)
setUpView()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpView()
}


open override func draw(_ rect: CGRect) {
super.draw(rect)

}

override internal func setUpView() {
super.setUpView()
Expand All @@ -56,43 +41,36 @@ open class TKLiquidSwitch: TKBaseSwitch, TKViewScale {

}

override public func changeValue() {
override internal func changeValue() {
super.changeValue()
changeStateAnimate(isOn, duration: self.animateDuration)
}


func changeStateAnimate(_ turnOn:Bool,duration:Double){



let bubbleTransformAnim = CAKeyframeAnimation(keyPath:"transform")
bubbleTransformAnim.values = [NSValue(caTransform3D: CATransform3DIdentity),
NSValue(caTransform3D: CATransform3DMakeScale(1, 0.8, 1)),
NSValue(caTransform3D: CATransform3DMakeScale(0.8, 1, 1)),
NSValue(caTransform3D: CATransform3DIdentity)]
bubbleTransformAnim.keyTimes = [NSNumber(value: 0), NSNumber(value: 1.0 / 3.0), NSNumber(value: 2.0 / 3.0), NSNumber(value: 1)]
bubbleTransformAnim.duration = duration
bubbleTransformAnim.duration = animateDuration

let bubblePositionAnim = CABasicAnimation(keyPath: "position")
bubblePositionAnim.fromValue = NSValue(cgPoint: bubblePosition(!isOn))
bubblePositionAnim.toValue = NSValue(cgPoint: bubblePosition(isOn))
bubblePositionAnim.duration = duration
bubblePositionAnim.duration = animateDuration

let bubbleGroupAnim = CAAnimationGroup()
bubbleGroupAnim.animations = [bubbleTransformAnim,bubblePositionAnim]
bubbleGroupAnim.isRemovedOnCompletion = false
bubbleGroupAnim.fillMode = kCAFillModeForwards
bubbleGroupAnim.duration = duration
bubbleGroupAnim.duration = animateDuration



bubbleLayer.add(bubbleGroupAnim, forKey: "Bubble")

let color = switchColor(isOn).cgColor
UIView.animate(withDuration: duration, animations: { () -> Void in
UIView.animate(withDuration: animateDuration, animations: { () -> Void in
self.lineLayer.fillColor = color
self.bubbleLayer.fillColor = color
})
})
}
}

Expand Down Expand Up @@ -130,7 +108,7 @@ extension TKLiquidSwitch{
// let cL = CGPoint(x: sR, y: lR)
let cC = CGPoint(x: sR * 2 + lR, y: lR)
// let cR = CGPoint(x: sR * 3 + lR * 2, y: lR)

bubblePath.move(to: l1)
bubblePath.addQuadCurve(to: c1, controlPoint: o1)
bubblePath.addArc(withCenter: cC, radius: lR, startAngle: -CGFloat.pi/2, endAngle: CGFloat.pi*3/2, clockwise: true)
Expand All @@ -145,7 +123,7 @@ extension TKLiquidSwitch{
return bubblePath.cgPath
}

func switchColor(_ isON:Bool)-> UIColor{
func switchColor(_ isOn: Bool)-> UIColor {
if isOn{
return color.on
}
Expand All @@ -154,7 +132,7 @@ extension TKLiquidSwitch{
}
}

func bubblePosition(_ isOn:Bool) -> CGPoint{
func bubblePosition(_ isOn :Bool) -> CGPoint{
let h = self.bounds.height
let w = self.bounds.width

Expand Down
Loading

0 comments on commit fbdbb18

Please sign in to comment.