#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIScrollViewDelegate> @property (retain, nonatomic) UIPageControl *pageControl; @property (retain, nonatomic) UIScrollView *scrollView; @property (retain, nonatomic) NSArray *arrPhotos; @end
3、源文件(.m)里面是这样写滴:
#import "ViewController.h"
@interface ViewController () @end @implementation ViewController @synthesize pageControl; @synthesize scrollView; @synthesize arrPhotos; - (void)viewDidLoad { [super viewDidLoad]; arrPhotos = [[NSArray alloc]initWithObjects:@"001.jpg",@"002.jpg",@"003.jpg", nil]; [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timeTurnPage) userInfo:nil repeats:YES]; //启用定时器 [self paoMaDengDemo]; //调用跑马灯的方法 } //实现跑马灯效果的方法 - (void)paoMaDengDemo { scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 300)]; scrollView.delegate = self; scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.contentSize = CGSizeMake(arrPhotos.count*self.view.frame.size.width, 300); scrollView.pagingEnabled = YES; [self.view addSubview:scrollView]; for (int i=0; i<arrPhotos.count; i++ ){ UIView *view = [[UIView alloc]initWithFrame:CGRectMake((i+1)*scrollView.frame.size.width, 0,scrollView.frame.size.width, scrollView.frame.size.height)]; //创建序号从 1 到 数组+1 的view [scrollView addSubview:view]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 25)]; label.text = [NSString stringWithFormat:@"这是第%d张图片",i+1]; label.textAlignment = NSTextAlignmentCenter; [view addSubview:label]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 25, scrollView.frame.size.width, scrollView.frame.size.height)]; imageView.image = [UIImage imageNamed:[arrPhotos objectAtIndex:i]]; [view addSubview:imageView]; } //图片循环的原理:3-[1-2-3]-1 //创建序号为0的view 取数组第一张图片 UIView *viewFirst = [[UIView alloc]initWithFrame:CGRectMake(scrollView.frame.size.width*(arrPhotos.count+1), 0, scrollView.frame.size.width, scrollView.frame.size.width)]; [scrollView addSubview:viewFirst]; UILabel *labelFirst = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 25)]; labelFirst.text = [NSString stringWithFormat:@"这是第%ld张图片",arrPhotos.count]; labelFirst.textAlignment = NSTextAlignmentCenter; [viewFirst addSubview:labelFirst]; UIImageView *imageViewFirst = [[UIImageView alloc]initWithFrame:CGRectMake(0, 25, scrollView.frame.size.width, scrollView.frame.size.height)]; imageViewFirst.image = [UIImage imageNamed:[arrPhotos objectAtIndex:0]]; [viewFirst addSubview:imageViewFirst]; //创建序号为 数组+1 的view 取数组最后一张图片 UIView *viewEnd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.width)]; [scrollView addSubview:viewEnd]; UILabel *labelEnd = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 25)]; labelEnd.text = [NSString stringWithFormat:@"这是第1张图片"]; labelEnd.textAlignment = NSTextAlignmentCenter; [viewEnd addSubview:labelEnd]; UIImageView *imageViewEnd = [[UIImageView alloc]initWithFrame:CGRectMake(0, 25, scrollView.frame.size.width, scrollView.frame.size.height)]; imageViewEnd.image = [UIImage imageNamed:[arrPhotos objectAtIndex:arrPhotos.count-1]]; [scrollView addSubview:imageViewEnd]; scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*(arrPhotos.count+2), scrollView.frame.size.height); //设置scrollview可滑动的范围为 数组+2 个长度 [scrollView scrollRectToVisible:CGRectMake(scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES]; //将scrollview设置从第一页开始显示 pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, scrollView.frame.size.height-20, self.view.frame.size.width, 20)]; pageControl.backgroundColor = [UIColor clearColor]; pageControl.pageIndicatorTintColor = [UIColor grayColor]; pageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; pageControl.numberOfPages = [arrPhotos count]; pageControl.currentPage = 0; [self.view addSubview:pageControl]; } //定时器方法 - (void)timeTurnPage { int currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width; currentPage ++; if (currentPage > arrPhotos.count) { currentPage = 0; [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width*currentPage, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; }else { [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width*currentPage, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:YES]; } pageControl.currentPage = currentPage; } #pragma mark - UIScrollView Delegate - (void)scrollViewDidScroll:(UIScrollView *)sender { int page = scrollView.contentOffset.x / scrollView.frame.size.width; page -- ; pageControl.currentPage = page; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width; if (currentPage==0) { [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width * [arrPhotos count],0,self.scrollView.frame.size.width,self.scrollView.frame.size.height) animated:NO]; // 序号0显示最后一页 } else if (currentPage==([arrPhotos count]+1)) { [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; //序号 数组+1 显示第一页 } } @end</span><span style="font-size: 11px;"> </span>