iOS开发-UIPickerView的使用&点菜项目展示

今天呢开始给同学们讲解UIPickerView的使用,首先通过一个app中广泛存在的选菜的功能来体现UIPickerView的用法!废话不多说直接上代码~首先看效果图!

 

 

//

//  ZZPickerView.h

//  15-点菜系统

//

//  Created by 周昭 on 16/12/19.

//  Copyright © 2016年 HT_Technology. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ZZPickerView : UIView

 

- (void)todayMainPush;

@end

 

//

//  ZZPickerView.m

//  15-点菜系统

//

//  Created by 周昭 on 16/12/19.

//  Copyright © 2016年 HT_Technology. All rights reserved.

//

 

#import "ZZPickerView.h"

 

@interface ZZPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong) NSArray *foods;

@property (nonatomic,weak) UIImageView *bgImgView;

@property (nonatomic,weak) UILabel *titleText;

@property (nonatomic,weak) UIPickerView *foodPicker;

@property (nonatomic,weak) UILabel *fruitLabel;

@property (nonatomic,weak) UILabel *mainLabel;

@property (nonatomic,weak) UILabel *drinkLabel;

@property (nonatomic,weak) UILabel *text1;

@property (nonatomic,weak) UILabel *text2;

@property (nonatomic,weak) UILabel *text3;

@property (nonatomic,weak) UIButton *arcBtn;

@end

 

@implementation ZZPickerView

 

- (NSArray *)foods

{

    if (_foods ==nil) {

        // 1.获得plist的全路径

        _foods = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"foods.plist"ofType:nil]];

    }

    return_foods;

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [superinitWithFrame:frame]) {

        [selfsetUpSubViews];

    }

    

    returnself;

}

 

- (instancetype)initWithCoder:(NSCoder *)decoder

{

    if (self = [superinitWithCoder:decoder]) {

        [selfsetUpSubViews];

    }

    

    returnself;

}

 

/**

 *  初始化所有的子控件&进行一次性初始化设置

 */

- (void)setUpSubViews

{

    UIImageView *bgImgView = [[UIImageViewalloc] init];

    bgImgView.image = [UIImageimageNamed:@"new_feature_2"];

    [selfaddSubview:bgImgView];

    self.bgImgView = bgImgView;

    

    UILabel *titleText = [[UILabelalloc] init];

    titleText.text =NSLocalizedString(@"周氏菜谱",nil);

    titleText.textAlignment =NSTextAlignmentCenter;

    titleText.font = [UIFontboldSystemFontOfSize:18.0f];

    titleText.textColor = [UIColorwhiteColor];

    [selfaddSubview:titleText];

    self.titleText = titleText;

    

    UIPickerView *foodPicker = [[UIPickerViewalloc] init];

    foodPicker.delegate =self;

    foodPicker.dataSource =self;

    [selfaddSubview:foodPicker];

    self.foodPicker = foodPicker;

    

    UILabel *fruitLabel = [[UILabelalloc] init];

    fruitLabel.textAlignment =NSTextAlignmentLeft;

    fruitLabel.font = [UIFontboldSystemFontOfSize:16.0f];

    fruitLabel.text =NSLocalizedString(@"水果:",nil);

    fruitLabel.textColor = [UIColorwhiteColor];

    [selfaddSubview:fruitLabel];

    self.fruitLabel = fruitLabel;

    

    UILabel *mainLabel = [[UILabelalloc] init];

    mainLabel.textAlignment =NSTextAlignmentLeft;

    mainLabel.font = [UIFontboldSystemFontOfSize:16.0f];

    mainLabel.text =NSLocalizedString(@"主菜:",nil);

    mainLabel.textColor = [UIColorwhiteColor];

    [selfaddSubview:mainLabel];

    self.mainLabel = mainLabel;

    

    UILabel *drinkLabel = [[UILabelalloc] init];

    drinkLabel.textAlignment =NSTextAlignmentLeft;

    drinkLabel.font = [UIFontboldSystemFontOfSize:16.0f];

    drinkLabel.text =NSLocalizedString(@"饮料:",nil);

    drinkLabel.textColor = [UIColorwhiteColor];

    [selfaddSubview:drinkLabel];

    self.drinkLabel = drinkLabel;

    

    UILabel *text1 = [[UILabelalloc] init];

    text1.textAlignment =NSTextAlignmentLeft;

    text1.font = [UIFontsystemFontOfSize:15.5f];

    text1.textColor = [UIColorwhiteColor];

    [selfaddSubview:text1];

    self.text1 = text1;

    

    UILabel *text2 = [[UILabelalloc] init];

    text2.textAlignment =NSTextAlignmentLeft;

    text2.font = [UIFontsystemFontOfSize:15.5f];

    text2.textColor = [UIColorwhiteColor];

    [selfaddSubview:text2];

    self.text2 = text2;

    

    UILabel *text3 = [[UILabelalloc] init];

    text3.textAlignment =NSTextAlignmentLeft;

    text3.font = [UIFontsystemFontOfSize:15.5f];

    text3.textColor = [UIColorwhiteColor];

    [selfaddSubview:text3];

    self.text3 = text3;

    

    UIButton *arcBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [arcBtn setTitle:NSLocalizedString(@"随机点菜",nil) forState:UIControlStateNormal];

    [arcBtn setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

    [arcBtn addTarget:selfaction:@selector(arcBtnClick)forControlEvents:UIControlEventTouchUpInside];

    arcBtn.titleLabel.font = [UIFontboldSystemFontOfSize:16.0];

    arcBtn.layer.cornerRadius =5;

    arcBtn.layer.masksToBounds =YES;

    arcBtn.layer.borderColor = [UIColorwhiteColor].CGColor;

    arcBtn.layer.borderWidth =1;

    [selfaddSubview:arcBtn];

    self.arcBtn = arcBtn;

}

 

/**

 *  拿到真实的尺寸调整子控件的布局

 */

- (void)layoutSubviews

{

    [superlayoutSubviews];

 

    self.bgImgView.frame =self.frame;

    

    CGFloat titleTextX =0;

    CGFloat titleTextY =30;

    CGFloat titleTextW =self.frame.size.width;

    CGFloat titleTextH =40;

    self.titleText.frame =CGRectMake(titleTextX, titleTextY, titleTextW, titleTextH);

    

    CGFloat foodPickerX =40;

    CGFloat foodPickerY =CGRectGetMaxY(self.titleText.frame);

    CGFloat foodPickerW = titleTextW;

    CGFloat foodPickerH =self.frame.size.height *0.5;

    self.foodPicker.frame =CGRectMake(foodPickerX, foodPickerY, foodPickerW, foodPickerH);

    

    CGFloat fruitLabelX =15;

    CGFloat fruitLabelY =CGRectGetMaxY(self.foodPicker.frame);

    CGFloat fruitLabelW =50;

    CGFloat fruitLabelH =40;

    self.fruitLabel.frame =CGRectMake(fruitLabelX, fruitLabelY, fruitLabelW, fruitLabelH);

    

    CGFloat mainLabelX = fruitLabelX;

    CGFloat mainLabelY =CGRectGetMaxY(self.fruitLabel.frame);

    CGFloat mainLabelW = fruitLabelW;

    CGFloat mainLabelH = fruitLabelH;

    self.mainLabel.frame =CGRectMake(mainLabelX, mainLabelY, mainLabelW, mainLabelH);

    

    CGFloat drinkLabelX = fruitLabelX;

    CGFloat drinkLabelY =CGRectGetMaxY(self.mainLabel.frame);

    CGFloat drinkLabelW = fruitLabelW;

    CGFloat drinkLabelH = fruitLabelH;

    self.drinkLabel.frame =CGRectMake(drinkLabelX, drinkLabelY, drinkLabelW, drinkLabelH);

    

    CGFloat text1X =CGRectGetMaxX(self.fruitLabel.frame);

    CGFloat text1Y = fruitLabelY;

    CGFloat text1W =self.frame.size.width;

    CGFloat text1H = fruitLabelH;

    self.text1.frame =CGRectMake(text1X, text1Y, text1W, text1H);

    

    CGFloat text2X = text1X;

    CGFloat text2Y = mainLabelY;

    CGFloat text2W = text1W;

    CGFloat text2H = fruitLabelH;

    self.text2.frame =CGRectMake(text2X, text2Y, text2W, text2H);

    

    CGFloat text3X = text1X;

    CGFloat text3Y = drinkLabelY;

    CGFloat text3W = text1W;

    CGFloat text3H = fruitLabelH;

    self.text3.frame =CGRectMake(text3X, text3Y, text3W, text3H);

    

    CGFloat arcBtnW =80;

    CGFloat arcBtnX =self.frame.size.width -15 - arcBtnW;

    CGFloat arcBtnY =CGRectGetMaxY(self.text3.frame) + 30;

    CGFloat arcBtnH =40;

    self.arcBtn.frame =CGRectMake(arcBtnX, arcBtnY, arcBtnW, arcBtnH);

}

 

#pragma mark - UIPickerView的数据源方法

/**

 *  有多少列

 */

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    returnself.foods.count;

}

 

/**

 *  第component列显示多少行

 */

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    NSArray *subFoods =self.foods[component];

    return subFoods.count;

}

 

 

#pragma mark - UIPickerView的代理方法

/**

 *  显示什么文字

 */

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    returnself.foods[component][row];

}

 

/**

 *  修改字体颜色

 */

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{

    UILabel *label = [[UILabelalloc] init];

    label.text =self.foods[component][row];

    label.font = [UIFontboldSystemFontOfSize:17.0f];

    label.textColor = [UIColorwhiteColor];

    return label;

}

 

/**

 *  随机选菜

 */

- (void)arcBtnClick

{

    for (int component =0; component < self.foods.count; component++) {

        // 1.每列的个数

        NSInteger count = [self.foods[component]count];

        

        // 2.之前的行号

        NSInteger oldRow = [self.foodPickerselectedRowInComponent:component];

        

        // 3.第几行(默认新的行号跟旧的行号一样)

        NSInteger row = oldRow;

        

        // 4.如果一样则取出不一样

        while (row == oldRow) {

            row = arc4random()%count;

        }

        

        // 5.pickerView主动选择

        [self.foodPickerselectRow:row inComponent:componentanimated:YES];

        

        // 6.设置label的文字

        [selfsetUpTextWith:self.foodPickerrow:row component:component];

    }

}

 

/**

 *  设置文字

 */

- (void)setUpTextWith:(UIPickerView *)foodPicker row:(NSInteger)row component:(NSInteger)component

{

    if (component ==0) { //水果

        self.text1.text =self.foods[component][row];

    } elseif (component == 1) { //主菜

        self.text2.text =self.foods[component][row];

    } elseif (component == 2) { //饮料

        self.text3.text =self.foods[component][row];

    }

}

 

/**

 *  默认今日主推

 */

- (void)todayMainPush

{

    [selfarcBtnClick];

}

@end

 

//

//  ZZViewController.h

//  15-点菜系统

//

//  Created by 周昭 on 16/12/19.

//  Copyright © 2016年 HT_Technology. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ZZViewController : UIViewController

 

@end

 

//

//  ZZViewController.m

//  15-点菜系统

//

//  Created by 周昭 on 16/12/19.

//  Copyright © 2016年 HT_Technology. All rights reserved.

//

 

#import "ZZViewController.h"

#import "ZZPickerView.h"

 

@interface ZZViewController()

 

@end

 

@implementation ZZViewController

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    [selfsetUpSubViews];

}

 

/**

 *  初始化子控件

 */

- (void)setUpSubViews

{

    ZZPickerView *picker = [[ZZPickerViewalloc] init];

    picker.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height);

    [picker todayMainPush];

    [self.viewaddSubview:picker];

}

@end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值