最近项目里用到了UIPickerView,要求无限轮播,简单看了看UIPickView的所有属性和所有代理方法,没有无限轮播的设置,遂去百度了一把,发现网上流传着一个帖子,大家各种转载,看了看后,明白其中道理,原来是这样!
UIPickerView,使用规则与UITableView很像,下边是UIPickerView的.h文件,包涵两个代理的描述:
//
// UIPickerView.h
// UIKit
//
// Copyright (c) 2006-2015 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIView.h>
#import <UIKit/UIKitDefines.h>
NS_ASSUME_NONNULL_BEGIN
@protocol UIPickerViewDataSource, UIPickerViewDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding>
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; // default is nil. weak reference
@property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate; // default is nil. weak reference
@property(nonatomic) BOOL showsSelectionIndicator; // default is NO
// info that was fetched and cached from the data source and delegate
@property(nonatomic,readonly) NSInteger numberOfComponents;
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
- (CGSize)rowSizeForComponent:(NSInteger)component;
// returns the view provided by the delegate via pickerView:viewForRow:forComponent:reusingView:
// or nil if the row/component is not visible or the delegate does not implement
// pickerView:viewForRow:forComponent:reusingView:
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
// Reloading whole view or single component
- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;
// selection. in this case, it means showing the appropriate row in the middle
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // scrolls the specified row to center.
- (NSInteger)selectedRowInComponent:(NSInteger)component; // returns selected row. -1 if nothing selected
@end
__TVOS_PROHIBITED
@protocol UIPickerViewDataSource<NSObject>
@required
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end
__TVOS_PROHIBITED
@protocol UIPickerViewDelegate<NSObject>
@optional
// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED;
// these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; // attributed title is favored if both methods are implemented
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;
@end
NS_ASSUME_NONNULL_END
你会发现,其中方法很少,也很容易理解,我不做过多解释。我们接着说文字大小和颜色的修改~
注意代理又一个这样的方法:
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; // attributed title is favored if both methods are implemented
OK,就是这个,这个方法返回一个NSAttributedString类型数据,这个数据在我的微博里已经转载了一篇详细文章:http://blog.csdn.net/icefishlily/article/details/52692141
上代码:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSDictionary* titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
kCL6, NSForegroundColorAttributeName,
[UIFont systemFontOfSize:30.0f], NSFontAttributeName,
nil];
NSInteger count = self.dataList.count;
NSString *str = [NSString stringWithFormat:@"%.1f%%", [self.dataList[row%count] doubleValue]];
NSAttributedString *restr = [[NSAttributedString alloc] initWithString:str attributes:titleTextAttributes];
return restr;
}
用了上边这个方法,就不要用下边的方法了。
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;
注意上边代码中字典的定义,用到了两个东西:
NSForegroundColorAttributeName 文字颜色值
NSFontAttributeName 文字font
好了,不用多说了,颜色和大小搞定了。
接着说无限轮播,其实UIPickerView是个假的无限轮播,其原理是将你需要展示的几个cell(这里暂且叫cell,好理解)复制很多份(例如200份),然后都放到UIPickerView里边展示,并且在pickerView最开始出现的时候,就让它默认拨到中间位置,这样上下都有100组同样的数据,在不滑倒边界时,就像是无限轮播了。
代码如下:
//下边代码放在初始化的位置即可
self.dataList = [[NSMutableArray alloc] init];
for (int i=0; i<=20; i++) {
[self.dataList addObject:[NSNumber numberWithDouble:i*0.1]];
}
//拨到中间位置
[self.pickView selectRow:self.dataList.count*100 inComponent:0 animated:YES];
//下边代码是UIPickerView的部分代理
#pragma mark UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.dataList.count * 200;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSDictionary* titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
kCL6, NSForegroundColorAttributeName,
[UIFont systemFontOfSize:30.0f], NSFontAttributeName,
nil];
NSInteger count = self.dataList.count;
NSString *str = [NSString stringWithFormat:@"%.1f%%", [self.dataList[row%count] doubleValue]];
NSAttributedString *restr = [[NSAttributedString alloc] initWithString:str attributes:titleTextAttributes];
return restr;
}