UI總結(jié)-UIPageControl

? ? ? ? ? ? ? ? ? UI總結(jié)-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];

//總共有幾個(gè)page點(diǎn)

self.page.numberOfPages = 7;

//每個(gè)page點(diǎn)的顏色

self.page.pageIndicatorTintColor = [UIColor redColor];

//當(dāng)前page點(diǎn)的顏色

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 的點(diǎn)擊事件,它可以通過(guò)作用的點(diǎn)擊來(lái)實(shí)現(xiàn)currentPage值得變化(點(diǎn)擊左邊變小,點(diǎn)擊右邊變大)

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

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

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

}

運(yùn)行結(jié)果如下:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容