码迷,mamicode.com
首页 > 其他好文 > 详细

UIPageControl 与 UIScrollView

时间:2015-08-13 23:46:47      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;

@end

@implementation MainViewController
- (void)dealloc{
    [_scrollView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 // 创建可以播放图片的ScrollView   
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 40)];
    self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"222.jpg"]];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 10, HEIGHT - 40);

    for (NSInteger i = 1; i < 11; i++) {
        NSString *picName = [NSString stringWithFormat:@"%ld.jpg",i];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
        imageView.frame = CGRectMake(WIDTH * (i - 1), 0, WIDTH, HEIGHT - 40);
        [self.scrollView addSubview:imageView];
        [imageView release];
    }
    self.scrollView.delegate = self;
    // scrollView 的缩放,坑爹的地方:缩放之后就不能再进行滚动了/
    // 设置缩放的比例
    //最大缩放比例
    self.scrollView.maximumZoomScale = 2;
    // 最小缩放比例
    self.scrollView.minimumZoomScale = 0.5;
    //对它缩放的原始比例
    self.scrollView.zoomScale = 1;

// 创建UIPageControl    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 40, WIDTH, 40)];
    [self.view addSubview:pageControl];
    pageControl.backgroundColor = [UIColor blackColor];
    [pageControl release];
    // 其他设置:
    // 图片个数和点的个数相同.
    pageControl.numberOfPages = 10;
    // 其余圆点的颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 被选中点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];

// 继承于UIControl,所以有点击方法.
    [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    pageControl.tag = 1000;
}

// UIPageControl 的点击方法实现
#pragma mark 让点击UIPageControl左右侧的时候,图片变换
- (void)pageAction:(UIPageControl *)page{
    // 点从第0个开始算.
    NSLog(@"%ld",page.currentPage);

   // 下面这个方式书写没有动画效果 
//self.scrollView.contentOffset = CGPointMake(page.currentPage * WIDTH, 0);
    // 有动画效果/
    [self.scrollView setContentOffset:CGPointMake(page.currentPage * WIDTH, 0) animated:YES];
}

#pragma mark 变换图片的时候,让点也跟着变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1000];
    page.currentPage = self.scrollView.contentOffset.x/WIDTH;
}

#pragma mark 专门用于缩放的协议方法的实现. 坏处是无法恢复到原来图片大小.不建议使用.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews firstObject];
}

效果如下图, 只能在重新运行模拟器….
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

UIPageControl 与 UIScrollView

标签:

原文地址:http://blog.csdn.net/gao_zi/article/details/47622845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!