您的位置:首页 > 其它

bug调试-卡片轮播反转漏白边

2015-12-07 00:17 267 查看

bug调试-卡片轮播反转漏白边

项目中做了一个卡片滚动的效果,使用scrollView, 自己写的重用.

但是有个bug:顺序滚动没问题,往回滚第三张会漏出白边:

_**


一开始以为是动画没做好:

- (void)refreshVisibleCellAppearance{

if (_minimumPageAlpha == 1.0 && _minimumPageScale == 1.0) {
return;//无需更新
}

CGFloat offset = _scrollView.contentOffset.x;

if (_cells.count > 0) {
for (NSInteger i = _visibleRange.location; i < _visibleRange.location + _visibleRange.length; i++) {

UIView *cell = [_cells objectAtIndex:i];
CGFloat origin = cell.center.x;
CGFloat delta = fabs(origin - _pageSize.width / 2 - offset);

CGRect originCellFrame = CGRectMake(_pageSize.width * i, 0, _pageSize.width, _pageSize.height);//如果没有缩小效果的情况下的本该的Frame

[UIView beginAnimations:@"CellAnimation" context:nil];
**if (delta < _pageSize.width) {
CGFloat inset = _pageSize.width * (1 - _minimumPageScale) * (delta / _pageSize.width) / 2.0;
//                    CGFloat inset = _pageSize.width * (1 - _minimumPageScale) / 2.0 ;
cell.frame = UIEdgeInsetsInsetRect(originCellFrame, UIEdgeInsetsMake(inset, 7.5, inset, 7.5));

} else {
CGFloat inset = _pageSize.width * (1 - _minimumPageScale) / 2.0 ;
cell.frame = UIEdgeInsetsInsetRect(originCellFrame, UIEdgeInsetsMake(inset, 7.5, inset, 7.5));** // 开始以为是这段代码有问题
}
[UIView commitAnimations];
}
}
}


注释掉怀疑的代码仍然不能修正bug

后来关注下面的代码:

{

//计算_visibleRange

CGPoint startPoint = CGPointMake(offset.x - _scrollView.frame.origin.x, offset.y - _scrollView.frame.origin.y);
CGPoint endPoint = CGPointMake(startPoint.x + self.bounds.size.width, startPoint.y + self.bounds.size.height);

NSInteger startIndex = 0;
for (NSInteger i =0; i < [_cells count]; i++) {
if (_pageSize.width * (i) > startPoint.x) {
startIndex = i;
break;
}
}

NSInteger endIndex = startIndex;
for (NSInteger i = startIndex; i < [_cells count]; i++) {
//如果都不超过则取最后一个
if ((_pageSize.width * (i + 1) < endPoint.x && _pageSize.width * (i + 2) >= endPoint.x) || i+ 2 == [_cells count]) {
endIndex = i + 1;//i+2 是以个数,所以其index需要减去1
break;
}
}

//可见页分别向前向后扩展一个,提高效率


// startIndex = MAX(startIndex - 2, 0);

// 我将下面的代码改成 startIndex = MAX(startIndex, 0);发现是这样的情况



// 偏移的图片在正中间了,于是我改成 startIndex = MAX(startIndex - 2, 0);

// 搞定!Ohyeah!

startIndex = MAX(startIndex - 1, 0);

endIndex = MIN(endIndex + 1, [_cells count] - 1);

_visibleRange.location = startIndex;

_visibleRange.length = endIndex - startIndex + 1;

NSLog(@"%ld===========%ld", startIndex, endIndex);

for (NSInteger i = startIndex; i <= endIndex; i++) {
[self setPageAtIndex:i];
}

for (NSInteger i = 0; i < startIndex; i ++) {
[self removeCellAtIndex:i];
}


// NSLog(@”=================%@”, _cells);

for (NSInteger i = endIndex + 1; i < [_cells count]; i ++) {

[self removeCellAtIndex:i];

}

}

最终效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bug