Aja*_*jay 8 iphone object uipickerview uipicker ios
在我的应用程序中,我正在尝试制作UIPickerView
包含三个组件(天,小时和分钟)的自定义.我已经使用三个组件制作了自定义选取器.现在我陷入了如何将标签添加到选择指示器的位置,该指示器显示了几天,几小时或几分钟的组件.
我已经浏览了本网站上发布的每一个链接或问题,但没有人帮助过我.
我正在尝试实现像这样的图像
任何人都可以建议我如何实现这一目标?
这就是我实现这一目标的方法......我在我发现的一些代码的帮助下制作了我的自定义 PickerView ......
\n\n在.h文件中:
\n\n// LabeledPickerView.h\n// LabeledPickerView\n\n#import <UIKit/UIKit.h>\n\n@interface LabeledPickerView : UIPickerView\n\n{\n NSMutableDictionary *labels;\n}\n\n/** Adds the label for the given component. */\n-(void)addLabel:(NSString *)labeltext forComponent:(NSUInteger)component forLongestString:(NSString *)longestString;\n@end\n
Run Code Online (Sandbox Code Playgroud)\n\n并在 .m 文件中...
\n\n// LabeledPickerView.m\n// LabeledPickerView\n\n#import "LabeledPickerView.h"\n\n@implementation LabeledPickerView\n\n/** loading programmatically */\n- (id)initWithFrame:(CGRect)aRect {\n if (self = [super initWithFrame:aRect]) {\n labels = [[NSMutableDictionary alloc] initWithCapacity:3];\n }\n return self;\n}\n\n/** loading from nib */\n- (id)initWithCoder:(NSCoder *)coder {\n if (self = [super initWithCoder:coder]) {\n labels = [[NSMutableDictionary alloc] initWithCapacity:3];\n }\n return self;\n}\n\n- (void) dealloc\n{\n [labels release];\n [super dealloc];\n}\n\n#pragma mark Labels\n\n// Add labelText to our array but also add what will be the longest label we will use in updateLabel\n// If you do not plan to update label then the longestString should be the same as the labelText\n// This way we can initially size our label to the longest width and we get the same effect Apple uses\n\n-(void)addLabel:(NSString *)labeltext forComponent:(NSUInteger)component forLongestString:(NSString *)longestString {\n [labels setObject:labeltext forKey:[NSNumber numberWithInt:component]];\n\n NSString *keyName = [NSString stringWithFormat:@"%@_%@", @"longestString", [NSNumber numberWithInt:component]];\n\n if(!longestString) {\n longestString = labeltext;\n }\n\n [labels setObject:longestString forKey:keyName];\n}\n\n//\n- (void) updateLabel:(NSString *)labeltext forComponent:(NSUInteger)component {\n\n UILabel *theLabel = (UILabel*)[self viewWithTag:component + 1];\n\n // Update label if it doesn\xc3\xa2\xe2\x82\xac\xe2\x84\xa2t match current label\n if (![theLabel.text isEqualToString:labeltext]) {\n\n NSString *keyName = [NSString stringWithFormat:@"%@_%@", @"longestString", [NSNumber numberWithInt:component]];\n NSString *longestString = [labels objectForKey:keyName];\n\n // Update label array with our new string value\n [self addLabel:labeltext forComponent:component forLongestString:longestString];\n\n // change label during fade out/in\n [UIView beginAnimations:nil context:NULL];\n [UIView setAnimationDuration:0.75];\n [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];\n theLabel.alpha = 0.00;\n theLabel.text = labeltext;\n theLabel.alpha = 1.00;\n [UIView commitAnimations];\n }\n\n}\n\n/**\n Adds the labels to the view, below the selection indicator glass-thingy.\n The labels are aligned to the right side of the wheel.\n The delegate is responsible for providing enough width for both the value and the label.\n */\n- (void)didMoveToWindow {\n // exit if view is removed from the window or there are no labels.\n if (!self.window || [labels count] == 0)\n return;\n\n UIFont *labelfont = [UIFont boldSystemFontOfSize:15];\n\n // find the width of all the wheels combined\n CGFloat widthofwheels = 0;\n for (int i=0; i<self.numberOfComponents; i++) {\n widthofwheels += [self rowSizeForComponent:i].width;\n }\n\n // find the left side of the first wheel.\n // seems like a misnomer, but that will soon be corrected.\n CGFloat rightsideofwheel = (self.frame.size.width - widthofwheels) / 2;\n\n // cycle through all wheels\n for (int component=0; component<self.numberOfComponents; component++) {\n // find the right side of the wheel\n rightsideofwheel += [self rowSizeForComponent:component].width;\n\n // get the text for the label.\n // move on to the next if there is no label for this wheel.\n NSString *text = [labels objectForKey:[NSNumber numberWithInt:component]];\n if (text) {\n\n // set up the frame for the label using our longestString length\n NSString *keyName = [NSString stringWithFormat:@"%@_%@", [NSString stringWithString:@"longestString"], [NSNumber numberWithInt:component]];\n NSString *longestString = [labels objectForKey:keyName];\n CGRect frame;\n frame.size = [longestString sizeWithFont:labelfont];\n\n // center it vertically\n frame.origin.y = (self.frame.size.height / 2) - (frame.size.height / 2) - 0.5;\n\n // align it to the right side of the wheel, with a margin.\n // use a smaller margin for the rightmost wheel.\n frame.origin.x = rightsideofwheel - frame.size.width -\n (component == self.numberOfComponents - 1 ? 5 : 7);\n\n // set up the label. If label already exists, just get a reference to it\n BOOL addlabelView = NO;\n UILabel *label = (UILabel*)[self viewWithTag:component + 1];\n if(!label) {\n label = [[[UILabel alloc] initWithFrame:frame] autorelease];\n addlabelView = YES;\n }\n\n label.text = text;\n label.font = labelfont;\n label.backgroundColor = [UIColor clearColor];\n label.shadowColor = [UIColor whiteColor];\n label.shadowOffset = CGSizeMake(0,1);\n\n // Tag cannot be 0 so just increment component number to esnure we get a positive\n // NB update/remove Label methods are aware of this incrementation!\n label.tag = component + 1;\n\n if(addlabelView) {\n /*\n and now for the tricky bit: adding the label to the view.\n kind of a hack to be honest, might stop working if Apple decides to\n change the inner workings of the UIPickerView.\n */\n if (self.showsSelectionIndicator) {\n // if this is the last wheel, add label as the third view from the top\n if (component==self.numberOfComponents-1)\n [self insertSubview:label atIndex:[self.subviews count]-3];\n // otherwise add label as the 5th, 10th, 15th etc view from the top\n else\n [self insertSubview:label aboveSubview:[self.subviews objectAtIndex:5*(component+1)]];\n } else\n // there is no selection indicator, so just add it to the top\n [self addSubview:label];\n\n }\n\n if ([self.delegate respondsToSelector:@selector(pickerView:didSelectRow:inComponent:)])\n [self.delegate pickerView:self didSelectRow:[self selectedRowInComponent:component] inComponent:component];\n }\n\n }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n并使用标签文本和组件标签调用此 addLabel: 方法,就是这样..!!
\n