本文主要是介绍UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作者太好了 原文链接http://blog.csdn.net/zhuiyi316/article/details/7756472
研究了一整天的成果,也借鉴了网上的一些资料,参考了http://huluwa.me/blog/ios-uiscrollview-loop/.的思想.主要实现了以下功能:
1.自动滑动图片,UIPageControl会随着图片滑动而改变;
2.用户可以手动切换图片,左右切换均可,当用户手动滑动图片时,图片停止自动切换;
3.实现了图片的循环播放;
4.UIPageControl可点击从而实现向左或右滑动,当用户点击UIPageControl时,图片不再自动切换;
直接上代码吧,不懂的后面还有工程和说明文档也一并附上(原来这个不能直接上传附件,要的就去资源页下载吧:http://download.csdn.net/detail/zhuiyi316/4434616)。
首先是.h文件,注意自己将IBOutlet和IBAction与storyBoard中拖入的控件相关联,我用的XCode4.3.1.
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController<UIScrollViewDelegate>
- {
- NSMutableArray *imageArray;//存放图片
- NSTimer *myTimer;//定时器
- }
- @property(nonatomic,retain) IBOutlet UIScrollView *myScrollView;
- @property(nonatomic,retain) IBOutlet
- UIPageControl *pageControl;
- -(IBAction)pageTurn:(UIPageControl *)sender;
- @end
- #import "ViewController.h"
- #import <QuartzCore/QuartzCore.h>
- @interface ViewController ()
- @end
- @implementation ViewController
- @synthesize myScrollView,pageControl;
- -(void)initArray
- {
- imageArray=[NSArray arrayWithObjects: [UIImage imageNamed:@"share.png"],[UIImage imageNamed:@"home_baike.png"],[UIImage imageNamed:@"home_chat.png"],[UIImage imageNamed:@"Icon.png"],nil];
- //存放图片的数组
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self initArray];
- [self configScrollView];
- }
- -(void)configScrollView
- {
- /*
- @//初始化UIScrollView,设置相关属性,均可在storyBoard中设置
- CGRect frame=CGRectMake(0, 0, 320, 480);
- self.myScrollView = [[UIScrollView alloc]initWithFrame:frame]; //scrollView的大小
- self.myScrollView.backgroundColor=[UIColor blueColor];
- self.myScrollView.pagingEnabled=YES;//以页为单位滑动,即自动到下一页的开始边界
- self.myScrollView.showsVerticalScrollIndicator=NO;
- self.myScrollView.showsHorizontalScrollIndicator=NO;//隐藏垂直和水平显示条
- */
- self.myScrollView.delegate=self;
- UIImageView *firstView=[[UIImageView alloc] initWithImage:[imageArray lastObject]];
- CGFloat Width=self.myScrollView.frame.size.width;
- CGFloat Height=self.myScrollView.frame.size.height;
- firstView.frame=CGRectMake(0, 0, Width, Height);
- [self.myScrollView addSubview:firstView];
- //set the last as the first
- for (int i=0; i<[imageArray count]; i++) {
- UIImageView *subViews=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:i]];
- subViews.frame=CGRectMake(Width*(i+1), 0, Width, Height);
- [self.myScrollView addSubview: subViews];
- }
- UIImageView *lastView=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]];
- lastView.frame=CGRectMake(Width*(imageArray.count+1), 0, Width, Height);
- [self.myScrollView addSubview:lastView];
- //set the first as the last
- [self.myScrollView setContentSize:CGSizeMake(Width*(imageArray.count+2), Height)];
- [self.view addSubview:self.myScrollView];
- [self.myScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:NO];
- //show the real first image,not the first in the scrollView
- /*
- @//设置pageControl的位置,及相关属性,可选
- CGRect pageControlFrame=CGRectMake(100, 160, 78, 36);
- self.pageControl=[[UIPageControl alloc]initWithFrame:pageControlFrame];
- [self.pageControl setBounds:CGRectMake(0, 0, 16*(self.pageControl.numberOfPages-1), 16)];//设置pageControl中点的间距为16
- [self.pageControl.layer setCornerRadius:8];//设置圆角
- */
- self.pageControl.numberOfPages=imageArray.count;
- // self.pageControl.backgroundColor=[UIColor blueColor];//背景
- self.pageControl.currentPage=0;
- self.pageControl.enabled=YES;
- [self.view addSubview:self.pageControl];
- [self.pageControl addTarget:self action:@selector(pageTurn:)forControlEvents:UIControlEventValueChanged];
- myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
- }
- #pragma UIScrollView delegate
- -(void)scrollToNextPage:(id)sender
- {
- int pageNum=self.pageControl.currentPage;
- CGSize viewSize=self.myScrollView.frame.size;
- CGRect rect=CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height);
- [self.myScrollView scrollRectToVisible:rect animated:NO];
- pageNum++;
- if (pageNum==imageArray.count) {
- CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height);
- [self.myScrollView scrollRectToVisible:newRect animated:NO];
- }
- }
- -(void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- CGFloat pageWidth=self.myScrollView.frame.size.width;
- int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
- if (currentPage==0) {
- self.pageControl.currentPage=imageArray.count-1;
- }else if(currentPage==imageArray.count+1){
- self.pageControl.currentPage=0;
- }
- self.pageControl.currentPage=currentPage-1;
- }
- -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- [myTimer invalidate];
- }
- -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
- }
- -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- CGFloat pageWidth=self.myScrollView.frame.size.width;
- CGFloat pageHeigth=self.myScrollView.frame.size.height;
- int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
- NSLog(@"the current offset==%f",self.myScrollView.contentOffset.x);
- NSLog(@"the current page==%d",currentPage);
- if (currentPage==0) {
- [self.myScrollView scrollRectToVisible:CGRectMake(pageWidth*imageArray.count, 0, pageWidth, pageHeigth) animated:NO];
- self.pageControl.currentPage=imageArray.count-1;
- NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
- NSLog(@"the last image");
- return;
- }else if(currentPage==[imageArray count]+1){
- [self.myScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO];
- self.pageControl.currentPage=0;
- NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
- NSLog(@"the first image");
- return;
- }
- self.pageControl.currentPage=currentPage-1;
- NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
- }
- -(IBAction)pageTurn:(UIPageControl *)sender
- {
- int pageNum=pageControl.currentPage;
- CGSize viewSize=self.myScrollView.frame.size;
- [self.myScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)];
- NSLog(@"myscrollView.contentOffSet.x==%f",myScrollView.contentOffset.x);
- NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage); [myTimer invalidate];
- }
- @end
还存在的问题:当我点击UIPageControl中的白点时一次只能切换一张图片,即不能精确定位到某一个点所对应的图片,上网查了好多资料,都没有那个解决。貌似那样是不可以的, http://stackoverflow.com/questions/1103915/is-uipagecontrol-useless-by-itself和 http://stackoverflow.com/questions/4037416/scrollview-pagecontrol-dot-doesnt-change-with-the-page?rq=1中有相关的讨论。希望各位牛人能帮忙解决一下这个问题。ps:stackoverflow.com是一个很牛气的网站,我的很多问题都是在这里找到解决办法的,这是一个英文网站,有时看着会比较吃力。努力学习英语啊, ,与各位同仁共勉。
这篇关于UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!