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

针对不一样宽度的指示器进行等宽度优化 #383

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
class PageControlExampleViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,FSPagerViewDataSource,FSPagerViewDelegate {

fileprivate let imageNames = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg"]
fileprivate let pageControlStyles = ["Default", "Ring", "UIImage", "UIBezierPath - Star", "UIBezierPath - Heart"]
fileprivate let pageControlStyles = ["Default", "Ring", "UIImage", "UIBezierPath - Star", "UIBezierPath - Heart", "Circle not same Width"]
fileprivate let pageControlAlignments = ["Right", "Center", "Left"]
fileprivate let sectionTitles = ["Style", "Item Spacing", "Interitem Spacing", "Horizontal Alignment"]

Expand Down Expand Up @@ -54,6 +54,13 @@ class PageControlExampleViewController: UIViewController,UITableViewDataSource,U
self.pageControl.setFillColor(color, for: .selected)
self.pageControl.setPath(self.heartPath, for: .normal)
self.pageControl.setPath(self.heartPath, for: .selected)
case 5:
self.pageControl.setPath(UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 8, height: 8), cornerRadius: 4), for: .normal)
self.pageControl.setPath(UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 12, height: 8), cornerRadius: 4), for: .selected)
self.pageControl.setStrokeColor(.red, for: .normal)
self.pageControl.setStrokeColor(.blue, for: .selected)
self.pageControl.setFillColor(.red, for: .normal)
self.pageControl.setFillColor(.blue, for: .selected)
default:
break
}
Expand Down
19 changes: 17 additions & 2 deletions Sources/FSPageControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ open class FSPageControl: UIControl {
return 0
case .center, .fill:
let midX = self.contentView.bounds.midX
if let norImage = self.images[.normal], let selImage = self.images[.selected] {
let totalWidth = CGFloat((numberOfPages - 1)) * (norImage.size.width + spacing) + selImage.size.width
let amplitude = (self.contentView.bounds.width - totalWidth) / 2.0
return amplitude
} else if let norPath = self.paths[.normal], let selPath = self.paths[.selected] {
let totalWidth = CGFloat((numberOfPages - 1)) * (norPath.bounds.size.width + spacing) + selPath.bounds.size.width
let amplitude = (self.contentView.bounds.width - totalWidth) / 2.0
return amplitude
}
let amplitude = CGFloat(self.numberOfPages/2) * diameter + spacing*CGFloat((self.numberOfPages-1)/2)
return midX - amplitude
case .right, .trailing:
Expand All @@ -125,9 +134,15 @@ open class FSPageControl: UIControl {
let state: UIControl.State = (index == self.currentPage) ? .selected : .normal
let image = self.images[state]
let size = image?.size ?? CGSize(width: diameter, height: diameter)
let origin = CGPoint(x: x - (size.width-diameter)*0.5, y: self.contentView.bounds.midY-size.height*0.5)
let origin = CGPoint(x: x, y: self.contentView.bounds.midY-size.height*0.5)
value.frame = CGRect(origin: origin, size: size)
x = x + spacing + diameter
if let image = self.images[state] {
x = x + spacing + image.size.width
} else if let path = self.paths[state] {
x = x + spacing + path.bounds.size.width
} else {
x = x + spacing + diameter
}
}

}
Expand Down