iOS开发自定义时间选取器

jopen 9年前

自定义时间选取器

今天我们做一个时间选取器,很简单,效果如下:

iOS开发自定义时间选取器

我们自定义一个LGDatePickerView,在LGDatePickerView里面实现。

背景半透明

背景是半透明的,点击的灰色背景的时候,时间选取器消失。在LGDatePickerView初始化方法里,代码如下:

- (id)init  {      self = [super init];      if (self) {    //背景半透明,绑定取消方法      UIControl *control = [[UIControl alloc] initWithFrame:SCREEN_BOUNDS];      control.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5f];      [self addSubview:control];      [control addTarget:self action:@selector(actionCancel:) forControlEvents:UIControlEventTouchUpInside];            }      return self;  }

绑定的actionCancel方法:

- (void)actionCancel:(id)sender  {      [self removeFromSuperview];  }

确定取消按钮

看到上面的确定取消按钮,你会怎么做,写一个UIView上面放两个UIButton。这样做也是可以实现的。我们还可以用UIToolbar。在LGDatePickerView初始化方法里加上下面这段代码:

 // Toolbar  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, SCREEN.height - 250, SCREEN.width, 50)];  toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;  UIBarButtonItem *itemCancelDone = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(actionConfirm:)];  UIBarButtonItem *itemCancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(actionCancel:)];  UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  [toolbar setItems:[NSArray arrayWithObjects:itemCancel,space,itemCancelDone, nil]];  [control addSubview:toolbar];

actionCancel上面已经实现了。下面实现actionConfirm方法。它有什么作用呢?

  • 点击的时候获取到时间,然后通过代理代理出去
  • 时间选取器消失

      - (void)actionConfirm:(id)sender    {        if ([self.delegate respondsToSelector:@selector(datePickerView:didSelectTime:)]) {            [self.delegate datePickerView:self didSelectTime:self.datePicker.date];        }        [self removeFromSuperview];    }

代理方法

在LGDatePickerView.h

@protocol LGDatePickerViewDelegate <NSObject>    - (void)datePickerView:(LGDatePickerView *)datepicker didSelectTime:(NSDate *)time;    @end

创建UIDatePicker

在LGDatePickerView.h定义一个全局变量:

@property (nonatomic, strong) UIDatePicker *datePicker;

在LGDatePickerView初始化方法里加上下面这段代码:

UIDatePicker *datePicker = [[UIDatePicker alloc] init];  datePicker.backgroundColor = [UIColor whiteColor];  datePicker.datePickerMode = UIDatePickerModeCountDownTimer;  datePicker.date = [NSDate date];  datePicker.frame = CGRectMake(0, SCREEN.height - 200, SCREEN.width, 220);  [control addSubview:datePicker];  self.datePicker = datePicker;

使用LGDatePickerView

使用起来很简单,创建一下,然后加载self.view上面即可:

    LGDatePickerView *datePicker = [[LGDatePickerView alloc] init];      datePicker.delegate = self;      datePicker.datePicker.date = [NSDate date];      datePicker.frame = self.view.bounds;      [self.view addSubview:datePicker];

以上就实现了iOS开发自定义时间选取器,代码不难,有什么问题,欢迎提问哈。

Posted by 李刚
来自:http://www.superqq.com/blog/2015/07/06/ioskai-fa-zi-ding-yi-shi-jian-xuan-qu-qi/