Skip to content

Commit

Permalink
Merge pull request #4 from LinusGeffarth/master
Browse files Browse the repository at this point in the history
updated codebase to Swift 4
  • Loading branch information
kovpas authored Oct 12, 2018
2 parents f23cb56 + af21029 commit b2e3138
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 139 deletions.
96 changes: 48 additions & 48 deletions PMZSwitch/PMZISwitchAnimatedThumb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
private let thumbSignWidth = CGFloat(7)

internal class PMZISwitchAnimatedThumb: UIView {

// MARK: - Properties -
// MARK: internal
internal var animationProgress: CGFloat = 0 {
Expand All @@ -29,19 +29,19 @@ internal class PMZISwitchAnimatedThumb: UIView {
}
}

internal var thumbTintColor: UIColor = UIColor.whiteColor() {
internal var thumbTintColor: UIColor = .white {
willSet {
backgroundView.backgroundColor = isOn ? onThumbTintColor : newValue
}
}
internal var onThumbTintColor: UIColor = UIColor.whiteColor() {
internal var onThumbTintColor: UIColor = .white {
willSet {
backgroundView.backgroundColor = isOn ? newValue : thumbTintColor
}
}
internal var shadowColor: UIColor = UIColor.grayColor() {
internal var shadowColor: UIColor = .gray {
willSet {
backgroundView.layer.shadowColor = newValue.CGColor
backgroundView.layer.shadowColor = newValue.cgColor
}
}

Expand All @@ -54,54 +54,54 @@ internal class PMZISwitchAnimatedThumb: UIView {
private var thumbAnimationGroup: CAAnimationGroup!
private var isOn: Bool = false
private var isTracking: Bool = false

// MARK: - Lifecycle -
/**
* Initialization
*/
* Initialization
*/

required internal init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

override internal init(frame: CGRect) {
let squareRect = CGRectMake(0, 0, defaultFrame.height, defaultFrame.height)
let initialFrame = CGRectIsEmpty(frame) ? CGRectInset(squareRect, borderMargin, borderMargin) : frame
let squareRect = CGRect(x: 0, y: 0, width: defaultFrame.height, height: defaultFrame.height)
let initialFrame = frame.isEmpty ? squareRect.inset(by: UIEdgeInsets(top: borderMargin, left: borderMargin, bottom: borderMargin, right: borderMargin)) : frame
super.init(frame: initialFrame)
setup()
}

private func setup() {
backgroundView = UIView(frame: self.bounds)

backgroundView.layer.cornerRadius = self.frame.height * 0.5
backgroundView.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: backgroundView.layer.cornerRadius).CGPath
backgroundView.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: backgroundView.layer.cornerRadius).cgPath
backgroundView.layer.shadowRadius = 4.0
backgroundView.layer.shadowOpacity = 0
backgroundView.layer.shadowColor = shadowColor.CGColor
backgroundView.layer.shadowOffset = CGSizeMake(0, 7)
backgroundView.layer.shadowColor = shadowColor.cgColor
backgroundView.layer.shadowOffset = CGSize(width: 0, height: 7)
backgroundView.layer.masksToBounds = false

self.addSubview(backgroundView)
self.userInteractionEnabled = false
self.isUserInteractionEnabled = false

setupSignViews()
}

internal func toggle(animated: Bool) {
resetAnimations()
isTracking = false

isOn = !isOn

thumbSignView1.layer.speed = animated ? 1 : 0;
thumbSignView2.layer.speed = animated ? 1 : 0;
backgroundView.layer.speed = animated ? 1 : 0;

thumbSignView1.layer.addAnimation(animationGroup1, forKey: "tickAnimation")
thumbSignView2.layer.addAnimation(animationGroup2, forKey: "tickAnimation")
backgroundView.layer.addAnimation(thumbAnimationGroup, forKey: "thumbAnimation")
thumbSignView1.layer.add(animationGroup1, forKey: "tickAnimation")
thumbSignView2.layer.add(animationGroup2, forKey: "tickAnimation")
backgroundView.layer.add(thumbAnimationGroup, forKey: "thumbAnimation")
}

internal func startTracking() {
Expand All @@ -115,9 +115,9 @@ internal class PMZISwitchAnimatedThumb: UIView {
thumbSignView2.layer.speed = 0;
backgroundView.layer.speed = 0;

thumbSignView1.layer.addAnimation(animationGroup1, forKey: "tickAnimation")
thumbSignView2.layer.addAnimation(animationGroup2, forKey: "tickAnimation")
backgroundView.layer.addAnimation(thumbAnimationGroup, forKey: "thumbAnimation")
thumbSignView1.layer.add(animationGroup1, forKey: "tickAnimation")
thumbSignView2.layer.add(animationGroup2, forKey: "tickAnimation")
backgroundView.layer.add(thumbAnimationGroup, forKey: "thumbAnimation")
}

internal func endTracking(isOn: Bool) {
Expand All @@ -126,27 +126,27 @@ internal class PMZISwitchAnimatedThumb: UIView {
return
}
isTracking = false

thumbSignView1.layer.timeOffset = animationDuration
thumbSignView2.layer.timeOffset = animationDuration
backgroundView.layer.timeOffset = animationDuration
}

private func setupSignViews() {
thumbSignView1 = createThumbSignView()
thumbSignView1.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
thumbSignView1.transform = CGAffineTransform(rotationAngle: .pi/4)
self.addSubview(thumbSignView1);

thumbSignView2 = createThumbSignView()
thumbSignView2.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_4))
thumbSignView2.transform = CGAffineTransform(rotationAngle: -(.pi/4))
self.addSubview(thumbSignView2);
}

private func createThumbSignView() -> UIView {
let thumbSignHeight = self.bounds.height / 2 - thumbSignWidth
let thumbSignView = UIView(frame: CGRectMake(self.bounds.width / 2 - 4, self.bounds.height / 2 - thumbSignHeight / 2, thumbSignWidth, thumbSignHeight))
let thumbSignView = UIView(frame: CGRect(x: self.bounds.width / 2 - 4, y: self.bounds.height / 2 - thumbSignHeight / 2, width: thumbSignWidth, height: thumbSignHeight))
thumbSignView.layer.cornerRadius = thumbSignWidth / 2
thumbSignView.backgroundColor = UIColor.whiteColor()
thumbSignView.backgroundColor = .white

return thumbSignView
}
Expand All @@ -159,41 +159,41 @@ internal class PMZISwitchAnimatedThumb: UIView {
thumbSignView1.layer.timeOffset = 0
thumbSignView2.layer.timeOffset = 0
backgroundView.layer.timeOffset = 0

let fillMode = kCAFillModeForwards
let fillMode = CAMediaTimingFillMode.forwards
let thumbSignHeight = self.bounds.height / 2 - thumbSignWidth

let rotationAnimation1 = baseAnimation("transform.rotation", fromValue: CGFloat(M_PI_4), toValue: CGFloat(-M_PI + M_PI_4))
let scaleAnimation1 = baseAnimation("bounds.size.height", fromValue: thumbSignHeight, toValue: thumbSignHeight + 7)
let moveAnimation1 = baseAnimation("position", fromValue: NSValue(CGPoint: thumbSignView1.center), toValue: NSValue(CGPoint: CGPoint(x: thumbSignView1.center.x + 10, y: thumbSignView1.center.y + 4)))
let rotationAnimation1 = baseAnimation(keyPath: "transform.rotation", fromValue: Double.pi/4, toValue: -Double.pi + Double.pi/4)
let scaleAnimation1 = baseAnimation(keyPath: "bounds.size.height", fromValue: thumbSignHeight, toValue: thumbSignHeight + 7)
let moveAnimation1 = baseAnimation(keyPath: "position", fromValue: NSValue(cgPoint: thumbSignView1.center), toValue: NSValue(cgPoint: CGPoint(x: thumbSignView1.center.x + 10, y: thumbSignView1.center.y + 4)))

animationGroup1 = CAAnimationGroup()
animationGroup1.duration = animationDuration
animationGroup1.animations = [rotationAnimation1, scaleAnimation1, moveAnimation1]
animationGroup1.removedOnCompletion = false
animationGroup1.isRemovedOnCompletion = false
animationGroup1.fillMode = fillMode

let rotationAnimation2 = baseAnimation("transform.rotation", fromValue: CGFloat(-M_PI_4), toValue: CGFloat(-M_PI - M_PI_4))
let scaleAnimation2 = baseAnimation("bounds.size.height", fromValue: thumbSignHeight, toValue: thumbSignHeight / 2 + 6)
let moveAnimation2 = baseAnimation("position", fromValue: NSValue(CGPoint: thumbSignView2.center), toValue: NSValue(CGPoint: CGPoint(x: thumbSignView2.center.x - 25, y: thumbSignView2.center.y + 17)))
let rotationAnimation2 = baseAnimation(keyPath: "transform.rotation", fromValue: -(Double.pi/4), toValue: -Double.pi - Double.pi/4)
let scaleAnimation2 = baseAnimation(keyPath: "bounds.size.height", fromValue: thumbSignHeight, toValue: thumbSignHeight / 2 + 6)
let moveAnimation2 = baseAnimation(keyPath: "position", fromValue: NSValue(cgPoint: thumbSignView2.center), toValue: NSValue(cgPoint: CGPoint(x: thumbSignView2.center.x - 25, y: thumbSignView2.center.y + 17)))

animationGroup2 = CAAnimationGroup()
animationGroup2.duration = animationDuration
animationGroup2.animations = [rotationAnimation2, scaleAnimation2, moveAnimation2]
animationGroup2.removedOnCompletion = false
animationGroup2.isRemovedOnCompletion = false
animationGroup2.fillMode = fillMode

let shadowOpacityAnimation = baseAnimation("shadowOpacity", fromValue: 0, toValue: 0.5)
let bgColorAnimation = baseAnimation("backgroundColor", fromValue: thumbTintColor.CGColor, toValue: onThumbTintColor.CGColor)

let shadowOpacityAnimation = baseAnimation(keyPath: "shadowOpacity", fromValue: 0 as Any, toValue: 0.5)
let bgColorAnimation = baseAnimation(keyPath: "backgroundColor", fromValue: thumbTintColor.cgColor, toValue: onThumbTintColor.cgColor)
thumbAnimationGroup = CAAnimationGroup()
thumbAnimationGroup.duration = animationDuration
thumbAnimationGroup.animations = [shadowOpacityAnimation, bgColorAnimation]
thumbAnimationGroup.removedOnCompletion = false
thumbAnimationGroup.isRemovedOnCompletion = false
thumbAnimationGroup.fillMode = fillMode
}

func baseAnimation(keyPath: String, fromValue: AnyObject?, toValue: AnyObject?) -> CABasicAnimation {
func baseAnimation(keyPath: String, fromValue: Any?, toValue: Any?) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: keyPath)
animation.duration = animationDuration
animation.fromValue = isOn ? toValue : fromValue
Expand Down
Loading

0 comments on commit b2e3138

Please sign in to comment.