UIScrollVIew 滚动视图内容总结:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 滚动视图
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];
scrollView.backgroundColor = [UIColor redColor];
// 设定滚动内容的范围(CGSize)
scrollView.contentSize = CGSizeMake(2800, 0); // 横纵 坐标
// 关掉边缘的弹动搞效果
// srollView.bounces = NO;
// 偏移量(CGPoint)
// srollView.contentOffset = CGPointMake(-100, -100);
// 内容距离上 左 下 右边缘的距离
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
// 滑动到顶部
scrollView.scrollsToTop = YES;
// 整屏翻动
scrollView.pagingEnabled = YES;
// srollView能不能滚动
scrollView .scrollEnabled = YES; // NO:不能滚动
// 是否显示横向的滚动条
scrollView.showsHorizontalScrollIndicator = YES;
// 2.将viewController设置为ScrollView的 代理人
scrollView.delegate = self;
// scrollView的缩放
// 缩放:1. 设置scrollView的缩放范围
scrollView.minimumZoomScale = 0.5; // 最小范围
scrollView.maximumZoomScale = 2; // 最大范围
// 设置缩放时是否弹动
scrollView.bouncesZoom = YES;
[self.view addSubview:scrollView];
[scrollView release];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 240, 400)];
imageView.image = [UIImage imageNamed:@"1.JPG"];
[scrollView addSubview:imageView];
[imageView release];
UIImageView *imageView0 = [[UIImageView alloc] initWithFrame:CGRectMake(300, 20, 240, 400)];
imageView0.image = [UIImage imageNamed:@"2.JPG"];
[scrollView addSubview:imageView0];
[imageView0 release];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)];
imageView1.image = [UIImage imageNamed:@"3.JPG"];
[scrollView addSubview:imageView1];
[imageView1 release];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)];
imageView2.image = [UIImage imageNamed:@"4.JPG"];
[scrollView addSubview:imageView2];
[imageView2 release];
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(860, 20, 240, 400)];
imageView3.image = [UIImage imageNamed:@"5.JPG"];
[scrollView addSubview:imageView3];
[imageView3 release];
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(1140, 20, 240, 400)];
imageView4.image = [UIImage imageNamed:@"6.JPG"];
[scrollView addSubview:imageView4];
[imageView4 release];
// UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(1420, 20, 240, 400)];
//
// [scrollView addSubview:imageView5];
// [imageView5 release];
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 420, 280, 62)];
// pageControl.backgroundColor =[UIColor clearColor];
// 显示多少个点点
pageControl.numberOfPages = 5;
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
// 给pageControl加一个响应方法
[pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
[pageControl release];
// 方便在其他方法中调用pageControl
self.page = pageControl;
}
- (void)pageControlAction:(UIPageControl *)pageControl
{
NSLog(@"%d", pageControl.currentPage);
}
// 缩放:2. 设置一个视图 随着scrollView放大缩小
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews firstObject]; // 把第一个子视图随着scrollView缩放
}
// 3. 实现相对应的代理方法(协议方法)
- (void)scrollViewDidSroll:(UIScrollView *)scrollView
{
// // 只要srollView滚动 就会一直触发这个方法
// NSLog(@"%s", __FUNCTION__);
// // srollView 的偏移量变化
// NSLog(@"偏移量变化: %@", NSStringFromCGPoint(scrollView.contentOffset));
//
// // 判断scrollView当前的页数
// // 偏移量.x / srollView宽度
// int pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width;
// NSLog(@"页数: %d", pageNumber);
// 当前scrollView滚动到当前页数
self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}
// 开始拖拽的时候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"开始拖拽:%s", __FUNCTION__);
}
// 结束拖拽的时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"结束拖拽:%s", __FUNCTION__);
}
// 开始减速的时候
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"开始减速:%s", __FUNCTION__);
}
// 结束减速的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"结束减速:%s", __FUNCTION__);
}