#import "RootViewController.h" @interface RootViewController (){ UIPageControl * _pageControl; NSTimer * _timer; UIScrollView * scrV; UILabel * label; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self creatScrollView]; [self creatPageController]; [self addTimer]; // Do any additional setup after loading the view. }//加入定时器,在定时器方法中执行换页操作,用于实现自动轮播 -(void)addTimer{ _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(toNextPage) userInfo:nil repeats:YES]; } -(void)toNextPage{ NSInteger page= _pageControl.currentPage; //此处以下使用的判断方法,都是基于界面的转换,将最后面补充的第一张图片用第一张图片顶掉,造成视觉的误差,偷梁换柱。 if (page==0) { scrV.contentOffset=CGPointMake(0, 0); } page++; CGFloat X=page*self.view.frame.size.width; [UIView animateWithDuration:1 animations:^{ scrV.contentOffset=CGPointMake(X, 0); }]; if(page==8){ page=0; } _pageControl.currentPage=page; label.text=[NSString stringWithFormat:@"%d/8",page+1]; }//定义页面控件,使得页面控件跟随图片转换 -(void)creatPageController{ _pageControl =[[UIPageControl alloc]initWithFrame:CGRectMake(0, 400, 320, 40)]; _pageControl.backgroundColor=[UIColor blackColor]; //页面控件透明度 _pageControl.alpha=0.5; //定义页面控件显示白点的个数,实际图片为9张,最后一张与第一张一致,因为只实现从左往右,只需在最后添加一张。如需从右往左,即可在第一张前补充一张为最后一张即可,改变初始偏移量就可以实现多个方向转换 _pageControl.numberOfPages=8; //虽然实际图片为9张或者10张,但真正要展览的图片只有8张,所以page的页码仍为8,开始展览的第一张一定为0; _pageControl.currentPage=0; [self.view addSubview:_pageControl]; //定义页码,此处不写无关 label=[[UILabel alloc]initWithFrame:CGRectMake(270, 460, 50, 20)]; label.text=@"1/8"; label.backgroundColor=[UIColor cyanColor]; label.textAlignment=NSTextAlignmentCenter; [self.view addSubview:label]; }//1??初始化scrollView,加载图片,我使用的动漫00.jpg--动漫08.jpg,所以可以循环创建 -(void)creatScrollView{ //定义与屏幕大小等高等宽的scrollView scrV=[[UIScrollView alloc]initWithFrame:self.view.bounds]; //加载图片 for(int i = 0; i < 9; i++){ UIImageView * imageV=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i, 0, 320,self.view.frame.size.height)]; [imageV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"动漫%02d.jpg",i+1]]]; [scrV addSubview:imageV]; } //定义scrollView的属性 scrV.contentSize=CGSizeMake(self.view.frame.size.width*9, 0); //定义初始偏移量 如果第一张前面加了最后一张,那就把初始改为320,0; scrV.contentOffset=CGPointMake(0, 0); //按页面跳转 scrV.pagingEnabled=YES; scrV.showsVerticalScrollIndicator=NO; //设置代理 scrV.delegate=self; [self.view addSubview:scrV]; } #pragma mark **UIScrollViewDelegate** //在减速结束的方法里,判断页面位置,使页面控件跟随图片变换 此处用于手动轮换。 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"减速"); NSInteger page=scrV.contentOffset.x/320; if(page==8){ scrollView.contentOffset=CGPointMake(0, 0); page=0; } _pageControl.currentPage=page; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
UIScrollView滚动条控件 大多应用于手机软件中的图片展览
时间: 2024-11-07 19:50:47