自動(dòng)小輪播圖



//封裝UIScrollView 方法

- (void)initscroll{

//初始化

self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), 220)];

//self.scrollview.backgroundColor = [UIColor redColor];

//添加到視圖

[self.view addSubview:self.scrollview];

//設(shè)置滾動(dòng)視圖的長度

[self.scrollview setContentSize:CGSizeMake(CGRectGetWidth(self.view.bounds)*4, 0)];

for (NSInteger i = 0; i < 4; i++) {

UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)*i, 0, CGRectGetWidth(self.view.bounds), 220)];

// 設(shè)置圖片

[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",(long)i]]];

[self.scrollview addSubview:imageview];

}

//設(shè)置圖片的分頁

[self.scrollview setPagingEnabled:YES];

//將圖片水平面的橫線隱藏

[self.scrollview setShowsHorizontalScrollIndicator:NO];

self.scrollview.delegate = self;

//設(shè)置滾動(dòng)視圖下面的點(diǎn)

self.pagecon = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollview.frame)-20, self.view.frame.size.width, 20)];

//設(shè)置的點(diǎn)數(shù)

[self.pagecon setNumberOfPages:4];

[self.pagecon setPageIndicatorTintColor:[UIColor blackColor]];

[self.view addSubview:self.pagecon];

self.edgesForExtendedLayout = UIRectEdgeNone;

self.automaticallyAdjustsScrollViewInsets = YES;

}

//調(diào)用 UIscrollview 的協(xié)議方法

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

[self.pagecon setCurrentPage:(NSInteger)(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame))];

}

//封裝的定時(shí)器

- (void)initTimer{

if (!self.timer) {

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];

//定時(shí)器會跑在標(biāo)記為common modes的模式下,以上兩種通用,像廣告輪播等就會用到該模式。

[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

/*self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];

*/

}

}

-(void)click{

NSInteger pagecount = self.pagecon.currentPage + 1;

if (pagecount >= 4) {

pagecount = 0;

}

[self.scrollview setContentOffset:CGPointMake(pagecount * CGRectGetWidth(self.scrollview.frame), 0) animated:YES];

}

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

if (self.timer) {

//invalidate 廢棄

[self.timer invalidate];

[self setTimer:nil];

}

}

//結(jié)束后返回第一張圖片

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

[self initTimer];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容