UI总结-UIPageControl

2016-05-17  本文已影响906人  Dear丶Musk

                  UI总结-UIPageControl

UIPageControl是继承与UIControl的一种控件:

#import "ViewController.h"

#define WIDTH self.view.frame.size.width

@interface ViewController ()

@property(nonatomic, retain)UIScrollView *scr;

@property(nonatomic, retain)UIPageControl *page;

@property(nonatomic, retain)UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

self.scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];

self.scr.backgroundColor = [UIColor grayColor];

[self.view addSubview:self.scr];

[_scr release];

self.scr.pagingEnabled = YES;

self.scr.bounces = NO;

self.scr.showsHorizontalScrollIndicator = NO;

self.scr.contentSize = CGSizeMake(WIDTH * 9 ,400);

UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];

imagev.image = [UIImage imageNamed:@"h6.jpeg"];

[self.scr addSubview:imagev];

[imagev release];

for (NSInteger i = 0; i < 7; i++ ) {

UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, 400)];

imagev.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i ]];

[self.scr addSubview:imagev];

[imagev release];

}

UIImageView *imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * 8, 0, self.view.frame.size.width, 400)];

imagev1.image = [UIImage imageNamed:@"h0.jpeg"];

[self.scr addSubview:imagev1];

[imagev1 release];

self.scr.contentOffset = CGPointMake(WIDTH , 0);

self.scr.delegate = self;

//UIPageControl是继承与UIControl的一种控件

self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH - 200, 360, 200, 40)];

self.page.backgroundColor = [UIColor clearColor];

[self.view addSubview:self.page];

[_page release];

//总共有几个page点

self.page.numberOfPages = 7;

//每个page点的颜色

self.page.pageIndicatorTintColor = [UIColor redColor];

//当前page点的颜色

self.page.currentPageIndicatorTintColor = [UIColor blueColor];

[self.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];

self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 360, 100, 40)];

[self.view addSubview:self.label];

self.label.backgroundColor = [UIColor clearColor];

[_label release];

self.label.textAlignment = 1;

self.label.text = @"1/7";

self.label.font = [UIFont systemFontOfSize:20];

self.label.textColor = [UIColor redColor];

}

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

if (self.scr.contentOffset.x == 0) {

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

}else if (self.scr.contentOffset.x == WIDTH * 8){

self.scr.contentOffset = CGPointMake(WIDTH, 0);

}

self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;

self.label.text = [NSString stringWithFormat:@"%ld/7",self.page.currentPage + 1];

}

//pageControl 的点击事件,它可以通过作用的点击来实现currentPage值得变化(点击左边变小,点击右边变大)

-(void)pageChange:(UIPageControl *)page{

self.scr.contentOffset = CGPointMake(WIDTH * (page.currentPage + 1) , 0);

self.label.text = [NSString stringWithFormat:@"%ld/7",page.currentPage + 1];

}

运行结果如下:

上一篇下一篇

猜你喜欢

热点阅读