#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
UIPageControl *_pageControl;
NSTimer *_timer;
int _speed;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//创建
[self createScrollView];
//创建pagecontrol
UIPageControl *pageControl = [self pageControl];
[self.view addSubview:pageControl];
_speed = 1;
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ontimer) userInfo:nil repeats:YES];
}
#pragma mark --- createScrollview
-(void)createScrollView
{
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
//设置滚动视图内容的大小
_scrollView.contentSize = CGSizeMake(320*7, 200);
for (int a = 0; a< 7; a++)
{
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(320*a, 0, 320, 200)];
NSString *imagestr = [NSString stringWithFormat:@"%d",a+1];
imageview.image = [UIImage imageNamed:imagestr];
[_scrollView addSubview:imageview];
}
//设置是否隐藏水平指示条或者竖直指示条
//Indicator:指示器
//Vertical:垂直方向
// _scrollView.showsVerticalScrollIndicator = NO;
// _scrollView.showsHorizontalScrollIndicator = NO;
//设置指示条的样式
// _scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
//设置scrollView内容的偏移量
//x: x方向上的偏移量 正值:往左发生偏移 负值:往右偏移
//y:y方向上的偏移量 正值:往上偏移 负值:往下偏移
//scrollView.contentOffset = CGPointMake(0, -100);
//设置是否开启回弹的效果
_scrollView.bounces = NO;
//设置scrollView是否分页显示
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
}
#pragma mark --- creatpageControl
//页面指示器
-(UIPageControl *)pageControl
{
_pageControl= [[UIPageControl alloc] initWithFrame:CGRectMake(100, 200, 130, 20)];
//设置当前选中圆点颜色
_pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
//设置未选中的圆点颜色
_pageControl.pageIndicatorTintColor = [UIColor blueColor];
//设置pageControl的圆点个数
_pageControl .numberOfPages = 7;
[_pageControl addTarget:self action:@selector(pageMove) forControlEvents:UIControlEventValueChanged];
return _pageControl;
}
#pragma mark --- pageCOntrol的帮定方法
-(void)pageMove
{
//随着currentpage的值不断地改变,我们要及时的修改图片的偏移量
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
}
#pragma mark----timer
-(void)ontimer
{
//currentPage属性设置小圆点的位置
if (_pageControl.currentPage == 6)
{
_speed = -1;
}
if (_pageControl.currentPage == 0)
{
_speed = 1;
}
_pageControl.currentPage = _pageControl.currentPage + _speed;
//改变偏移量带有动画的方法
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
}
#pragma mark --- UIScrollViewDelegate
//将要开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[_timer invalidate];
_timer = nil;
}
//拖拽结束后减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//手动滑动图片会修改滚动试图偏移量,偏移量改变,让currentPage的值也跟着改变;
_pageControl.currentPage = _scrollView.contentOffset.x/320;
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ontimer) userInfo:nil repeats:YES];
}
@end
滚动视图+分页控制器+定时器
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在屏幕上显示视图控制器有两种方法:嵌入到容器视图控制器中或者present这个视图控制器。容器视图控制器提供应用的...
- 翻译自“View Controller Programming Guide for iOS”。 1 定义子类 使用...