TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
1、先上效果图:
2、使用说明:
该控件可以广泛应用于各种选择场景,这里以TextField为例,只需要一行代码即可在需要选择控件的TextField中应用- [SAPickView pickViewWithArray:array forTextField:textField];
复制代码 轻松搞定!
3、源代码
SAPickView.h- //
- // SAPickView.h
- //
- // Created by Sian on 14-9-29.
- // Copyright (c) 2014年 Sian. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface SAPickView : UIView
- @property (nonatomic, strong) UIPickerView *pickView;
- @property (nonatomic, strong) NSArray *dataArray;
- @property (nonatomic, strong) UITextField *textField;
- - (id)initWithArray:(NSArray *)array;
- + (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField;
- - (void)show;
- @end
复制代码
SAPickView.m- //
- // SAPickView.m
- //
- // Created by Sian on 14-9-29.
- // Copyright (c) 2014年 Sian. All rights reserved.
- //
- #import "SAPickView.h"
- @interface SAPickView () <UIPickerViewDataSource, UIPickerViewDelegate>
- {
- UIToolbar *_toolBar;
- CGSize _size;
- }
- @end
- @implementation SAPickView
- - (id)initWithArray:(NSArray *)array
- {
- if (self = [super init]) {
- self.alpha = 0;
- self.dataArray = array;
- _size = [[UIScreen mainScreen] bounds].size;
- self.backgroundColor = [UIColor clearColor];
- [self addsubViews];
- } return self;
- }
- - (void)addsubViews
- {
- self.frame = (CGRect){CGPointZero, _size};
- [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
-
- _toolBar = [[UIToolbar alloc] init];
- _toolBar.tintColor = kGrayColor(1.0);
- _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
- [self addSubview:_toolBar];
-
- // 按钮自定义
- UIBarButtonItem *item1 = [UIBarButtonItem barButtonItemWithTitle:@"取消" addTarget:self action:@selector(dismiss)];
- UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
- UIBarButtonItem *item3 = [UIBarButtonItem barButtonItemWithTitle:@"确定" addTarget:self action:@selector(done)];
- _toolBar.items = [NSArray arrayWithObjects:item1, item2, item3, nil];
-
- self.pickView = [[UIPickerView alloc] init];
- self.pickView.showsSelectionIndicator = YES;
- self.pickView.backgroundColor = kGrayColor(1.0);
- self.pickView.dataSource = self;
- self.pickView.delegate = self;
- self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
- [self addSubview:self.pickView];
- }
- + (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField
- {
- SAPickView *pickView = [[self alloc] initWithArray:array];
- pickView.textField = textField;
- [pickView show];
- }
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 1;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return self.dataArray.count;
- }
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- return self.dataArray[row];
- }
- - (void)show
- {
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 1;
- _toolBar.frame = (CGRect){0, _size.height - 206, _size.width, 44};
- self.pickView.frame = (CGRect){0, _size.height - 206 + 44, _size.width, 206 - 44};
- }];
- }
- - (void)dismiss
- {
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 0;
- _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
- self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- - (void)done
- {
- self.textField.text = [self.dataArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
- [self dismiss];
- }
- @end
复制代码
|
|