Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add differential dot modifier between current with normal. #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 37 additions & 11 deletions CHIPageControl/CHIPageControlJaloro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import UIKit

open class CHIPageControlJaloro: CHIBasePageControl {

@IBInspectable open var elementWidth: CGFloat = 20 {
@IBInspectable open var elementWidth: CGFloat = 6 {
didSet {
setNeedsLayout()
}
Expand All @@ -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) {
Expand All @@ -67,52 +83,62 @@ 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 {
return sizeThatFits(CGSize.zero)
}

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)
}

Expand Down