您好,登錄后才能下訂單哦!
這篇文章主要介紹iOS如何實(shí)現(xiàn)UIScrollView滾動視圖/無限循環(huán)滾動/自動滾動功能,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體代碼如下所示;
<UIScrollViewDelegate> #define WIDTH [[UIScreen mainScreen] bounds].size.width #define HEIGHT [[UIScreen mainScreen] bounds].size.height @property (nonatomic, strong)NSTimer *timer; //定時器 @property (nonatomic, retain)NSMutableArray *arr; //放圖片的數(shù)組 @property (nonatomic, retain)UIView *headerView; //tableView的表頭 @property (nonatomic, retain)UIImageView *image; //圖片 @property (nonatomic, retain)UIScrollView *scrollView; @property (nonatomic, retain)UIPageControl *pageC; //頁碼 //設(shè)置頭視圖 - (void)headImage{ //圖片數(shù)組 self.arr = [NSMutableArray arrayWithObjects:@"8.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"1.jpg", nil]; self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200 * HEIGHT/667)]; self.scrollView.backgroundColor = [UIColor clearColor]; //設(shè)置滾動量 self.scrollView.contentSize = CGSizeMake(WIDTH * self.arr.count, 0); //設(shè)置偏移量 self.scrollView.contentOffset = CGPointMake(WIDTH, 0); //設(shè)置按頁滾動 self.scrollView.pagingEnabled = YES; //設(shè)置是否顯示水平滑動條 self.scrollView.showsHorizontalScrollIndicator = NO; //設(shè)置是否邊界反彈 self.scrollView.bounces = NO; //把scrollView添加到tableView的表頭的視圖上 [self.headerView addSubview:self.scrollView]; [_scrollView release]; //循環(huán)圖片添加到UIImageView上 for (int i = 0 ; i < self.arr.count; i++) { NSString *name = [self.arr objectAtIndex:i]; UIImage *img = [UIImage imageNamed:name]; self.image = [[UIImageView alloc]init]; self.image.frame = CGRectMake(i * WIDTH, 0, WIDTH, 200 * HEIGHT/667); self.image.image = img; [self.scrollView addSubview:self.image]; [_image release]; } self.scrollView.delegate = self; //設(shè)置頁面 self.pageC = [[UIPageControl alloc]initWithFrame:CGRectMake(100 * WIDTH/375, 120 * HEIGHT/667, 200* WIDTH/375, 60*HEIGHT/667)]; self.pageC.backgroundColor = [UIColor clearColor]; //把頁碼添加到頭視圖上 [self.headerView addSubview:self.pageC]; //設(shè)置頁碼數(shù) self.pageC.numberOfPages = self.arr.count; //設(shè)置選中頁碼的顏色 self.pageC.currentPageIndicatorTintColor = [UIColor brownColor]; //設(shè)置未選中的頁碼顏色 self.pageC.pageIndicatorTintColor = [UIColor grayColor]; //設(shè)置當(dāng)前選中頁 self.pageC.currentPage = 0; //核心方法 [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; [_pageC release]; //自定義一個定時器方法 [self addTimer]; } //定時器執(zhí)行方法 - (void)change:(NSTimer *)time{ if (self.pageC.currentPage == self.pageC.numberOfPages - 1) { self.pageC.currentPage = 0; } else if (self.pageC.currentPage < self.pageC.numberOfPages - 1) { self.pageC.currentPage++; } [self.scrollView setContentOffset:CGPointMake((self.pageC.currentPage + 1) * WIDTH, 0) animated:NO]; }
以上是“iOS如何實(shí)現(xiàn)UIScrollView滾動視圖/無限循環(huán)滾動/自動滾動功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。