Skip to content

Commit

Permalink
support rectForSegment public function
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Nov 3, 2016
1 parent 2fa12f7 commit fa24aae
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions SegmentedControl/SegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ open class SegmentedControl: UIControl {
}
}
open fileprivate(set) var isLongPressActivated = false
open var scrollContentInset: UIEdgeInsets {
return scrollView.contentInset
}
open var scrollContentSize: CGSize {
return scrollView.contentSize
}
open var scrollContentOffset: CGPoint {
return scrollView.contentOffset
}

fileprivate lazy var scrollView: SCScrollView = {
let scrollView = SCScrollView()
Expand Down Expand Up @@ -153,6 +162,25 @@ open class SegmentedControl: UIControl {
}
}

open func rectForSegment(at index: Int) -> CGRect? {
guard 0..<segmentsCount() ~= index else {
return nil
}
switch layoutPolicy {
case .fixed:
return CGRect(x: singleSegmentWidth() * CGFloat(index),
y: 0,
width: singleSegmentWidth(),
height: frame.height)
case .dynamic:
let frontWidths = totalSegmentsWidths(before: index)
return CGRect(x: contentInset.left + frontWidths.reduce(0, +) + segmentSpacing * CGFloat(frontWidths.count),
y: 0,
width: singleSegmentWidth(at: index),
height: frame.height)
}
}

// MARK: - Initialization
public override init(frame: CGRect) {
super.init(frame: frame)
Expand Down

0 comments on commit fa24aae

Please sign in to comment.