UIScrollView 照片滚动

#import"RootViewController.h"

#define VIEW_WIDTH self.view.frame.size.width

#define VIEW_HEIGHT self.view.frame.size.height

@interfaceRootViewController ()

@property(nonatomic,retain) UIScrollView *scrollView;

@property(nonatomic,retain) UIPageControl *page;

@property(nonatomic,retain) NSTimer *timer;

@end

// scrollView

self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

self.scrollView.backgroundColor = [UIColor yellowColor];

[self.view addSubview:self.scrollView];

[_scrollView release];

//代理人

self.scrollView.delegate =self;

//滚动范围

self.scrollView.contentSize = CGSizeMake(VIEW_WIDTH *7, VIEW_HEIGHT);

//整页

self.scrollView.pagingEnabled =YES;

//添加图片

for(NSInteger i =1; i <=6; i++) {

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*VIEW_WIDTH,0, VIEW_WIDTH, VIEW_HEIGHT)];

//图片名

NSString *name = [NSString stringWithFormat:@"S%ld.jpg", i];

imgView.image = [UIImage imageNamed:name];

[self.scrollView addSubview:imgView];

[imgView release];

//在最后添加最后一页

UIImageView *img= [[UIImageView  alloc] initWithFrame:CGRectMake(VIEW_WIDTH*6,0, VIEW_WIDTH, VIEW_HEIGHT)];

img.image = [UIImage imageNamed:@"S1.jpg"];

[self.scrollView addSubview:img];

[img release];

}

// UIPageControl

self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0,0,150,30)];

self.page.backgroundColor = [UIColor blackColor];

//添加父视图

//加载self.view保证视图滑动时依然存在

[self.view addSubview:self.page];

[_page release];

self.page.numberOfPages =6;

self.page.center = CGPointMake(self.view.center.x, VIEW_HEIGHT-50);

[self.page addTarget:selfaction:@selector(page:) forControlEvents:UIControlEventValueChanged];

self.page.tag =1000;

//定时器实现自动轮播

//多长时间翻页

self.timer = [NSTimer scheduledTimerWithTimeInterval:1target:selfselector:@selector(autoRoll) userInfo:nilrepeats:YES];

}

/*******************************************************************/

//当手指拖拽事停止定时器

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

[self.timer invalidate];

}

//定时器方法

-(void)autoRoll{

//翻页所用时间

[UIView animateWithDuration:0.5animations:^{

//当前x+图片宽度

self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + VIEW_WIDTH,0);

}completion:^(BOOLfinished) {

//完成动画之后要做的事

if(self.scrollView.contentOffset.x/VIEW_WIDTH ==6) {

self.scrollView.contentOffset = CGPointZero;

}

}];

}

//当使用定时器滚动时没有拖拽和减速阶段需要在scrollViewDidScroll方法中修改

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

self.page.currentPage = scrollView.contentOffset.x/VIEW_WIDTH;

if(scrollView.contentOffset.x/VIEW_WIDTH ==7) {

scrollView.contentOffset  = CGPointZero;

}

}

//当手指离开屏幕(结束减速时)重新创建定时器

//结束减速

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//刚滚动到最后一页跳回第一页

//修改小圆点

self.page.currentPage = scrollView.contentOffset.x/VIEW_WIDTH;

if(scrollView.contentOffset.x/VIEW_WIDTH ==6) {

scrollView.contentOffset  = CGPointZero;

}

//新建定时器

//[self creatTimer];

//1秒之后执行[self creatTimer]

[selfperformSelector:@selector(creatTimer) withObject:nilafterDelay:0.5];

}

-(void)creatTimer{

self.timer = [NSTimer scheduledTimerWithTimeInterval:1target:selfselector:@selector(autoRoll) userInfo:nilrepeats:YES];

}

#pragma mark - page方法

- (void)page:(UIPageControl *)page

{

//通过动画滚动

[UIView animateWithDuration:0.5animations:^{

self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH*page.currentPage,0);

}];

}

你可能感兴趣的:(UIScrollView 照片滚动)