-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int currentIndex = scrollView.contentOffset.x/(NORMAL_VIEW_WIDTH+ITEM_SPACE);
if (currentIndex>_imageViewArray.count-2||currentIndex<0) {
return;
}
int rightIndex = currentIndex+1;
UIImageView *currentImageView = _imageViewArray[currentIndex];
UIImageView *rightImageView = _imageViewArray[rightIndex];
CGFloat scale = (scrollView.contentOffset.x-currentIndex*(NORMAL_VIEW_WIDTH+ITEM_SPACE))/(NORMAL_VIEW_WIDTH+ITEM_SPACE);
//NSLog(@"%f",scale);
CGFloat width = SELECT_VIEW_WIDTH-scale*(SELECT_VIEW_WIDTH-NORMAL_VIEW_WIDTH);
CGFloat height = SELECT_VIEW_HEIGHT-scale*(SELECT_VIEW_HEIGHT-NORMAL_VIEW_HEIGHT);
if (width<NORMAL_VIEW_WIDTH) {
width = NORMAL_VIEW_WIDTH;
}
if (height<NORMAL_VIEW_HEIGHT) {
height = NORMAL_VIEW_HEIGHT;
}
if (width>SELECT_VIEW_WIDTH) {
width = SELECT_VIEW_WIDTH;
}
if (height>SELECT_VIEW_HEIGHT) {
height = SELECT_VIEW_HEIGHT;
}
CGRect rect = CGRectMake(-(width-NORMAL_VIEW_WIDTH)/2, 0, width, height);
currentImageView.frame = rect;
width = NORMAL_VIEW_WIDTH+scale*(SELECT_VIEW_WIDTH-NORMAL_VIEW_WIDTH);
height = NORMAL_VIEW_HEIGHT+scale*(SELECT_VIEW_HEIGHT-NORMAL_VIEW_HEIGHT);
if (width<NORMAL_VIEW_WIDTH) {
width = NORMAL_VIEW_WIDTH;
}
if (height<NORMAL_VIEW_HEIGHT) {
height = NORMAL_VIEW_HEIGHT;
}
if (width>SELECT_VIEW_WIDTH) {
width = SELECT_VIEW_WIDTH;
}
if (height>SELECT_VIEW_HEIGHT) {
height = SELECT_VIEW_HEIGHT;
}
rect = CGRectMake(-(width-NORMAL_VIEW_WIDTH)/2, 0, width, height);
NSLog(@"%@",NSStringFromCGRect(rect));
rightImageView.frame = rect;
}
3、点击某一个Item,让Item处于中间选中状态。
-(void)clickImage:(UITapGestureRecognizer *)tap{
UIImageView *imageView = (UIImageView *)tap.view;
NSInteger tag = imageView.tag;
UIView *containerView = _viewArray[tag];
CGFloat offsetX = CGRectGetMidX(containerView.frame)-SCREEN_WIDTH/2;
[_scrollview scrollRectToVisible:CGRectMake(offsetX, 0, SCREEN_WIDTH, 120) animated:YES];
if (_delegate && [_delegate respondsToSelector:@selector(itemSelected:)]) {
[_delegate itemSelected:tag];
}
}
4、当用户在滑动结束,并具有初始速度的时候,当滑动停止的时候,我们需要把距离中间最近Item定位到最中间。
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int currentIndex = roundf(scrollView.contentOffset.x/(NORMAL_VIEW_WIDTH+ITEM_SPACE));
UIView *containerView = _viewArray[currentIndex];
CGFloat offsetX = CGRectGetMidX(containerView.frame)-SCREEN_WIDTH/2;
[_scrollview scrollRectToVisible:CGRectMake(offsetX, 0, SCREEN_WIDTH, 120) animated:YES];
if (_delegate && [_delegate respondsToSelector:@selector(itemSelected:)]) {
[_delegate itemSelected:currentIndex];
}
}
5、当用户在滑动结束的时候,但是没有初始速度的时候,此时不会触发-(void)scrollViewDidEndDecelerating:(UIScrollView )scrollView方法,我们需要在-(void)scrollViewDidEndDragging:(UIScrollView )scrollView willDecelerate:(BOOL)decelerate方法中,进行处理。










