- .h
#import
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController : UIViewController
/**
* 滚动视图
*/
@property (nonatomic, strong)UIScrollView *scroll;
/**
* 分页控件
*/
@property (nonatomic, strong)UIPageControl *page;
/**
* 滚动条
*/
@property (nonatomic, strong)UIPickerView *pick;
// 数据源
@property (nonatomic, strong)NSArray *arr_data;
@end
- .m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.scroll.backgroundColor = [UIColor grayColor];
self.scroll.contentSize = CGSizeMake(WIDTH*3, HEIGHT);
// 分页
self.scroll.pagingEnabled = YES;
// 隐藏滚动条
self.scroll.showsHorizontalScrollIndicator = NO;
UIImageView *imv1 = [[UIImageView alloc] initWithFrame:self.view.frame];
imv1.backgroundColor = [UIColor purpleColor];
UIImageView *imv2 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
imv2.backgroundColor = [UIColor blueColor];
UIImageView *imv3 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT)];
imv3.backgroundColor = [UIColor redColor];
[self.scroll addSubview:imv1];
[self.scroll addSubview:imv2];
[self.scroll addSubview:imv3];
[self.view addSubview:self.scroll];
// 分页标识
self.page = [[UIPageControl alloc] initWithFrame:CGRectMake((WIDTH-120)/2, HEIGHT-100, 120, 30)];
self.page.numberOfPages = 3;
self.page.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.page];
// 代理
self.scroll.delegate = self;
// 滚动条
self.arr_data = @[@"年", @"月", @"日", @"时", @"分", @"秒"];
self.pick = [[UIPickerView alloc] initWithFrame:CGRectMake((WIDTH-200)/2, HEIGHT-300, 200, 100)];
// 两个代理(代理和数据源)
self.pick.delegate = self;
self.pick.dataSource = self;
[self.view addSubview:self.pick];
}
// 分页
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.page.currentPage = (int)scrollView.contentOffset.x/WIDTH;
}
// 代理
#pragma mark - delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
#pragma mark - sourcedata
// 数据源
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.arr_data.count;
}
#pragma mark - title
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.arr_data[row];
}
#pragma mark - selecter
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// row:下标
NSLog(@"%@", self.arr_data[row]);
}
#pragma mark - rowheight
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}