卡片式无限自动轮播源码参考
scrollView的属性设置及解释
1.pagingEnable = yes ; 开启分页模式,(ps为达到分页效果,可以设置contentSize为subView的整数倍width等)
(iOS 自定义scrollView的pagingEnabled http://blog.csdn.net/liuxu0718/article/details/48344295)
2.clipsToBounds = NO; 关闭剪切图片,达到轮播的效果 (UIView里面都是默认开启的,只有UIScrollView默认关闭
3.ScrollsToTop = YES; 该属性在垂直滚动时, 如果为YES则 点击状态栏会使UIScrollView滚动回初始位置
(谈一谈UIScrollView的scrollsToTop属性 http://www.jianshu.com/p/b4ac529950af)
4
CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用情况
(http://blog.csdn.net/ys410900345/article/details/42924827)
无限轮播的实现">第一部分:无限轮播的实现
下面是我画的图解,希望大家能看懂
RotateScrollview类的源码
仅用于解释上面的过程,复制过去可以自己实现效果
源码
#import "RotateScrollview.h"
@interface RotateScrollview ()<UIScrollViewDelegate>
/**
* 原始页数
*/
@property(nonatomic, assign) NSInteger orginPageCount;
/**
* 总页数
*/
@property(nonatomic, assign) NSInteger pageCount;
/**
* 一页的尺寸
*/
@property(nonatomic, assign) CGSize pageSize;
/**
左右间距,默认20
*/
@property(nonatomic, assign) CGFloat leftRightMargin;
/**
上下间距,默认30
*/
@property(nonatomic, assign) CGFloat topBottomMargin;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation RotateScrollview
@synthesize orginPageCount;
@synthesize pageCount;
@synthesize pageSize;
@synthesize leftRightMargin;
@synthesize topBottomMargin;
-(instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:frame]) {
[self initsilize];
}return self;
}
- (void)initsilize{
leftRightMargin = 10;
topBottomMargin = 15;
pageSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
orginPageCount = 5;
pageCount = orginPageCount*3;
self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.showsVerticalScrollIndicator = YES;
self.scrollView.showsHorizontalScrollIndicator = YES;
[self addSubview:self.scrollView];
for (int i = 0; i<15; i++) {
UIImageView *imgV = [[UIImageView alloc] initWithFrame:self.bounds];
//第一组
if (i<=4) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i]];
imgV.image = image;
}
//第二组
if (i<=10&&i>4) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i-5]];
imgV.image = image;
}
//第三组
if (i<15&&i>9) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i-10]];
imgV.image = image;
}
imgV.frame = CGRectMake(pageSize.width*i, 0, pageSize.width, pageSize.height);
[self.scrollView addSubview:imgV];
}
self.scrollView.contentSize = CGSizeMake(pageSize.width*pageCount, 0);
//设置scrollview的当前显示状态为 第二组
[self.scrollView setContentOffset:CGPointMake(pageSize.width*orginPageCount*2, 0)];
}
#pragma - mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//获取拖拽的下标
NSInteger pageIndex;
pageIndex = (int)round(_scrollView.contentOffset.x / pageSize.width) % orginPageCount;
//向左滑动实现无限滚动
if (scrollView.contentOffset.x / pageSize.width >= 2 * self.orginPageCount) {
NSLog(@"%.f",scrollView.contentOffset.x / pageSize.width);
//回到
[scrollView setContentOffset:CGPointMake(pageSize.width * self.orginPageCount, 0) animated:NO];
}
//向右滑动实现无限滚动
if (scrollView.contentOffset.x/ pageSize.width < self.orginPageCount -1) {
[scrollView setContentOffset:CGPointMake(pageSize.width * (self.orginPageCount*2-1), 0) animated:NO];
}
}
第二部分:卡片式动画的实现
(节后来补, ^(* ̄(oo) ̄)^大家节日快乐)