iOS_9_scrollView分页
生活随笔
收集整理的這篇文章主要介紹了
iOS_9_scrollView分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最后效果圖:
BeyondViewController.h
// // BeyondViewController.h // 8_scrollVIew分頁瀏覽 // // Created by beyond on 14-7-25. // Copyright (c) 2014年 com.beyond. All rights reserved. //#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@endBeyondViewController.m
// // BeyondViewController.m // 8_scrollVIew分頁瀏覽 /*下面代碼存在性能問題,僅作為新特性介紹界面使用不可作為圖片瀏覽器~1,一次性生成8個ImageView會存在性能問題,解決方法:使用3個ImageView(或2個ImageView)2,另外,循環播放還沒實現*/ // Created by beyond on 14-7-25. // Copyright (c) 2014年 com.beyond. All rights reserved. //#import "BeyondViewController.h" // 圖片總張數 #define kImgCount 8 @interface BeyondViewController ()<UIScrollViewDelegate> {// 分頁條碼指示控制器UIPageControl *_pageControl; }@end@implementation BeyondViewController- (void)viewDidLoad {[super viewDidLoad];// 調用自己定義方法[self scrollViewWithPage]; }// 帶分頁功能的scrollView - (void)scrollViewWithPage {// 1,設置scrollView的可視大小,內容大小,等屬性_scrollView.frame = self.view.bounds;_scrollView.showsHorizontalScrollIndicator = NO;_scrollView.showsVerticalScrollIndicator = NO;_scrollView.bouncesZoom = NO;_scrollView.bounces = NO;// 設置代碼,監聽滾動完成的事件_scrollView.delegate = self;// 2,創建8個UIImageView,加入到scrollView// 每一個圖片寬,高CGFloat imgW = self.view.bounds.size.width;CGFloat imgH = self.view.bounds.size.height;for (int i=0; i<kImgCount; i++) {// UIImageView// 圖片名:01.jpg ~ 07.jpgNSString *imgName = [NSString stringWithFormat:@"0%d.png",i+1];UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];// 假設保持imageView里面的image不變形//設置UIImageView的對象的下面兩個屬性,能夠圖片不變形且充滿圖片框為前提進行填充。imgView.clipsToBounds = YES;imgView.contentMode = UIViewContentModeScaleAspectFill;// y是0,x是一張連著一張imgView.frame = CGRectMake(i*imgW, 0, imgW, imgH);// 將全部的圖片加入到scrollView[_scrollView addSubview:imgView];}// 3,這個最重要,是滾動區域// _scrollView.contentSize = CGSizeMake(kImgCount*imgW, imgH);// 0代表高度方向不滾動_scrollView.contentSize = CGSizeMake(kImgCount*imgW, 0);// 按scrollView的寬度分頁_scrollView.pagingEnabled = YES;// 4,pageControl分頁指示條_pageControl = [[UIPageControl alloc]init];// pageControl分頁指示條的中心點在底部中間_pageControl.numberOfPages = kImgCount; //這個最重要_pageControl.center = CGPointMake(imgW*0.5, imgH-20);_pageControl.bounds = CGRectMake(0, 0, 150, 15);_pageControl.pageIndicatorTintColor = [UIColor grayColor];_pageControl.currentPageIndicatorTintColor = [UIColor redColor];_pageControl.enabled = NO; //取消其默認的點擊行為[self.view addSubview:_pageControl];} /*在這種方法里面,能夠進行性能優化,由于時時在監聽滾動,從而隨時進行3個UIImageView的拼接,甚至可精簡到僅僅有2個UIImageView進行動態拼接*/ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {// scrollView的contentOffset是最重要的屬性,點,x,y記錄的是滾動的距離,相對的是scrollView的可視界面的左上角的距離CGPoint offset = scrollView.contentOffset;int curPageNo = offset.x / _scrollView.bounds.size.width;_pageControl.currentPage = curPageNo ;}@end版權聲明:本文博客原創文章,博客,未經同意,不得轉載。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的iOS_9_scrollView分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IP地址便捷修改器 V3.5 绿色版
- 下一篇: IAP