轮播图

2017-09-21  本文已影响0人  9898a

//创建数组

imgArr = [NSMutableArray arrayWithObjects:@"a.JPEG",@"w.JPG",@"e.JPEG",@"r.JPEG",@"t.jpeg",@"d.jpg", nil];

MessageArr = [NSMutableArray arrayWithObjects:@"爱你不是应为你的美",@"最爱你的人是我",@"你怎么舍得我难过",@"在你需要我的时候",@"我来陪你度过",@"这一生没有你怎么过", nil];

scrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 300)];

scrView.backgroundColor = [UIColor orangeColor];

//添加图片

for (NSInteger i = 0, x=0; i<6; i++ ) {

//创建图片视图

UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, 300)];

//添加图片

image.image = [UIImage imageNamed:imgArr[i]];

//加入滚动视图

[scrView addSubview:image];

x +=self.view.frame.size.width;

}

//设置滚动视图的范围

scrView.contentSize =CGSizeMake(self.view.frame.size.width*4,300);

//是否按页滚动

scrView.pagingEnabled=YES;

//取消反弹

scrView.bounces=NO;

//设置隐藏滑动条

scrView.showsVerticalScrollIndicator=NO;

scrView.showsHorizontalScrollIndicator=NO;

scrView.delegate=self;

[self.view addSubview:scrView];

//初始化页码

thePage=[[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width-130, 315, 100, 30)];

//设置页码个数

thePage.numberOfPages=6;

//设置页码初始页

thePage.currentPage=0;

//设置页码颜色

thePage.pageIndicatorTintColor=[UIColor blueColor];

//设置当前页码颜色

thePage.currentPageIndicatorTintColor=[UIColor yellowColor];

[thePage addTarget:self action:@selector(shsh) forControlEvents:UIControlEventTouchUpInside];

//把分页控件添加到视图上

[self.view addSubview:thePage];

//添加计时器

timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerValueChange) userInfo:nil repeats:YES];

//触发

[timer fire];

number=0;

//计时器响应方法

-(void)timerValueChange

{

//设置偏移量

scrView.contentOffset=CGPointMake(self.view.frame.size.width*number, 0);

number++;

if (number == imgArr.count-1) {

number=0;

}

//    //    //点的大小                              为了把点 放到中间

//    [scrView setContentOffset:CGPointMake(number*scrView.frame.size.width, 0) animated:YES];

}

//实现页码和滚动视图的关联

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//当前页  把滚动的页数与滚动控件进行关联

thePage.currentPage=scrView.contentOffset.x/self.view.frame.size.width;

}

//遵守两个协议方法

//表示在滑动滚动视图的时候调用此方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGPoint point=scrView.contentOffset;//偏移量

//当前点的第几张图片

thePage.currentPage=point.x/scrView.frame.size.width;

if (table.contentOffset.y/self.view.frame.size.height>0) {

self.navigationController.navigationBarHidden=YES;

scrView.hidden=NO;

}

else{

self.navigationController.navigationBarHidden=NO;

scrView.hidden=NO;

}

}

上一篇 下一篇

猜你喜欢

热点阅读