UIScroll和UIPickView

Posted 010_purple

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIScroll和UIPickView相关的知识,希望对你有一定的参考价值。

  • .h

#import <UIKit/UIKit.h>

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

#define HEIGHT self.view.frame.size.height

 

@interface ViewController : UIViewController<UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

 

/**

 *  滚动视图

 */

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

}

 

以上是关于UIScroll和UIPickView的主要内容,如果未能解决你的问题,请参考以下文章

何时使用 UITouch 与 UIScroll

UIScroll 及其嵌套元素

UIScroll 视图 + UIView + UITableview 问题

UIScroll 视图未按预期工作

iOS - 当文本字段在子视图内时,键盘上的 UIScroll 弹出到文本字段

升级 Xcode,我有 2 个与 ScrollView 相同的视图。一个 View 将组件添加到 UIScroll 很好,另一个没有