//
// ViewController.m
// UIScrollView-分页
//
// Created by 朱立志 on 14-5-1.
// Copyright (c) 2014年 朱立志. All rights reserved.
//
#define count 5
#import "ViewController.h"
@interface ViewController ()
{
UIPageControl *_control;
UIScrollView *scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *uiScrollview = [[UIScrollView alloc] init];
uiScrollview.frame = self.view.bounds;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
[self.view addSubview:uiScrollview];
for (int i = 0; i < count; i++) {
NSString *imageName = [NSString stringWithFormat:@"pages.bundle/%d.jpg" ,i+1];
UIImage *image = [UIImage imageNamed: imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(i*width, 0, width, height);
[uiScrollview addSubview:imageView];
}
scrollView = uiScrollview;
uiScrollview.showsHorizontalScrollIndicator = NO;
uiScrollview.contentSize = CGSizeMake(count*width, height);
uiScrollview.pagingEnabled = YES;
uiScrollview.backgroundColor = [UIColor grayColor];
uiScrollview.delegate = self;
UIPageControl *control = [[UIPageControl alloc] init];
control.numberOfPages = count;
control.bounds = CGRectMake(0, 0, 200, 50);
control.center = CGPointMake(width*0.5, height-50);
control.currentPage = 0;
_control = control;
[_control addTarget:self action:@selector(onPointClick) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:control];
}
- (void) onPointClick
{
NSLog(@"onPointClick");
CGFloat offsetX = _control.currentPage * scrollView.frame.size.width;
[UIView animateWithDuration:0.3 animations:^{
scrollView.contentOffset = CGPointMake(offsetX, 0);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int pageNum = scrollView.contentOffset.x / scrollView.frame.size.width;
_control.currentPage = pageNum;
}
@end