From 511d88842b3d4f7cfd247efb01e45df9e6e2f69f Mon Sep 17 00:00:00 2001 From: alexvndre Date: Tue, 23 Apr 2019 18:23:20 +0200 Subject: [PATCH 1/3] Add border width --- DDAnimatedSwitch/DDSwitch.swift | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/DDAnimatedSwitch/DDSwitch.swift b/DDAnimatedSwitch/DDSwitch.swift index 221ea47..0c322f8 100644 --- a/DDAnimatedSwitch/DDSwitch.swift +++ b/DDAnimatedSwitch/DDSwitch.swift @@ -219,6 +219,16 @@ open class DDSwitch: UIControl { setup() } } + + /* + The thumb border width + Default is 1 + */ + open var thumbBorderWidth: CGFloat = 1 { + didSet { + setup() + } + } /* Inner shadow collor of the switch @@ -348,7 +358,8 @@ open class DDSwitch: UIControl { addInnerShadow() setupThumbShadow() setupThumbImage() - + setupThumbBorder() + addSubview(imageView) addSubview(thumb) @@ -357,13 +368,18 @@ 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 { @@ -371,6 +387,7 @@ open class DDSwitch: UIControl { } 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 From 7ab1e6fd53b88e8af13aa11431223ef303b985d7 Mon Sep 17 00:00:00 2001 From: alexvndre Date: Tue, 23 Apr 2019 18:29:19 +0200 Subject: [PATCH 2/3] Add thumbBorderColor --- DDAnimatedSwitch/DDSwitch.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/DDAnimatedSwitch/DDSwitch.swift b/DDAnimatedSwitch/DDSwitch.swift index 0c322f8..d727d69 100644 --- a/DDAnimatedSwitch/DDSwitch.swift +++ b/DDAnimatedSwitch/DDSwitch.swift @@ -220,6 +220,16 @@ open class DDSwitch: UIControl { } } + /* + The thumb border color + Default is UIColor.lightGray + */ + open var thumbBorderColor: UIColor = UIColor.lightGray { + didSet { + setup() + } + } + /* The thumb border width Default is 1 From 020cec33a8196e3f0c741c6c33b95c816bc576f8 Mon Sep 17 00:00:00 2001 From: alexvndre Date: Tue, 23 Apr 2019 19:01:22 +0200 Subject: [PATCH 3/3] Set border color during animation --- DDAnimatedSwitch/DDSwitch.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/DDAnimatedSwitch/DDSwitch.swift b/DDAnimatedSwitch/DDSwitch.swift index d727d69..c27ec65 100644 --- a/DDAnimatedSwitch/DDSwitch.swift +++ b/DDAnimatedSwitch/DDSwitch.swift @@ -348,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