| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // TimePickerButton.m
- // JasonDevelop
- //
- // Created by Jason Lee on 2014. 8. 27..
- // Copyright (c) jasondevelop. All rights reserved.
- //
- #import "CommonUtil.h"
- #import "TimePickerButton.h"
- #import "UIViewController-ActionSheetSimulation.h"
- #define kfToolBarHeight 44.0f
- #define kfRoundWidth 2.0f
- #define kUIBtnTintColor [UIColor whiteColor]
- #define ksEmptyString @""
- @interface TimePickerButtonDatePicker : UIDatePicker {
- BOOL changed;
- }
- @end
- @implementation TimePickerButtonDatePicker
- - (id)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- self.layer.backgroundColor = [UIColor blackColor].CGColor;
- }
- return self;
- }
- - (void)awakeFromNib {
- self.layer.backgroundColor = [UIColor clearColor].CGColor;
- }
- - (void)addSubview:(UIView *)view {
-
- if (!changed) {
- changed = YES;
- [self setValue:[UIColor whiteColor] forKey:@"textColor"];
- }
- [super addSubview:view];
- }
- @end
- @interface TimePickerButton () {
- UIActionSheet *_actionSheet;
- TimePickerButtonDatePicker *_datePicker;
- NSDateFormatter *_dateFormmater;
- UIToolbar *_actionToolbar;
- UIView *_simulatedActionSheetView;
- }
- @end
- @implementation TimePickerButton
- - (id)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- _datePicker = [[TimePickerButtonDatePicker alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 216)];
- [_datePicker setDatePickerMode:UIDatePickerModeTime];
-
- _dateFormmater = [[CommonUtil dateFormatter] copy];
- [_dateFormmater setTimeZone:[NSTimeZone systemTimeZone]];
- [_dateFormmater setDateFormat:@"HH시 mm분"];
-
- }
- return self;
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
-
- [self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
- }
- #pragma mark - UIActionSheet Delegate
- - (void)didTouchButton {
- [self showPicker];
- }
- - (void)generateActionSheet {
-
- //피커뷰를 만듬.
- UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"취소" style:UIBarButtonItemStylePlain target:self action:@selector(pickerCancelPressed:)];
-
- UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
- UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
- fixedSpace.width = 10;
- UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"선택" style:UIBarButtonItemStylePlain target:self action:@selector(pickerDonePressed:)];
-
- if (IOS_VERSION >= 7.0f) {
- cancelButton.tintColor = kUIBtnTintColor;
- doneBtn.tintColor = kUIBtnTintColor;
- }
-
- CGFloat width = [UIScreen mainScreen].bounds.size.width;
- _actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, kfToolBarHeight)];
- _actionToolbar.barStyle = UIBarStyleBlack;
- [_actionToolbar setItems:[NSArray arrayWithObjects:fixedSpace, cancelButton, flexSpace, doneBtn, fixedSpace, nil] animated:YES];
-
-
- _simulatedActionSheetView = [[CommonUtil currentViewController] actionSheetSimulationWithPickerView:_datePicker withToolbar:_actionToolbar];
- }
- - (void)showPicker {
- [[CommonUtil currentViewController].view endEditing:YES];
-
- if (!_simulatedActionSheetView) {
- [self generateActionSheet];
- }
- [[CommonUtil currentViewController] showActionSheetSimulation:_simulatedActionSheetView pickerView:_datePicker withToolbar:_actionToolbar];
- }
- - (void)pickerDonePressed:(id)sender {
- [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
- //request new category.
- NSDateComponents *pickerComps = [CommonUtil dateComponents:_datePicker.date];
- NSCalendar *cal = [NSCalendar currentCalendar];
- NSDate *pickerDate = [cal dateFromComponents:pickerComps];
-
-
- NSString *stringDate = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:pickerDate]];
- [self setTitle:stringDate forState:UIControlStateNormal];
- }
- - (void)pickerCancelPressed:(id)sender {
- [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
- }
- - (void)setHour:(NSInteger)hour minute:(NSInteger)minute {
-
- NSCalendar *cal = [NSCalendar currentCalendar];
-
- NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date]];
- [ds setHour:hour];
- [ds setMinute:minute];
- [ds setSecond:0];
-
- NSDate *date = [cal dateFromComponents:ds];
- [self setTitle:[CommonUtil stringFromTime:date] forState:UIControlStateNormal];
- }
- - (void)setTitle:(NSString *)title forState:(UIControlState)state {
- if ([title isEqualToString:ksEmptyString]) {
- [super setTitle:ksEmptyString forState:state];
- return;
- }
-
- [super setTitle:[CommonUtil formattedTime:title] forState:state];
-
- if (_datePicker) {
- NSDateComponents *ds = [CommonUtil timeComponentsFromString:title];
- [ds setTimeZone:[NSTimeZone systemTimeZone]];
-
- NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:ds];
- [_datePicker setDate:date animated:NO];
- }
- }
- //- (void)setTime:(NSDate *)time {
- // NSString *dateString = [CommonUtil stringFromTime:time];
- // [self setTitle:dateString forState:UIControlStateNormal];
- //}
- - (NSString *)timeString {
- if ([[self titleForState:UIControlStateNormal] isEqualToString:ksEmptyString])
- return ksEmptyString;
-
- // return _datePicker ? [CommonUtil stringFromTime:_datePicker.date] : ksEmptyString;
-
- NSString *timeString = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:_datePicker.date]];
- return _datePicker ? timeString : ksEmptyString;
- }
- - (NSString *)hourString {
- if (!self.dateComponents) {
- return nil;
- }
- NSString *hour = self.dateComponents.hour < 10 ? [NSString stringWithFormat:@"0%zd", self.dateComponents.hour] : [NSString stringWithFormat:@"%zd", self.dateComponents.hour];
- return hour;
- }
- - (NSString *)minuteString {
- if (!self.dateComponents) {
- return nil;
- }
-
- NSString *minute = self.dateComponents.minute < 10 ? [NSString stringWithFormat:@"0%zd", self.dateComponents.minute] : [NSString stringWithFormat:@"%zd", self.dateComponents.minute];
- return minute;
- }
- - (NSDate *)date {
- return _datePicker.date;
- }
- - (NSDateComponents *)dateComponents {
- return _datePicker ? [CommonUtil dateComponents:_datePicker.date] : nil;
- }
- @end
|