diff --git a/CHIPageControl/CHIPageControlJaloro.swift b/CHIPageControl/CHIPageControlJaloro.swift index 8af1f85..65e78aa 100755 --- a/CHIPageControl/CHIPageControlJaloro.swift +++ b/CHIPageControl/CHIPageControlJaloro.swift @@ -27,7 +27,7 @@ import UIKit open class CHIPageControlJaloro: CHIBasePageControl { - @IBInspectable open var elementWidth: CGFloat = 20 { + @IBInspectable open var elementWidth: CGFloat = 6 { didSet { setNeedsLayout() } @@ -39,15 +39,31 @@ open class CHIPageControlJaloro: CHIBasePageControl { } } + @IBInspectable open var activeElementWidth: CGFloat = 10 { + didSet { + setNeedsLayout() + } + } + + @IBInspectable open var activeElementHeight: CGFloat = 6 { + didSet { + setNeedsLayout() + } + } + fileprivate var inactive = [CHILayer]() fileprivate var active = CHILayer() required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) + + radius = 3 } public override init(frame: CGRect) { super.init(frame: frame) + + radius = 3 } override func updateNumberOfPages(_ count: Int) { @@ -67,44 +83,54 @@ open class CHIPageControlJaloro: CHIBasePageControl { override open func layoutSubviews() { super.layoutSubviews() - + let floatCount = CGFloat(inactive.count) - let x = (self.bounds.size.width - self.elementWidth*floatCount - self.padding*(floatCount-1))*0.5 + let x = (self.bounds.size.width - self.elementWidth*(floatCount - 1) - self.activeElementWidth - self.padding*(floatCount - 1))*0.5 let y = (self.bounds.size.height - self.elementHeight)*0.5 - var frame = CGRect(x: x, y: y, width: self.elementWidth, height: self.elementHeight) + let activeframe = CGRect(x: x, y: y, width: self.activeElementWidth, height: self.activeElementHeight) active.cornerRadius = self.radius active.backgroundColor = (self.currentPageTintColor ?? self.tintColor)?.cgColor - active.frame = frame + active.frame = activeframe - inactive.enumerated().forEach() { index, layer in + var inactiveFrame = CGRect(x: x, y: y, width: self.elementWidth, height: self.elementHeight) + + inactive.enumerated().forEach { index, layer in layer.backgroundColor = self.tintColor(position: index).withAlphaComponent(self.inactiveTransparency).cgColor if self.borderWidth > 0 { layer.borderWidth = self.borderWidth layer.borderColor = self.tintColor(position: index).cgColor } layer.cornerRadius = self.radius - layer.frame = frame - frame.origin.x += self.elementWidth + self.padding + layer.frame = inactiveFrame + let width = index == 0 ? self.activeElementWidth : self.elementWidth + inactiveFrame.origin.x += width + self.padding } update(for: progress) } override func update(for progress: Double) { guard let min = inactive.first?.frame, - let max = inactive.last?.frame, progress >= 0 && progress <= Double(numberOfPages - 1), numberOfPages > 1 else { return } let total = Double(numberOfPages - 1) - let dist = max.origin.x - min.origin.x let percent = CGFloat(progress / total) + let floatCount = CGFloat(inactive.count) + let dist = self.elementWidth*(floatCount - 1) + self.padding*(floatCount-1) let offset = dist * percent active.frame.origin.x = min.origin.x + offset + var x = min.origin.x + let activeIndex = Int(progress.rounded()) + inactive.enumerated().forEach() { index, layer in + layer.frame.origin.x = x + let width = activeIndex == index ? self.activeElementWidth : self.elementWidth + x += width + self.padding + } } override open var intrinsicContentSize: CGSize { @@ -112,7 +138,7 @@ open class CHIPageControlJaloro: CHIBasePageControl { } override open func sizeThatFits(_ size: CGSize) -> CGSize { - return CGSize(width: CGFloat(inactive.count) * self.elementWidth + CGFloat(inactive.count - 1) * self.padding, + return CGSize(width: CGFloat(inactive.count - 1) * self.elementWidth + self.activeElementWidth + CGFloat(inactive.count - 1) * self.padding, height: self.elementHeight) }