UIScrollView 照片滾動(dòng)

#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;

//滾動(dòng)范圍

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保證視圖滑動(dòng)時(shí)依然存在

[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;

//定時(shí)器實(shí)現(xiàn)自動(dòng)輪播

//多長時(shí)間翻頁

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

}

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

//當(dāng)手指拖拽事停止定時(shí)器

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

[self.timer invalidate];

}

//定時(shí)器方法

-(void)autoRoll{

//翻頁所用時(shí)間

[UIView animateWithDuration:0.5animations:^{

//當(dāng)前x+圖片寬度

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

}completion:^(BOOLfinished) {

//完成動(dòng)畫之后要做的事

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

self.scrollView.contentOffset = CGPointZero;

}

}];

}

//當(dāng)使用定時(shí)器滾動(dòng)時(shí)沒有拖拽和減速階段需要在scrollViewDidScroll方法中修改

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

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

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

scrollView.contentOffset ?= CGPointZero;

}

}

//當(dāng)手指離開屏幕(結(jié)束減速時(shí))重新創(chuàng)建定時(shí)器

//結(jié)束減速

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//剛滾動(dòng)到最后一頁跳回第一頁

//修改小圓點(diǎn)

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

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

scrollView.contentOffset ?= CGPointZero;

}

//新建定時(shí)器

//[self creatTimer];

//1秒之后執(zhí)行[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

{

//通過動(dòng)畫滾動(dòng)

[UIView animateWithDuration:0.5animations:^{

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

}];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容