//創建一個類,在類裏實現循環滑動視圖
//聲明
#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];