UIPageControl

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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值