iOS開發自定義時間選取器

jopen 9年前發布 | 12K 次閱讀 IOS iOS開發 移動開發

自定義時間選取器

今天我們做一個時間選取器,很簡單,效果如下:

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/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!