//创建一个类,在类里实现循环滑动视图
//声明
#import <UIKit/UIKit.h>
@interface ScrollView : UIView<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scroll;
@end
//实现#import "ScrollView.h"
@implementation ScrollView
//定义三个全局变量
UIPageControl*pageControl;
//滑动试图的宽和高
CGFloat width;
CGFloat height;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
width=self.frame.size.width;
height=self.frame.size.height-40;
if (self) {
//设置滑动试图属性
_scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
_scroll.backgroundColor=[UIColor blueColor];
[self addSubview:_scroll];
_scroll.pagingEnabled=YES;
_scroll.scrollsToTop=YES;
_scroll.showsVerticalScrollIndicator=NO;
_scroll.showsHorizontalScrollIndicator=NO;
_scroll.delegate=self;
//容量为要显示的图片数11再加1等于12
_scroll.contentSize=CGSizeMake(width*12, height);
[_scroll release];
//循环加载图片
for (int i=0; i<11; i++) {
NSString*str=[NSString stringWithFormat:@"%d.jpg",i+101];
UIImageView *view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:str]];
view.frame=CGRectMake(i*width, 0, width, height);
[_scroll addSubview:view];
[view release];
}
//在要显示的11张图片后面加载跟第一张一样的图片
UIImageView *view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"101.jpg"]];
view.frame=CGRectMake(11*width, 0, width, height);
[_scroll addSubview:view];
[view release];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height,width, 20)];
//设置页面的数量
[pageControl setNumberOfPages:11];
//设置当前page和所有page的颜色
pageControl.currentPageIndicatorTintColor=[UIColor redColor];
pageControl.pageIndicatorTintColor=[UIColor grayColor];
//监听页面是否发生改变
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventAllEvents];
[self addSubview:pageControl];
[pageControl release];
//定时器循环
// [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(p:) userInfo:nil repeats:YES];
// Initialization code
}
return self;
}
-(void)changePage:(UIPageControl*)page//视图随currentPage值滑动
{
[_scroll scrollRectToVisible:CGRectMake(page.currentPage*width, 0, width, height) animated:YES];
}
-(void)p:(NSTimer*)time//nstimer自动循环的方法
{
int k=_scroll.contentOffset.x;
int currentPage = k/width+1;
if (currentPage>10||currentPage==0) {
currentPage = 0 ;
}
[pageControl setCurrentPage:currentPage];
if (_scroll.contentOffset.x >= width*11) {
_scroll.contentOffset = CGPointMake(0, 0);
}
k+=width;
_scroll.contentOffset=CGPointMake(k, 0);
}
//代理的两个方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x<0) {
scrollView.contentOffset=CGPointMake(width*11, 0);
}if (scrollView.contentOffset.x>width*11) {
scrollView.contentOffset=CGPointMake(0, 0);
}
}// any offset changes//已经结束滑动时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int currentPage = (int)(scrollView.contentOffset.x/width);
if (currentPage>10||currentPage==0) {
currentPage = 0 ;
}
[pageControl setCurrentPage:currentPage];
}// called when scroll view grinds to a halt//已经停止减速时调用
//在根视图控制器创建一个定义的这个类的一个对象
ScrollView *scroll=[[ScrollView alloc]initWithFrame:CGRectMake(20, 40, 280, 420)];
[self.view addSubview:scroll];
[scroll release];