天天看点

UIPageControlimport “MainViewController.h”define WIDTH self.view.frame.size.widthdefine HEIGHT self.view.frame.size.heightpragma mark 专门用来缩放的协议方法

UIPageControl

import “MainViewController.h”

define WIDTH self.view.frame.size.width

define HEIGHT self.view.frame.size.height

@interface MainViewController ()

@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.

    self.scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,WIDTH ,HEIGHT )];

    self.scrollView.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:self.scrollView];

    [self.scrollView release];

    self.scrollView.contentSize=CGSizeMake(WIDTH*7, HEIGHT);

    self.scrollView.pagingEnabled=YES;

    for (NSInteger i=1; i<8; i++) {

    NSString *picName=[NSString stringWithFormat:@”h%ld.jpg”,i];

    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];

    imageView.frame=CGRectMake(WIDTH*(i-1), 0, WIDTH, HEIGHT-37);

    [self.scrollView addSubview:imageView];

    [imageView release];

    }

    self.scrollView.contentOffset=CGPointMake(WIDTH*0, 0);

    self.scrollView.bounces=NO;

    self.scrollView.showsHorizontalScrollIndicator=NO;

    self.scrollView.showsVerticalScrollIndicator=NO;

    UIPageControl *pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0,630,WIDTH,37)];

    pageControl.backgroundColor=[UIColor yellowColor];

    [self.view addSubview:pageControl];

    [pageControl release];

    // 图片的个数和点的个数相等

    pageControl.numberOfPages=7;

    // 点的背景颜色

    pageControl.pageIndicatorTintColor=[UIColor redColor];

    // 被选中的点的颜色

    pageControl.currentPageIndicatorTintColor=[UIColor blackColor];

    pageControl.currentPage=0;

    // 给它添加一个点击方法

    [pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];

    self.scrollView.delegate=self;

    pageControl.tag=1000;

    // scrollView的缩放

    // 缩放的比例

    // 最大的比例

    self.scrollView.maximumZoomScale=2;

    // 最小的比例

    self.scrollView.minimumZoomScale=0.5;

    // 原始的缩放比例

    self.scrollView.zoomScale=1;

}

pragma mark 专门用来缩放的协议方法

-(UIView )viewForZoomingInScrollView:(UIScrollView )scrollView{

return [scrollView.subviews firstObject];

}

-(void)pageControlAction:(UIPageControl *)pageControl{

// 点的个数从第0张开始

NSLog(@”%ld”,pageControl.currentPage);

// 触发事件,进行图片的切换

[self.scrollView setContentOffset:CGPointMake(pageControl.currentPage*WIDTH, 0) animated:YES];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

// 滑动图片,让圆点一起跟着动

UIPageControl pageControl=(UIPageControl )[self.view viewWithTag:1000];

pageControl.currentPage=self.scrollView.contentOffset.x/WIDTH;

}