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

Supports UICollectionView to use custom Layout, and can configure UIC… #273

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 60 additions & 8 deletions Sources/Core/JXSegmentedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ open class JXSegmentedView: UIView, JXSegmentedViewRTLCompatible {
/// 点击切换的时候,contentScrollView的切换是否需要动画
open var isContentScrollViewClickTransitionAnimationEnabled: Bool = true

public enum Direction {
case horizontal
case vertical
}
open var customizeDirection: Direction?

var scrollPosition: UICollectionView.ScrollPosition {
if let customizeDirection {
switch customizeDirection {
case .horizontal:
return .centeredHorizontally
case .vertical:
return .centeredVertically
}
}
return .centeredHorizontally
}

private var itemDataSource = [JXSegmentedBaseItemModel]()
private var innerItemSpacing: CGFloat = 0
private var lastContentOffset: CGPoint = CGPoint.zero
Expand Down Expand Up @@ -350,11 +368,36 @@ open class JXSegmentedView: UIView, JXSegmentedViewRTLCompatible {
}
}

let minX: CGFloat = 0
let maxX = totalContentWidth - bounds.size.width
let targetX = selectedItemFrameX - bounds.size.width/2 + selectedItemWidth/2
collectionView.setContentOffset(CGPoint(x: max(min(maxX, targetX), minX), y: 0), animated: false)

if let customizeDirection {

let indexPath = IndexPath(row: selectedIndex,
section: 0)
if indexPath.item >= 0,
indexPath.section >= 0,
indexPath.section < collectionView.numberOfSections,
indexPath.item < collectionView.numberOfItems(inSection: indexPath.section) {

switch customizeDirection {
case .horizontal:
collectionView.selectItem(
at: indexPath,
animated: false,
scrollPosition: .centeredHorizontally)
case .vertical:
collectionView.selectItem(
at: indexPath,
animated: false,
scrollPosition: .centeredVertically)
}
}

} else {
let minX: CGFloat = 0
let maxX = totalContentWidth - bounds.size.width
let targetX = selectedItemFrameX - bounds.size.width/2 + selectedItemWidth/2
collectionView.setContentOffset(CGPoint(x: max(min(maxX, targetX), minX), y: 0), animated: false)
}

if contentScrollView != nil {
if contentScrollView!.frame.equalTo(CGRect.zero) &&
contentScrollView!.superview != nil {
Expand Down Expand Up @@ -562,14 +605,23 @@ open class JXSegmentedView: UIView, JXSegmentedViewRTLCompatible {
//延时为了解决cellwidth变化,点击最后几个cell,scrollToItem会出现位置偏移bu。需要等cellWidth动画渐变结束后再滚动到index的cell位置。
let selectedAnimationDurationInMilliseconds = Int((dataSource?.selectedAnimationDuration ?? 0)*1000)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(selectedAnimationDurationInMilliseconds)) {
self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
self.collectionView.scrollToItem(
at: IndexPath(item: index, section: 0),
at: self.scrollPosition,
animated: true)
}
}else if selectedType == .scroll {
//滚动选中的直接处理
collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
collectionView.scrollToItem(
at: IndexPath(item: index, section: 0),
at: self.scrollPosition,
animated: true)
}
}else {
collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
collectionView.scrollToItem(
at: IndexPath(item: index, section: 0),
at: self.scrollPosition,
animated: true)
}

if contentScrollView != nil && (selectedType == .click || selectedType == .code) {
Expand Down