Skip to content

Commit

Permalink
优化,修复#12
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintGao committed Feb 24, 2021
1 parent 4968fa3 commit b67def2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion GKCycleScrollView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'GKCycleScrollView'
s.version = '1.0.5'
s.version = '1.0.6'
s.summary = '一个轻量级的自定义轮播图组件'
s.homepage = 'https://github.com/QuintGao/GKCycleScrollView'
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
9 changes: 8 additions & 1 deletion GKCycleScrollView/GKCycleScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ typedef NS_ENUM(NSUInteger, GKCycleScrollViewScrollDirection) {
@protocol GKCycleScrollViewDelegate <NSObject>

@optional
// 返回自定义cell尺寸
/// 返回自定义cell尺寸
/// @param cycleScrollView cycleScrollView description
- (CGSize)sizeForCellInCycleScrollView:(GKCycleScrollView *)cycleScrollView;

// cell滑动时调用
Expand All @@ -43,6 +44,12 @@ typedef NS_ENUM(NSUInteger, GKCycleScrollViewScrollDirection) {
// cell点击时调用
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView didSelectCellAtIndex:(NSInteger)index;

/// scrollView滚动中的回调
/// @param cycleScrollView cycleScrollView对象
/// @param fromIndex 正在滚动中,相对位置处于左边或上边的index,根据direction区分
/// @param toIndex 正在滚动中,相对位置处于右边或下边的index,根据direction区分
/// @param ratio 从左到右或从上到下计算的百分比,根据direction区分
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView scrollingFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex ratio:(CGFloat)ratio;
#pragma mark - UIScrollViewDelegate 相关
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView willBeginDragging:(UIScrollView *)scrollView;
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView didScroll:(UIScrollView *)scrollView;
Expand Down
62 changes: 57 additions & 5 deletions GKCycleScrollView/GKCycleScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ - (void)updateScrollViewAndCellSize {
}

- (void)setupCellsWithContentOffset:(CGPoint)offset {
if (self.showCount == 0) return;
if (self.cellSize.width <= 0 || self.cellSize.height <= 0) return;
//计算_visibleRange
CGPoint startPoint = CGPointMake(offset.x - self.scrollView.frame.origin.x, offset.y - self.scrollView.frame.origin.y);
Expand Down Expand Up @@ -413,6 +414,7 @@ - (void)setupCellsWithContentOffset:(CGPoint)offset {
}

- (void)updateVisibleCellAppearance {
if (self.showCount == 0) return;
if (self.cellSize.width <= 0 || self.cellSize.height <= 0) return;

switch (self.direction) {
Expand All @@ -438,7 +440,7 @@ - (void)updateVisibleCellAppearance {
leftRightInset = adjustLeftRightMargin * delta / self.cellSize.width;
topBottomInset = adjustTopBottomMargin * delta / self.cellSize.width;

NSInteger index = i % self.realCount;
NSInteger index = self.realCount == 0 ? 0 : i % self.realCount;
if (index == self.currentSelectIndex) {
[self.scrollView bringSubviewToFront:cell];
}
Expand Down Expand Up @@ -496,7 +498,7 @@ - (void)updateVisibleCellAppearance {
leftRightInset = adjustLeftRightMargin * delta / self.cellSize.height;
topBottomInset = adjustTopBottomMargin * delta / self.cellSize.height;

NSInteger index = i % self.realCount;
NSInteger index = self.realCount == 0 ? 0 : i % self.realCount;
if (index == self.currentSelectIndex) {
[self.scrollView bringSubviewToFront:cell];
}
Expand Down Expand Up @@ -699,9 +701,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self handleCellScrollWithIndex:index];
}

if ([self.delegate respondsToSelector:@selector(cycleScrollView:didScroll:)]) {
[self.delegate cycleScrollView:self didScroll:scrollView];
}
[self handleScrollViewDidScroll:scrollView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
Expand Down Expand Up @@ -765,6 +765,58 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}
}

- (void)handleScrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.delegate respondsToSelector:@selector(cycleScrollView:didScroll:)]) {
[self.delegate cycleScrollView:self didScroll:scrollView];
}

if ([self.delegate respondsToSelector:@selector(cycleScrollView:scrollingFromIndex:toIndex:ratio:)]) {
BOOL isFirstRevirse = NO; // 是否在第一个位置反向滑动
CGFloat ratio = 0; // 滑动百分比
if (self.direction == GKCycleScrollViewScrollDirectionHorizontal) {
CGFloat offsetX = scrollView.contentOffset.x;
CGFloat maxW = self.realCount * scrollView.bounds.size.width;

CGFloat changeOffsetX = self.isInfiniteLoop ? (offsetX - maxW) : offsetX;
if (changeOffsetX < 0) {
changeOffsetX = -changeOffsetX;
isFirstRevirse = YES;
}
ratio = (changeOffsetX / scrollView.bounds.size.width);
}else if (self.direction == GKCycleScrollViewScrollDirectionVertical) {
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat maxW = self.realCount * scrollView.bounds.size.height;

CGFloat changeOffsetY = self.isInfiniteLoop ? (offsetY - maxW) : offsetY;
if (changeOffsetY < 0) {
changeOffsetY = -changeOffsetY;
isFirstRevirse = YES;
}
ratio = (changeOffsetY / scrollView.bounds.size.height);
}
if (ratio > self.realCount || ratio < 0) return; // 越界,不作处理
ratio = MAX(0, MIN(self.realCount, ratio));
NSInteger baseIndex = floor(ratio);
if (baseIndex + 1 > self.realCount) {
// 右边越界了
baseIndex = 0;
}
CGFloat remainderRatio = ratio - baseIndex;
if (remainderRatio <= 0 || remainderRatio >= 1) return;
NSInteger toIndex = 0;
if (isFirstRevirse) {
baseIndex = self.realCount - 1;
toIndex = 0;
remainderRatio = 1 - remainderRatio;
}else if (baseIndex == self.realCount - 1) {
toIndex = 0;
}else {
toIndex = baseIndex + 1;
}
[self.delegate cycleScrollView:self scrollingFromIndex:baseIndex toIndex:toIndex ratio:remainderRatio];
}
}

#pragma mark - 懒加载
- (UIScrollView *)scrollView {
if (!_scrollView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (void)viewDidLoad {
// 默认样式:无缩放,自动轮播,无限轮播
GKCycleScrollView *cycleScrollView1 = [[GKCycleScrollView alloc] initWithFrame:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, kScreenW, 130)];
cycleScrollView1.dataSource = self;
cycleScrollView1.delegate = self;
[self.view addSubview:cycleScrollView1];
self.cycleScrollView1 = cycleScrollView1;

Expand Down Expand Up @@ -188,6 +189,7 @@ - (GKCycleScrollViewCell *)cycleScrollView:(GKCycleScrollView *)cycleScrollView

#pragma mark - GKCycleScrollViewDelegate
- (CGSize)sizeForCellInCycleScrollView:(GKCycleScrollView *)cycleScrollView {
if (cycleScrollView == self.cycleScrollView1) return CGSizeMake(kScreenW, 130);
if (cycleScrollView == self.cycleScrollView5) return CGSizeMake(kScreenW, 40);
return CGSizeMake(kScreenW - 100, 130);
}
Expand All @@ -205,4 +207,10 @@ - (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView didSelectCellAtInde
NSLog(@"cell点击,index=%zd", index);
}

- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView scrollingFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex ratio:(CGFloat)ratio {
if (cycleScrollView != self.cycleScrollView1) return;

NSLog(@"fromIndex:%zd,toIndex:%zd,ratio:%f", fromIndex, toIndex, ratio);
}

@end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ cycleScrollView.dataSource = self;

## 版本更新

* 2021.02.24 1.0.6 优化代码,防止crash,增加滑动百分比代理
* 2021.02.22 1.0.5 修复xib加载时可能导致的布局错乱问题
* 2021.01.27 1.0.3 修复某些情况下cell不能滑动的bug
* 2020.12.07 1.0.1 修复cell子视图点击处理bug
Expand Down

0 comments on commit b67def2

Please sign in to comment.