您的位置:首页 > 产品设计 > UI/UE

UIPageControl

2015-08-16 09:50 369 查看

UIPageControl配合UIScrollView使用

self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
self.scrollView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.scrollView];
self.scrollView.delegate = self;
[_scrollView release];

self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;

self.scrollView.contentSize = CGSizeMake(WIDTH * 7, HEIGHT);
for (NSInteger i = 1; i < 8; i++) {
NSString *picName = [NSString stringWithFormat:@"h%ld.jpeg",i];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:picName]];
imageView.frame = CGRectMake(WIDTH * (i - 1), 0, WIDTH, HEIGHT);
[self.scrollView addSubview:imageView];
[imageView release];
}
self.scrollView.pagingEnabled = YES;
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 635, 200, 40)];
[self.view addSubview:page];

//scrollView的缩放
//缩放的比例
self.scrollView.maximumZoomScale = 2;
//最小的比例
self.scrollView.minimumZoomScale = 0.5;
//原始的缩放比例
self.scrollView.zoomScale = 1;

//page.backgroundColor = [UIColor blackColor];
page.tag = 1000;
[page release];

//图片个数和点的个数相同
page.numberOfPages = 7;
//点的背景颜色
page.pageIndicatorTintColor = [UIColor grayColor];
//被选中的圆点的背景颜色
page.currentPageIndicatorTintColor = [UIColor purpleColor];
//给它添加一个点击的方法
[page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

self.LTview = [[LTView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
self.LTview.lable.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.LTview];
[self.LTview release];

//当scrollView停止滑动时用偏移量的值除以宽度赋值给page,使page与滚动的页面相匹配
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger i = scrollView.contentOffset.x / WIDTH;
UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1000];
page.currentPage = i;
}

-(void)pageAction:(UIPageControl *)page{
//点的个数从第0张开始
NSLog(@"%ld",page.currentPage);
//触发事件进行图片的切换
//self.scrollView.contentOffset = CGPointMake(WIDTH * page.currentPage, 0);
[self.scrollView setContentOffset:CGPointMake(WIDTH * page.currentPage, 0) animated:YES];
}

#pragma mark 专门用来缩放的协议方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView.subviews firstObject];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: