自定义一个自动滚动的SCrollView
来源:互联网 发布:淘宝店铺发布二手产品 编辑:程序博客网 时间:2024/06/01 08:03
如简单的ScrollView先定义ScrollView的属性
@property (nonatomic , copy) NSInteger (^totalPagesCount)(void); //总的页面个数
@property (nonatomic , copy) UIView *(^fetchContentViewAtIndex)(NSInteger pageIndex);//获取第pageIndex个位置的contentView
@property (nonatomic , copy) void (^TapActionBlock)(NSInteger pageIndex);//当点击的时候,执行的block
@property (nonatomic , readonly) UIScrollView *scrollView;
初始化方法
//fram ->fram @param animationDuration 自动滚动的间隔时长。如果<=0,不自动滚动。
- (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration;
定义所需要的一些属性
@property (nonatomic , assign) NSInteger currentPageIndex;//当前页面的Index@property (nonatomic , assign) NSInteger totalPageCount; //总的页面数@property (nonatomic , strong) NSMutableArray *contentViews;//创建可变数组装视图@property (nonatomic , strong) UIScrollView *scrollView;@property (nonatomic , strong) NSTimer *animationTimer;//滚动时间间隔@property (nonatomic , assign) NSTimeInterval animationDuration; 获取时间间隔同样先初始化
- (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration
{
self = [self initWithFrame:frame];
if (animationDuration > 0.0) {
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
target:self
selector:@selector(animationTimerDidFired:)
userInfo:nil
repeats:YES];
[self.animationTimer pauseTimer];
}
return self;
}
//设置总页面的个数
- (void)setTotalPagesCount:(NSInteger (^)(void))totalPagesCount
{
_totalPageCount = totalPagesCount();
if (_totalPageCount > 0) {
[self configContentViews];
[self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
}
}
初始化ScrollView 里面的一些方法在上一篇文中有提到
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.autoresizesSubviews = YES;
self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
self.scrollView.autoresizingMask = 0xFF;
self.scrollView.contentMode = UIViewContentModeCenter;
self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
self.scrollView.delegate = self;
self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0);
self.scrollView.pagingEnabled = YES;
[self addSubview:self.scrollView];
self.currentPageIndex = 0;
}
return self;
}
设置Scrollview上的content 数据 及contentView的个数
- (void)setScrollViewContentDataSource
{
NSInteger previousPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
if (self.contentViews == nil) {
self.contentViews = [@[] mutableCopy];
}
[self.contentViews removeAllObjects];
if (self.fetchContentViewAtIndex) {
[self.contentViews addObject:self.fetchContentViewAtIndex(previousPageIndex)];
[self.contentViews addObject:self.fetchContentViewAtIndex(_currentPageIndex)];
[self.contentViews addObject:self.fetchContentViewAtIndex(rearPageIndex)];
}
}
//获取下一页面的index
- (NSInteger)getValidNextPageIndexWithPageIndex:(NSInteger)currentPageIndex;
{
if(currentPageIndex == -1) {
return self.totalPageCount - 1;
} else if (currentPageIndex == self.totalPageCount) {
return 0;
} else {
return currentPageIndex;
}
}
//动画开始执行timer
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.animationTimer pauseTimer];
}
//手指离开屏幕后ScrollView还会继续滚动一段时间只到停止
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
}
//滚动方向
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int contentOffsetX = scrollView.contentOffset.x;
if(contentOffsetX >= (2 * CGRectGetWidth(scrollView.frame))) {
self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
NSLog(@"next,当前页:%d",self.currentPageIndex);
[self configContentViews];
}
if(contentOffsetX <= 0) {
self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
NSLog(@"previous,当前页:%d",self.currentPageIndex);
[self configContentViews];
}
}
设置contentoffset是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:YES];
}
#pragma mark -
#pragma mark - 响应事件
- (void)animationTimerDidFired:(NSTimer *)timer
{
CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);
[self.scrollView setContentOffset:newOffset animated:YES];
}
点击的视图肯定就是显示出来的视图
- (void)contentViewTapAction:(UITapGestureRecognizer *)tap
{
if (self.TapActionBlock) {
self.TapActionBlock(self.currentPageIndex);
}
}
在ViewController里面实现
NSMutableArray *viewsArray = [@[] mutableCopy];
NSArray *colorArray = @[[UIColor cyanColor],[UIColor blueColor],[UIColor greenColor],[UIColor yellowColor],[UIColor purpleColor]];
for (int i = 0; i < 5; ++i) {
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
tempLabel.backgroundColor = [(UIColor *)[colorArray objectAtIndex:i] colorWithAlphaComponent:0.5];
[viewsArray addObject:tempLabel];
}
self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 300) animationDuration:2];
self.mainScorllView.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
return viewsArray[pageIndex];
};
self.mainScorllView.totalPagesCount = ^NSInteger(void){
return 5;
};
self.mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
NSLog(@"点击了第%d个",pageIndex);
};
[self.view addSubview:self.mainScorllView];
创建另一个类继承自NSTimer
定义方法
- (void)pauseTimer;
- (void)resumeTimer;
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval;
实现方法
-(void)pauseTimer
{
if (![self isValid]) {
return ;
}
[self setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer
{
if (![self isValid]) {
return ;
}
[self setFireDate:[NSDate date]];
}
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval
{
if (![self isValid]) {
return ;
}
[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}
- 自定义一个自动滚动的SCrollView
- 一个有两秒轮询滚动的scrollView
- iOS中scrollview自动滚动的实现
- iOS ScrollView自动滚动的代码
- 控制ScrollView的自动滚动速度
- iOS中scrollview自动滚动的实现
- iOS中scrollview自动滚动的实现
- ScrollView的使用以及自动滚动
- QML中ScrollView的自动滚动
- 进入界面的时候Scrollview自动滚动
- iOS中scrollview自动滚动的实现
- scrollview 自动滚动
- 禁止ScrollView自动滚动
- ScrollView自动滚动实现
- 禁止ScrollView自动滚动
- 自定义一个简单的ScrollView
- 一个scrollView无法滚动的原因
- 自定义ScrollView+自定义滚动条
- C/C++随机数的产生
- TCP/IP详解卷2:实现 第二章mbuf:存储器缓存 笔记二
- 机器学习综述——机器学习理论基础与发展脉络
- Java编程中“为了性能”需做的26件事
- java leetode Container With Most Water
- 自定义一个自动滚动的SCrollView
- 延时显示
- HDU1001
- C++访问控制权限
- Java Web 服务性能优化实践
- Best Time to Buy and Sell Stock III
- 数据库事务隔离级别
- Linux学习笔记一
- 我的ACM之快速排序