// Created By 郭仔
//==================================================
师傅领进门,修行在个人!自学才是王道!
//==================================================
UIScrollView:
UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)]; scrollView.backgroundColor = [UIColor greenColor]; [self.view addSubview:scrollView]; // 设置内容大小,当contentSize大于scrollView.frame.size才能滑动 scrollView.contentSize = CGSizeMake(1000, 1000); UIView * view = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)]; view.backgroundColor = [UIColor redColor]; [scrollView addSubview:view]; scrollView.showsHorizontalScrollIndicator = YES; scrollView.showsVerticalScrollIndicator = YES; // scrollView.contentOffset = CGPointMake(50, 50); // scrollView.pagingEnabled = YES; UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(200, 50, 50, 50)]; view2.backgroundColor = [UIColor blackColor]; [scrollView addSubview:view2]; [view2 release]; scrollView.bounces = YES;) scrollView.scrollsToTop = YES; scrollView.scrollEnabled = YES; scrollView.alwaysBounceHorizontal = NO; scrollView.alwaysBounceVertical = YES; scrollView.minimumZoomScale = 0.5; scrollView.maximumZoomScale = 2; scrollView.zoomScale = 1.5; // 设置代理 scrollView.delegate = self;如果要实现缩放功能,则需要遵循UIScrollViewDelegate,设置自身为代理。
例如:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView { // NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize)); return [scrollView.subviews objectAtIndex:0]; }
// ==================================================================
UIPageView:
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 400, 200, 50)]; pageControl.backgroundColor = [UIColor orangeColor]; [self.view addSubview:pageControl]; pageControl.numberOfPages = 5; pageControl.currentPage = 2; pageControl.pageIndicatorTintColor = [UIColor blueColor]; pageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];// ==================================================================
滚动视图的模拟:
UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; // _scrollView = scrollView; [self.view addSubview:scrollView]; scrollView.pagingEnabled = YES; // scrollView.delegate = self; scrollView.contentSize = CGSizeMake(320*5, 568); // 把图片名放到数组里 NSArray * imagesName = [NSArray arrayWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png", nil]; for (int i = 0; i< imagesName.count; i ++) { UIImage * img = [UIImage imageNamed:[imagesName objectAtIndex:i]]; UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 568)]; imageView.image = img; [scrollView addSubview:imageView]; [imageView release]; } UIPageControl * pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 400, 200, 50)]; pageControl.numberOfPages = 5; [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; pageControl.pageIndicatorTintColor = [UIColor redColor]; pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; // self.pageControl = pageControl; [self.view addSubview:pageControl]; [pageControl release]; [scrollView release];方法的实现:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGPoint offSet = scrollView.contentOffset; int pageCount = offSet.x/320; _pageControl.currentPage = pageCount; } - (void)pageChanged:(UIPageControl *)control { long currentPage = control.currentPage; CGPoint offSet = CGPointMake(currentPage * 320, 0); // 设置scrollView的偏移量 [self.scrollView setContentOffset:offSet animated:YES]; }//================================================================
UI开发----UIScrollView和UIPageControl简单模拟滚动视图
原文地址:http://blog.csdn.net/guoxianzhuang/article/details/45156681