目标,侧边图,先缩小,再调整间距
比较麻烦的是, 自定义的 banner视图回收机制 和 坐标换算(因为里面有一个视图层级关系)
- 重写 _scrollViewDen.frame , 覆盖原来的
- [_dataSource numberOfPagesInFlowView:self] * 3; 我觉得 多了,
应该是 ,多于两张视图的情况下, +1 ,就好了
- (void)layoutSubviews{
[super layoutSubviews];
//重置pageCount
if (_dataSource && [_dataSource respondsToSelector:@selector(numberOfPagesInFlowView:)]) {
//原始页数
self.orginPageCount = [_dataSource numberOfPagesInFlowView:self];
//总页数
_pageCount = self.orginPageCount == 1 ? 1: [_dataSource numberOfPagesInFlowView:self] * 3;
//我觉得 多了, 应该是 ,多于两张视图的情况下, +1 ,就好了
if (self.pageControl && [self.pageControl respondsToSelector:@selector(setNumberOfPages:)]) {
[self.pageControl setNumberOfPages:self.orginPageCount];
}
}
//重置pageWidth
if (_delegate && [_delegate respondsToSelector:@selector(sizeForPageInFlowView:)]) {
_pageSize = [_delegate sizeForPageInFlowView:self];
_sideCellWidth = _pageSize.width * kZoomInPageScale + 2*kInteritemSpacing ;
}
[_reusableCells removeAllObjects];
_visibleRange = NSMakeRange(0, 0);
//填充cells数组
[_mutableCells removeAllObjects];
for (NSInteger index=0; index<_pageCount; index++){
[_mutableCells addObject:[NSNull null]];
}
_scrollViewDen.frame = CGRectMake(0, 0, _pageSize.width, _pageSize.height); // 这行代码 ,涉及坐标转换, 很 关键 Deng
CGFloat contentWidthDen = _sideCellWidth * (_pageCount - 1) + _pageSize.width;
_scrollViewDen.contentSize = CGSizeMake(contentWidthDen, _pageSize.height);
CGPoint theCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
_scrollViewDen.center = theCenter;
if (self.orginPageCount > 1) {
//不仅仅 只有一张图 , 滚到第二组
[_scrollViewDen setContentOffset: CGPointMake(_sideCellWidth, 0) animated:NO];
self.pageIndex = self.orginPageCount;
}
[self toSetPagesAtContentOffset: _scrollViewDen.contentOffset];//根据当前scrollView的offset设置cell
[self refreshVisibleCellAppearance];//更新各个可见Cell的显示外貌
}
想要先缩小侧边图,再调整间距, 感觉ScrollView 的 不足(先天不足),他是这么算的。
CGRect originCellFrame = CGRectMake(_sideCellWidth * i, 0, _pageSize.width, _pageSize.height);//如果没有缩小效果的情况下的本该的Frame
调整ScrollView上面的间距, 就是 修改addSubView的 view的originX,
不应该用乘法,应该用加法, 至少乘法需要条件判断cellIndexBanner.frame = UIEdgeInsetsInsetRect(originCellFrame, UIEdgeInsetsMake(insetV, kInteritemSpacing, insetV, kInteritemSpacing));
所以感觉ScrollView addSubView 的 centerPoint没变, 以此为中心,缩进缩小。
以此库设置inter item spacing 困难, 需重新设计
- (void)refreshVisibleCellAppearance{
CGFloat offset = _scrollViewDen.contentOffset.x;
NSUInteger startIndex = self.visibleRange.location;
NSUInteger endIndex = self.visibleRange.location + _visibleRange.length;
for (NSUInteger i = startIndex; i < endIndex; i++) {
WSIndexBanner *cellIndexBanner = [_mutableCells objectAtIndex:i];
CGFloat origin = cellIndexBanner.frame.origin.x;
CGFloat delta = fabs(origin - offset);
CGRect originCellFrame = CGRectMake(_sideCellWidth * i, 0, _pageSize.width, _pageSize.height);//如果没有缩小效果的情况下的本该的Frame
CGFloat insetV = _pageSize.width * (1 - kZoomInPageScale) / 2.0 ;
if (delta < _pageSize.width) {
cellIndexBanner.coverView.alpha = (delta / _pageSize.width) * (1 - _minimumPageAlpha);
insetV = insetV * (delta / _sideCellWidth);
cellIndexBanner.frame = UIEdgeInsetsInsetRect(originCellFrame, UIEdgeInsetsMake(insetV, kInteritemSpacing, insetV, kInteritemSpacing));
cellIndexBanner.mainImageView.frame = cellIndexBanner.bounds;
} else {
cellIndexBanner.coverView.alpha = _minimumPageAlpha;
cellIndexBanner.frame = UIEdgeInsetsInsetRect(originCellFrame, UIEdgeInsetsMake(insetV, kInteritemSpacing, insetV, kInteritemSpacing));
cellIndexBanner.mainImageView.frame = cellIndexBanner.bounds;
}
}
}
或许 UICollectionFlowLayout , 会合适一些