Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions DDAnimatedSwitch/DDSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ open class DDSwitch: UIControl {
setup()
}
}

/*
The thumb border color
Default is UIColor.lightGray
*/
open var thumbBorderColor: UIColor = UIColor.lightGray {
didSet {
setup()
}
}

/*
The thumb border width
Default is 1
*/
open var thumbBorderWidth: CGFloat = 1 {
didSet {
setup()
}
}

/*
Inner shadow collor of the switch
Expand Down Expand Up @@ -328,6 +348,7 @@ open class DDSwitch: UIControl {
isAnimating = true
UIView.animate(withDuration: TimeInterval(duration), animations: {
self.backgroundColor = self._isOn ? self.onTintColor : self.offTintColor
self.thumbBorderColor = self._isOn ? self.onTintColor : self.offTintColor
self.thumb.frame.origin.x = self._isOn ? self.onPoint.x : self.offPoint.x
if self.isThumbImageColorAnimate {
self.thumb.imageView.tintColor = self._isOn ? self.offTintColor : self.onTintColor
Expand All @@ -348,7 +369,8 @@ open class DDSwitch: UIControl {
addInnerShadow()
setupThumbShadow()
setupThumbImage()

setupThumbBorder()

addSubview(imageView)
addSubview(thumb)

Expand All @@ -357,20 +379,26 @@ open class DDSwitch: UIControl {
}

/// MARK: - Private

private func setupThumbShadow() {
thumb.layer.shadowColor = thumbShadowColor.cgColor
thumb.layer.shadowRadius = thumbShadowRadius
thumb.layer.shadowOpacity = thumbShadowOpacity
thumb.layer.shadowOffset = thumbShadowOffset
}

private func setupThumbShadow() {
thumb.layer.shadowColor = thumbShadowColor.cgColor
thumb.layer.shadowRadius = thumbShadowRadius
thumb.layer.shadowOpacity = thumbShadowOpacity
thumb.layer.shadowOffset = thumbShadowOffset
}

private func setupThumbBorder() {
thumb.layer.borderWidth = thumbBorderWidth
thumb.layer.borderColor = _isOn ? onTintColor.cgColor : offTintColor.cgColor
}

private func reset() {
guard isAnimating == false else {
return
}
layer.cornerRadius = bounds.size.height * cornerRadius
backgroundColor = _isOn ? onTintColor : offTintColor
thumbBorderColor = _isOn ? onTintColor : offTintColor
let aThumbSize = self.thumbSize != CGSize.zero ? self.thumbSize : CGSize(width: bounds.size.height - 2, height: bounds.height - 2)
let yPos = (self.bounds.size.height - aThumbSize.height) / 2

Expand Down