123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- //
- // IQActionSheetPickerView.m
- // https://github.com/hackiftekhar/IQActionSheetPickerView
- // Copyright (c) 2013-14 Iftekhar Qurashi.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "IQActionSheetPickerView.h"
- #import <QuartzCore/QuartzCore.h>
- #import "IQActionSheetViewController.h"
- NSString * const kIQActionSheetAttributesForNormalStateKey = @"kIQActionSheetAttributesForNormalStateKey";
- /// Identifies an attributed string of the toolbar title for highlighted state.
- NSString * const kIQActionSheetAttributesForHighlightedStateKey = @"kIQActionSheetAttributesForHighlightedStateKey";
- @interface IQActionSheetPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
- {
- UIPickerView *_pickerView;
- UIDatePicker *_datePicker;
- }
- @property(nonatomic,strong) NSArray* selectArray;
- @property(nonatomic, strong) IQActionSheetViewController *actionSheetController;
- @end
- @implementation IQActionSheetPickerView
- @synthesize actionSheetPickerStyle = _actionSheetPickerStyle;
- @synthesize titlesForComponents = _titlesForComponents;
- @synthesize widthsForComponents = _widthsForComponents;
- @synthesize isRangePickerView = _isRangePickerView;
- @synthesize delegate = _delegate;
- @synthesize date = _date;
- -(void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (instancetype)initWithTitle:(NSString *)title delegate:(id<IQActionSheetPickerViewDelegate>)delegate
- {
- CGRect rect = [[UIScreen mainScreen] bounds];
- rect.size.height = 216+44;
-
- self = [super initWithFrame:rect];
- if (self)
- {
-
- self.clickBackHide = YES;
- self.pickBkgColor = HexColorFromRGBA(0x000000,0.5);
- //UIToolbar
- {
- self.actionToolbar = [[IQActionSheetToolbar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 44)];
- self.actionToolbar.barStyle = UIBarStyleDefault;
- self.actionToolbar.cancelButton.target = self;
- self.actionToolbar.cancelButton.action = @selector(pickeryoupaifcancelClicked:);
- self.actionToolbar.doneButton.target = self;
- self.actionToolbar.doneButton.action = @selector(pickerDoneClicked:);
- self.actionToolbar.titleButton.title = title;
- [self addSubview:self.actionToolbar];
- }
- //UIPickerView
- {
- _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_actionToolbar.frame) , CGRectGetWidth(_actionToolbar.frame), 216)];
- _pickerView.backgroundColor = [UIColor whiteColor];
- [_pickerView setShowsSelectionIndicator:NO];
- [_pickerView setDelegate:self];
- [_pickerView setDataSource:self];
- [self addSubview:_pickerView];
- }
-
- //UIDatePicker
- {
- _datePicker = [[UIDatePicker alloc] initWithFrame:_pickerView.frame];
- [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
- _datePicker.frame = _pickerView.frame;
- [_datePicker setDatePickerMode:UIDatePickerModeDate];
- [self addSubview:_datePicker];
- }
-
- //Initial settings
- {
- self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
- [self setFrame:CGRectMake(0, 0, CGRectGetWidth(_pickerView.frame), CGRectGetMaxY(_pickerView.frame))];
- [self setActionSheetPickerStyle:IQActionSheetPickerStyleTextPicker];
-
- self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
- _actionToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- _pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- _datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- }
- }
-
- _delegate = delegate;
-
- return self;
- }
- -(void)setActionSheetPickerStyle:(IQActionSheetPickerStyle)actionSheetPickerStyle
- {
- _actionSheetPickerStyle = actionSheetPickerStyle;
-
- switch (actionSheetPickerStyle) {
- case IQActionSheetPickerStyleTextPicker:
- [_pickerView setHidden:NO];
- [_datePicker setHidden:YES];
- break;
- case IQActionSheetPickerStyleDatePicker:
- [_pickerView setHidden:YES];
- [_datePicker setHidden:NO];
- [_datePicker setDatePickerMode:UIDatePickerModeDate];
- break;
- case IQActionSheetPickerStyleDateTimePicker:
- [_pickerView setHidden:YES];
- [_datePicker setHidden:NO];
- [_datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
- break;
- case IQActionSheetPickerStyleTimePicker:
- [_pickerView setHidden:YES];
- [_datePicker setHidden:NO];
- [_datePicker setDatePickerMode:UIDatePickerModeTime];
- break;
-
- default:
- break;
- }
- }
- /**
- * Set Picker View Background Color
- *
- * @param pickerViewBackgroundColor Picker view custom background color
- */
- -(void)setPickerViewBackgroundColor:(UIColor *)pickerViewBackgroundColor{
- _pickerView.backgroundColor = pickerViewBackgroundColor;
- }
- /**
- * Set Cancel Button Title Attributes
- *
- * @param cancelButtonAttributes Cancel Button Title Attributes
- */
- -(void)setCancelButtonAttributes:(NSDictionary *)cancelButtonAttributes{
- id attributesForCancelButtonNormalState = [cancelButtonAttributes objectForKey:kIQActionSheetAttributesForNormalStateKey];
- if (attributesForCancelButtonNormalState != nil && [attributesForCancelButtonNormalState isKindOfClass:[NSDictionary class]]) {
- [_actionToolbar.cancelButton setTitleTextAttributes:(NSDictionary *)attributesForCancelButtonNormalState forState:UIControlStateNormal];
- }
-
- id attributesForCancelButtonnHighlightedState = [cancelButtonAttributes objectForKey: kIQActionSheetAttributesForHighlightedStateKey];
- if (attributesForCancelButtonnHighlightedState != nil && [attributesForCancelButtonnHighlightedState isKindOfClass:[NSDictionary class]]) {
- [_actionToolbar.cancelButton setTitleTextAttributes:(NSDictionary *)attributesForCancelButtonnHighlightedState forState:UIControlStateHighlighted];
- }
- }
- /**
- * Set Done Button Title Attributes
- *
- * @param cancelButtonAttributes Done Button Title Attributes
- */
- -(void)setDoneButtonAttributes:(NSDictionary *)doneButtonAttributes{
- id attributesForDoneButtonNormalState = [doneButtonAttributes objectForKey:kIQActionSheetAttributesForNormalStateKey];
- if (attributesForDoneButtonNormalState != nil && [attributesForDoneButtonNormalState isKindOfClass:[NSDictionary class]]) {
- [_actionToolbar.doneButton setTitleTextAttributes:(NSDictionary *)attributesForDoneButtonNormalState forState:UIControlStateNormal];
- }
-
-
- id attributesForDoneButtonnHighlightedState = [doneButtonAttributes objectForKey: kIQActionSheetAttributesForHighlightedStateKey];
- if (attributesForDoneButtonnHighlightedState != nil && [attributesForDoneButtonnHighlightedState isKindOfClass:[NSDictionary class]]) {
- [_actionToolbar.doneButton setTitleTextAttributes:(NSDictionary *)attributesForDoneButtonnHighlightedState forState:UIControlStateHighlighted];
- }
- }
- /**
- * Set Action Bar Color
- *
- * @param barColor Custom color for toolBar
- */
- -(void)setToolbarTintColor:(UIColor *)toolbarTintColor{
- _toolbarTintColor = toolbarTintColor;
-
- [_actionToolbar setBarTintColor:toolbarTintColor];
- }
- /**
- * Set Action Tool Bar Button Color
- *
- * @param buttonColor Custom color for toolBar button
- */
- -(void)setToolbarButtonColor:(UIColor *)toolbarButtonColor{
- _toolbarButtonColor = toolbarButtonColor;
-
- [_actionToolbar setTintColor:toolbarButtonColor];
- }
- /*!
- Font for the UIPickerView
- */
- - (void)setTitleFont:(UIFont *)titleFont {
- _titleFont = titleFont;
-
- _actionToolbar.titleButton.font = titleFont;
- }
- /*!
- * Color for the UIPickerView
- */
- - (void)setTitleColor:(UIColor *)titleColor {
- _titleColor = titleColor;
-
- _actionToolbar.titleButton.titleColor = titleColor;
- }
- #pragma mark - Done/Cancel
- -(void)pickeryoupaifcancelClicked:(UIBarButtonItem*)barButton
- {
- if ([self.delegate respondsToSelector:@selector(actionSheetPickerViewWillCancel:)])
- {
- [self.delegate actionSheetPickerViewWillCancel:self];
- }
-
- [self dismissWithCompletion:^{
-
- if ([self.delegate respondsToSelector:@selector(actionSheetPickerViewDidCancel:)])
- {
- [self.delegate actionSheetPickerViewDidCancel:self];
- }
- }];
- }
- -(void)pickerDoneClicked:(UIBarButtonItem*)barButton
- {
- switch (_actionSheetPickerStyle)
- {
- case IQActionSheetPickerStyleTextPicker:
- {
- NSMutableArray *selectedTitles = [[NSMutableArray alloc] init];
- for (NSInteger component = 0; component<_pickerView.numberOfComponents; component++)
- {
- NSInteger row = [_pickerView selectedRowInComponent:component];
-
- if (row!= -1)
- {
- [selectedTitles addObject:_titlesForComponents[component][row]];
- }
- else
- {
- [selectedTitles addObject:[NSNull null]];
- }
- }
-
- [self setSelectedTitles:selectedTitles];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitles:)])
- {
- [self.delegate actionSheetPickerView:self didSelectTitles:selectedTitles];
- }
-
- }
- break;
- case IQActionSheetPickerStyleDatePicker:
- case IQActionSheetPickerStyleDateTimePicker:
- case IQActionSheetPickerStyleTimePicker:
- {
- [self setDate:_datePicker.date];
-
- [self setSelectedTitles:@[_datePicker.date]];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectDate:)])
- {
- [self.delegate actionSheetPickerView:self didSelectDate:_datePicker.date];
- }
- }
-
- default:
- break;
- }
-
- [self dismiss];
- }
- #pragma mark - IQActionSheetPickerStyleDatePicker / IQActionSheetPickerStyleDateTimePicker / IQActionSheetPickerStyleTimePicker
- -(void)dateChanged:(UIDatePicker*)datePicker
- {
- [self sendActionsForControlEvents:UIControlEventValueChanged];
- }
- -(void) setDate:(NSDate *)date
- {
- [self setDate:date animated:NO];
- }
- -(void)setDate:(NSDate *)date animated:(BOOL)animated
- {
- _date = date;
- if (_date != nil) [_datePicker setDate:_date animated:animated];
- }
- -(void)setMinimumDate:(NSDate *)minimumDate
- {
- _minimumDate = minimumDate;
-
- _datePicker.minimumDate = minimumDate;
- }
- -(void)setMaximumDate:(NSDate *)maximumDate
- {
- _maximumDate = maximumDate;
-
- _datePicker.maximumDate = maximumDate;
- }
- #pragma mark - IQActionSheetPickerStyleTextPicker
- -(void)reloadComponent:(NSInteger)component
- {
- [_pickerView reloadComponent:component];
- }
- -(void)reloadAllComponents
- {
- [_pickerView reloadAllComponents];
- }
- -(void)setSelectedTitles:(NSArray *)selectedTitles
- {
- [self setSelectedTitles:selectedTitles animated:NO];
- }
- -(NSArray *)selectedTitles
- {
- if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
- {
- NSMutableArray *selectedTitles = [[NSMutableArray alloc] init];
-
- NSUInteger totalComponent = _pickerView.numberOfComponents;
-
- for (NSInteger component = 0; component<totalComponent; component++)
- {
- NSInteger selectedRow = [_pickerView selectedRowInComponent:component];
-
- if (selectedRow == -1)
- {
- [selectedTitles addObject:[NSNull null]];
- }
- else
- {
- NSArray *items = _titlesForComponents[component];
-
- if ([items count] > selectedRow)
- {
- id selectTitle = items[selectedRow];
- [selectedTitles addObject:selectTitle];
- }
- else
- {
- [selectedTitles addObject:[NSNull null]];
- }
- }
- }
-
- return selectedTitles;
- }
- else
- {
- return nil;
- }
- }
- -(void)setSelectedTitles:(NSArray *)selectedTitles animated:(BOOL)animated
- {
- if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
- {
- NSUInteger totalComponent = MIN(selectedTitles.count, _pickerView.numberOfComponents);
-
- for (NSInteger component = 0; component<totalComponent; component++)
- {
- NSArray *items = _titlesForComponents[component];
- id selectTitle = selectedTitles[component];
-
- if ([items containsObject:selectTitle])
- {
- NSUInteger rowIndex = [items indexOfObject:selectTitle];
- [_pickerView selectRow:rowIndex inComponent:component animated:animated];
- }
- }
- }
- }
- -(void)selectIndexes:(NSArray *)indexes animated:(BOOL)animated
- {
- if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
- {
- NSUInteger totalComponent = MIN(indexes.count, _pickerView.numberOfComponents);
-
- for (NSInteger component = 0; component<totalComponent; component++)
- {
- NSArray *items = _titlesForComponents[component];
- NSUInteger selectIndex = [indexes[component] unsignedIntegerValue];
-
- if (selectIndex < items.count)
- {
- [_pickerView selectRow:selectIndex inComponent:component animated:animated];
- }
- }
- }
- }
- #pragma mark - UIPickerView delegate/dataSource
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
- return 50;
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
- {
- //If having widths
- if (_widthsForComponents)
- {
- CGFloat width = [_widthsForComponents[component] floatValue];
-
- //If width is 0, then calculating it's size.
- if (width <= 0)
- return ((pickerView.bounds.size.width-20)-2*(_titlesForComponents.count-1))/_titlesForComponents.count;
- //Else returning it's width.
- else
- return width;
- }
- //Else calculating it's size.
- else
- {
- return ((pickerView.bounds.size.width-20)-2*(_titlesForComponents.count-1))/_titlesForComponents.count;
- }
- }
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
-
- return [_titlesForComponents count];
-
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
-
- return _titlesForComponents[component].count;
-
- }
- -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- { //扩展修改线的颜色
- for(UIView *singleLine in pickerView.subviews)
- {
- if (singleLine.frame.size.height < 1)
- {
- singleLine.backgroundColor = self.lineColor;
- singleLine.frame = CGRectMake(KScreenWidth/2-self.lineSize.width/2,singleLine.frame.origin.y,self.lineSize.width, self.lineSize.height);
- }
- }
- UILabel *labelText = [[UILabel alloc] init];
- if(self.pickerComponentsColor != nil) {
- labelText.textColor = self.pickerComponentsColor;
- }
- if(self.pickerComponentsFont == nil){
- labelText.font = [UIFont boldSystemFontOfSize:20.0];
- }else{
- labelText.font = self.pickerComponentsFont;
- }
- labelText.backgroundColor = [UIColor clearColor];
- [labelText setTextAlignment:NSTextAlignmentCenter];
- [labelText setText:_titlesForComponents[component][row]];
- return labelText;
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- {
- UILabel* label = (UILabel*)[pickerView viewForRow:row forComponent:component];
- if(self.pickerComponentsSelectColor != nil) {
- label.textColor = self.pickerComponentsSelectColor;
- }
- if (_isRangePickerView && pickerView.numberOfComponents == 3)
- {
- if (component == 0)
- {
- [pickerView selectRow:MAX([pickerView selectedRowInComponent:2], row) inComponent:2 animated:YES];
- self.selectArray = _titlesForComponents[row+1];
- }
- else if (component == 2)
- {
- [pickerView selectRow:MIN([pickerView selectedRowInComponent:0], row) inComponent:0 animated:YES];
- }
- }
- [self sendActionsForControlEvents:UIControlEventValueChanged];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didChangeRow:inComponent:)]) {
- [self.delegate actionSheetPickerView:self didChangeRow:row inComponent:component];
- }
- }
- #pragma mark - show/Hide
- -(void)dismiss
- {
- [_actionSheetController dismissWithCompletion:nil];
- _actionSheetController = nil;
- }
- -(void)dismissWithCompletion:(void (^)(void))completion
- {
- [_actionSheetController dismissWithCompletion:completion];
- _actionSheetController = nil;
- }
- -(void)show
- {
- [self showWithCompletion:nil];
- }
- -(void)showWithCompletion:(void (^)(void))completion
- {
- [_pickerView reloadAllComponents];
-
- if (_actionSheetController == nil)
- {
- _actionSheetController = [[IQActionSheetViewController alloc] init];
- [_actionSheetController showPickerView:self completion:completion];
-
- }
- }
- @end
|