滑動視圖的使用spa
//滑動視圖的建立,並設置frame值 UIScrollView *scroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 300)]; // 設置滑動視圖內容的大小 scroller.contentSize = CGSizeMake(800, 900); scroller.backgroundColor = [UIColor blackColor]; [self.view addSubview:scroller]; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 300)]; view.backgroundColor = [UIColor blackColor]; [scroller addSubview:view]; UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(375, 0, 375, 300)]; view.backgroundColor = [UIColor orangeColor]; [scroller addSubview:view2]; UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 375, 300)]; view3.backgroundColor = [UIColor redColor]; [scroller addSubview:view3]; //設置滾動條 scroller.showsHorizontalScrollIndicator = NO; scroller.showsVerticalScrollIndicator = NO; //設置樣式 scroller.indicatorStyle = UIScrollViewIndicatorStyleBlack; scroller.indicatorStyle = UIScrollViewIndicatorStyleWhite; scroller.indicatorStyle = UIScrollViewIndicatorStyleDefault; //反彈效果 //scroller.bounces = NO; //內容偏移量具體左邊和上邊的位置時300 200; //scroller.contentOffset = CGPointMake(300, 200); //分頁顯示 scroller.pagingEnabled = YES; //禁用滑動效果 // scroller.scrollEnabled = NO; }
滑動視圖代理方法,對滑動視圖進行縮放代理
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIScrollView *scorllView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:scorllView]; UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; imgView.image = [UIImage imageNamed:@"5.jpeg"]; imgView.tag = 100; [scorllView addSubview:imgView]; scorllView.delegate = self; //設置圖片的縮放比例 scorllView.minimumZoomScale = 0.5; scorllView.maximumZoomScale = 2.0; scorllView.bouncesZoom = NO; } //實現UIScrollerView的代理方法 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { UIImageView *imgView = (UIImageView *)[scrollView viewWithTag:100]; return imgView; }
滑動視圖代理方法,使用滑動視圖進行分頁,而且使其顯示的圖片能夠能夠循環滾動。code
使視圖實現循環滾動的總體思想:假如一共有6頁,6張圖片是按次序排好的,則在第一頁的前面放第六張圖片,在最後一頁的後面放第一頁的圖片,當圖片滑動到該循環的地方時,改變圖片的偏移量,這樣當用戶滑動的時候圖片出現的不是太突兀。只是經過改變偏移量就ok了!orm
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 建立滾動視圖 [self creatScrollView]; } - (void)creatScrollView { //建立滑動視圖 UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)]; scrollView.backgroundColor = [UIColor blueColor]; [self.view addSubview:scrollView]; //設置滑動視圖的分頁狀態 scrollView.pagingEnabled = YES; // 利用for循環,在滑動視圖上添加imageview 並添加圖片 for (int i = 0; i < 6; i ++) { NSString *imgStr = [NSString stringWithFormat:@"%d.jpeg",i + 1]; UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake((i + 1) * KWIDTH, 0 , KWIDTH, KHEIGHT)]; imgView.image = [UIImage imageNamed:imgStr]; [scrollView addSubview:imgView]; } //設置滑動視圖內容的大小 scrollView.contentSize = CGSizeMake(KWIDTH * 8, KHEIGHT); //設置代理 scrollView.delegate = self; UIImage *img1 = [UIImage imageNamed:@"1.jpeg"]; UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(7 * KWIDTH, 0, KWIDTH, KHEIGHT)]; imgView1.image = img1; [scrollView addSubview:imgView1]; UIImage *img2 = [UIImage imageNamed:@"6.jpeg"]; UIImageView *imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake( 0, 0, KWIDTH, KHEIGHT)]; imgView2.image = img2; [scrollView addSubview:imgView2]; //pageContr爲全局變量 pageContr = [[UIPageControl alloc]initWithFrame:CGRectMake(150, KHEIGHT - 50, 100, 50)]; 設置當前頁及頁數 pageContr.currentPage = 0; pageContr.numberOfPages = 6; [self.view addSubview:pageContr]; } //實現scrollViewDidEndDecelerating,滑動視圖沒有速度時調用的代理方法 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { pageContr.currentPage = (int)(scrollView.contentOffset.x + KWIDTH/2)/KWIDTH; if (scrollView.contentOffset.x == 7 * KWIDTH) { scrollView.contentOffset = CGPointMake(KWIDTH, 0); } if(scrollView.contentOffset.x == 0) { scrollView.contentOffset = CGPointMake(6 * KWIDTH, 0); } }