注册——性别选择、年龄选择、UIPickerView的简单使用

性别选择

 //

//  QJSexChangeController.m
//  
//
//  Created by 袁俊亮 on 15/7/15.
//  Copyright (c) 2015年 yuanjunliang. All rights reserved.
//

#import "QJSexSelectedController.h"
#import "QJUserDateController.h"
#import "QJUserInfo.h"

#define titleMargin 64 // 标题距离头部的尺度
#define titleToIconMargin 54 // 标题和第一个女性头像之间的间距
#define iconToLabelZHMargin 12 // 头像和label之间的间距
#define zhLabelTOenLabelMargin 6 // 中文label和英文label之间的间距
#define enLabelTOIcon 52 // 第一个女性英文label到男性icon之间的距离

#define iconWidth womanIcon.currentBackgroundImage.size.width * 0.8
#define iconHeight womanIcon.currentBackgroundImage.size.height * 0.8


@interface QJSexSelectedController ()
/**
 *  选择性别
 
*/
@property (nonatomic, assign) sexSelected sexSelect;
@property (nonatomic, weak) UIButton *manIcon;
@property (nonatomic, weak) UIButton *womanIcon;
@end

@implementation QJSexSelectedController

- (instancetype)init
{
    if (self = [super init]) {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"background"]];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, titleMargin, self.view.width, 20)];
    titleLabel.text = @"您的性别";
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    
    // 添加女性头像
    UIButton *womanIcon = [[UIButton alloc] init];
    [self addSexBtn:womanIcon sexSelected:sexSelectedWomen WithImageName:@"woman_normal" highlightedName:@"woman_highlighted"];
    CGFloat womanIconX = self.view.width * 0.5 - iconWidth * 0.5;
    CGFloat womanIconY = titleLabel.frame.origin.y + titleLabel.frame.size.height * 0.5 + titleToIconMargin;
    CGFloat womanIconW = iconWidth;
    CGFloat womanIconH = iconHeight;
    womanIcon.frame = CGRectMake(womanIconX, womanIconY, womanIconW, womanIconH);
    [womanIcon addTarget:self action:@selector(selectSex:) forControlEvents:UIControlEventTouchUpInside];
    self.womanIcon = womanIcon;
    [self.view addSubview:womanIcon];
    
    // 添加女性中文label
    CGFloat womanLabelZHX = 0;
    CGFloat womanLabelZHY = womanIconY + womanIconH + iconToLabelZHMargin;
    CGFloat womanLabelZHW = self.view.width;
    CGFloat womanLabelZHH = 15;
    
    UILabel *womanLabelZH = [[UILabel alloc] initWithFrame:CGRectMake(womanLabelZHX,womanLabelZHY , womanLabelZHW, womanLabelZHH)];
    womanLabelZH.text = @"女性";
    womanLabelZH.textAlignment = UITextAlignmentCenter;
    womanLabelZH.font = [UIFont boldSystemFontOfSize:15];
    womanLabelZH.textColor = [UIColor whiteColor];
    [self.view addSubview:womanLabelZH];
    
    // 添加女性英文Label
    CGFloat womanLabelENX = 0;
    CGFloat womanLabelENY = womanLabelZHY + womanLabelZHH + zhLabelTOenLabelMargin;
    CGFloat womanLabelENW = self.view.width;
    CGFloat womanLabelENH = 12;
    UILabel *womanLabelEN = [[UILabel alloc] initWithFrame:CGRectMake(womanLabelENX, womanLabelENY, womanLabelENW, womanLabelENH)];
    womanLabelEN.text = @"Female";
    womanLabelEN.font = [UIFont systemFontOfSize:12];
    womanLabelEN.textColor = [UIColor whiteColor];
    womanLabelEN.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:womanLabelEN];
    
    /************************/
    // 添加男性头像
    UIButton *manIcon = [[UIButton alloc] init];
    [self addSexBtn:manIcon sexSelected:sexSelectedMan WithImageName:@"man_normal" highlightedName:@"man_highlighted"];
    CGFloat manIconX = self.view.width * 0.5 - iconWidth * 0.5;
    CGFloat manIconY = womanLabelENY + womanLabelENH + enLabelTOIcon;
    CGFloat manIconW = iconWidth;
    CGFloat manIconH = iconHeight;
    manIcon.frame = CGRectMake(manIconX, manIconY, manIconW, manIconH);
    [manIcon addTarget:self action:@selector(selectSex:) forControlEvents:UIControlEventTouchUpInside];
    self.manIcon = manIcon;
    [self.view addSubview:manIcon];
    
    // 添加男性中文label
    CGFloat manLabelZHX = 0;
    CGFloat manLabelZHY = manIconY + manIconH + iconToLabelZHMargin;
    CGFloat manLabelZHW = self.view.width;
    CGFloat manLabelZHH = 15;
    
    UILabel *manLabelZH = [[UILabel alloc] initWithFrame:CGRectMake(manLabelZHX,manLabelZHY , manLabelZHW, manLabelZHH)];
    manLabelZH.text = @"男性";
    manLabelZH.textAlignment = UITextAlignmentCenter;
    manLabelZH.font = [UIFont boldSystemFontOfSize:15];
    manLabelZH.textColor = [UIColor whiteColor];
    [self.view addSubview:manLabelZH];
    
    // 添加男性英文Label
    CGFloat manLabelENX = 0;
    CGFloat manLabelENY = manLabelZHY + manLabelZHH + zhLabelTOenLabelMargin;
    CGFloat manLabelENW = self.view.width;
    CGFloat manLabelENH = 12;
    UILabel *manLabelEN = [[UILabel alloc] initWithFrame:CGRectMake(manLabelENX, manLabelENY, manLabelENW, manLabelENH)];
    manLabelEN.text = @"Male";
    manLabelEN.font = [UIFont systemFontOfSize:12];
    manLabelEN.textColor = [UIColor whiteColor];
    manLabelEN.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:manLabelEN];
    
    // 添加上一步下一步按钮背景框
    [self creatPreNextStepBtn];
    
    // 默认选中女性
    [self selectSex:womanIcon];
}


/**
 *  添加性别头像按钮
 
*/
- (void)addSexBtn:(UIButton *)sexBtn sexSelected:(sexSelected)sexSelect WithImageName:(NSString *)name highlightedName:(NSString *)highlightedName
{
    [sexBtn setBackgroundImage:[UIImage imageWithName:name] forState:UIControlStateNormal];
    [sexBtn setBackgroundImage:[UIImage imageWithName:highlightedName] forState:UIControlStateSelected];
    sexBtn.tag = sexSelect;
    
    [self.view addSubview:sexBtn];
}

/**
 *  选择性别
 
*/
- (void)selectSex:(UIButton *)sexBtn
{
    QJUserInfo *userInfo = [[QJUserInfo alloc] init];
    if (sexBtn.tag == sexSelectedMan) {
        userInfo.sexSelect = sexSelectedMan;
        self.manIcon.selected = YES;
        self.womanIcon.selected = NO;
    }else if (sexBtn.tag == sexSelectedWomen){
        userInfo.sexSelect = sexSelectedWomen;
        self.womanIcon.selected = YES;
        self.manIcon.selected = NO;
    }
    QJLog(@"%d",userInfo.sexSelect);
}

/**
 *  创建上一步下一步按钮
 
*/
- (void)creatPreNextStepBtn
{
    // 添加上一步下一步按钮背景框
    UIButton *preNextBackgroundImage = [[UIButton alloc] init];
    [preNextBackgroundImage setBackgroundImage:[UIImage imageWithName:@"pre_next_step_background"] forState:UIControlStateNormal];
    CGFloat preNextBackgroundImageX = self.view.width * 0.5 - preNextBackgroundImage.currentBackgroundImage.size.width * 0.5;
    CGFloat preNextBackgroundImageY = self.view.height - preNextStepBtnTOendMargin - preNextBackgroundImage.currentBackgroundImage.size.height;
    CGFloat preNextBackgroundImageW = preNextBackgroundImage.currentBackgroundImage.size.width;
    CGFloat preNextBackgroundImageH = preNextBackgroundImage.currentBackgroundImage.size.height;
    preNextBackgroundImage.frame = CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW, preNextBackgroundImageH);
    
    [self.view addSubview:preNextBackgroundImage];
    
    // 添加上一步按钮
    UIButton *preStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW * 0.5, preNextBackgroundImageH)];
    [preStepBtn setBackgroundImage:[UIImage imageWithName:@"pre_step_background"] forState:UIControlStateHighlighted];
    [preStepBtn addTarget:self action:@selector(preStep) forControlEvents:UIControlEventTouchUpInside];
    [preStepBtn setTitle:@"上一步" forState:UIControlStateNormal];
    
    [self.view addSubview:preStepBtn];
    
    // 下一步按钮
    UIButton *nextStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX + preNextBackgroundImageW * 0.5 +1, preNextBackgroundImageY, preNextBackgroundImageW * 0.5 -1, preNextBackgroundImageH)];
    [nextStepBtn setBackgroundImage:[UIImage imageWithName:@"next_step_background"] forState:UIControlStateHighlighted];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextStepBtn];
}

/**
 *  上一步
 
*/
- (void)preStep
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  下一步
 
*/
- (void)nextStep
{
    QJUserDateController *userDateVC = [[QJUserDateController alloc] init];
    [self presentViewController:userDateVC animated:YES completion:nil];
}

@end

 

年龄选择——使用UIDatePicker

 //

//  QJUserDateController.m
//  
//
//  Created by 袁俊亮 on 15/7/15.
//  Copyright (c) 2015年 yuanjunliang. All rights reserved.
//

#import "QJUserDateController.h"
#import "QJSexSelectedController.h"
#import "QJSkinTypeViewController.h"
#import "SRMonthPicker.h"

#define highForRow 39 //每一行的高度

@interface QJUserDateController ()<SRMonthPickerDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, weak) SRMonthPicker *datePicker;
@end

@implementation QJUserDateController

- (instancetype)init
{
    if (self = [super init]) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"background"]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(072, self.view.width, 44)];
    titleLabel.text = @"您的年龄";
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];

    // 创建datePicker
    SRMonthPicker *datePicker = [[SRMonthPicker alloc] initWithFrame:CGRectMake(0, titleLabel.y + 100300300)];
    datePicker.monthPickerDelegate = self;
    [self.view addSubview:datePicker];
    datePicker.enableColourRow = YES;
    datePicker.delegate = self;
    self.datePicker = datePicker;
    
    // 设置默认选中的年
    NSDateFormatter *yearDateFormatter = [[NSDateFormatter alloc] init];
    [yearDateFormatter setDateFormat:@"yyyy"];
    NSInteger yearSelect = [[yearDateFormatter stringFromDate:self.datePicker.date] integerValue] - 1;
    [self.datePicker selectRow:yearSelect inComponent:0 animated:YES];
    
    // 设置默认选中的月
    NSDateFormatter *mounthDateFormatter = [[NSDateFormatter alloc] init];
    [mounthDateFormatter setDateFormat:@"MM"];
    NSInteger mounthSelect = [[mounthDateFormatter stringFromDate:self.datePicker.date] integerValue] - 1;
    [self.datePicker selectRow:mounthSelect inComponent:1 animated:YES];
    
    // 添加上一步下一步
    [self creatPreNextStepBtn];
}

#pragma mark - SRMonthPickerDelegate
/**
 *  获取选择时间
 
*/
- (void)monthPickerDidChangeDate:(SRMonthPicker *)monthPicker
{
    // 时间格式
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSString *dateStr = [dateFormatter stringFromDate:self.datePicker.date];
    QJLog(@"%@",dateStr);
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return highForRow;
}


/**
 *  创建上一步下一步按钮
 
*/
- (void)creatPreNextStepBtn
{
    // 添加上一步下一步按钮背景框
    UIButton *preNextBackgroundImage = [[UIButton alloc] init];
    [preNextBackgroundImage setBackgroundImage:[UIImage imageWithName:@"pre_next_step_background"] forState:UIControlStateNormal];
    CGFloat preNextBackgroundImageX = self.view.width * 0.5 - preNextBackgroundImage.currentBackgroundImage.size.width * 0.5;
    CGFloat preNextBackgroundImageY = self.view.height - preNextStepBtnTOendMargin - preNextBackgroundImage.currentBackgroundImage.size.height;
    CGFloat preNextBackgroundImageW = preNextBackgroundImage.currentBackgroundImage.size.width;
    CGFloat preNextBackgroundImageH = preNextBackgroundImage.currentBackgroundImage.size.height;
    preNextBackgroundImage.frame = CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW, preNextBackgroundImageH);
    
    [self.view addSubview:preNextBackgroundImage];
    
    // 添加上一步按钮
    UIButton *preStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW * 0.5, preNextBackgroundImageH)];
    [preStepBtn setBackgroundImage:[UIImage imageWithName:@"pre_step_background"] forState:UIControlStateHighlighted];
    [preStepBtn addTarget:self action:@selector(preStep) forControlEvents:UIControlEventTouchUpInside];
    [preStepBtn setTitle:@"上一步" forState:UIControlStateNormal];
    
    [self.view addSubview:preStepBtn];
    
    // 下一步按钮
    UIButton *nextStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX + preNextBackgroundImageW * 0.5 +1, preNextBackgroundImageY, preNextBackgroundImageW * 0.5 -1, preNextBackgroundImageH)];
    [nextStepBtn setBackgroundImage:[UIImage imageWithName:@"next_step_background"] forState:UIControlStateHighlighted];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextStepBtn];
}

/**
 *  上一步
 
*/
- (void)preStep
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  下一步
 
*/
- (void)nextStep
{
    QJSkinTypeViewController *skinTypeVC = [[QJSkinTypeViewController alloc] init];
    [self presentViewController:skinTypeVC animated:YES completion:nil];
}

@end

 

UIPickerView的简单使用

//
//  QJSkinTypeViewController.m
//  
//
//  Created by 袁俊亮 on 15/7/16.
//  Copyright (c) 2015年 yuanjunliang. All rights reserved.
//

#import "QJSkinTypeViewController.h"
#import "QJUserDateController.h"

@interface QJSkinTypeViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *skinTypeArray;

@property (nonatomic, weak) UIPickerView *skinPickerView;
@end

@implementation QJSkinTypeViewController

- (NSArray *)skinTypeArray
{
    if (!_skinTypeArray) {
       _skinTypeArray = @[@"干性肌肤"@"油性肌肤"@"混合性肌肤"@"中性肌肤"@"敏感性肌肤"];
    }
    return _skinTypeArray;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"background"]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // titleLabel
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(072, self.view.width, 44)];
    titleLabel.text = @"您的肤质";
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:titleLabel];
    
    // UIPickerView属性设置
    UIPickerView *skinPickerView = [[UIPickerView alloc] init];
    skinPickerView.frame = CGRectMake(0, titleLabel.y + 100 , self.view.width, 216);
    skinPickerView.dataSource = self;
    skinPickerView.delegate = self;
    [skinPickerView selectRow:2 inComponent:0 animated:YES];
    [self.view addSubview:skinPickerView];
    self.skinPickerView = skinPickerView;
    
    // 添加上一步下一步按钮
    [self creatPreNextStepBtn];
}


#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 5;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.skinTypeArray[row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *rowLabel = [[UILabel alloc] init];
    rowLabel.textAlignment = UITextAlignmentCenter;
    rowLabel.textColor = [UIColor whiteColor];
    rowLabel.font = [UIFont boldSystemFontOfSize:22];
    rowLabel.text = self.skinTypeArray[row];
    
    return rowLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%@",self.skinTypeArray[row]);
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44;
}


#pragma mark - 上一步下一步
/**
 *  创建上一步下一步按钮
 
*/
- (void)creatPreNextStepBtn
{
    // 添加上一步下一步按钮背景框
    UIButton *preNextBackgroundImage = [[UIButton alloc] init];
    [preNextBackgroundImage setBackgroundImage:[UIImage imageWithName:@"pre_next_step_background"] forState:UIControlStateNormal];
    CGFloat preNextBackgroundImageX = self.view.width * 0.5 - preNextBackgroundImage.currentBackgroundImage.size.width * 0.5;
    CGFloat preNextBackgroundImageY = self.view.height - preNextStepBtnTOendMargin - preNextBackgroundImage.currentBackgroundImage.size.height;
    CGFloat preNextBackgroundImageW = preNextBackgroundImage.currentBackgroundImage.size.width;
    CGFloat preNextBackgroundImageH = preNextBackgroundImage.currentBackgroundImage.size.height;
    preNextBackgroundImage.frame = CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW, preNextBackgroundImageH);
    
    [self.view addSubview:preNextBackgroundImage];
    
    // 添加上一步按钮
    UIButton *preStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX, preNextBackgroundImageY, preNextBackgroundImageW * 0.5, preNextBackgroundImageH)];
    [preStepBtn setBackgroundImage:[UIImage imageWithName:@"pre_step_background"] forState:UIControlStateHighlighted];
    [preStepBtn addTarget:self action:@selector(preStep) forControlEvents:UIControlEventTouchUpInside];
    [preStepBtn setTitle:@"上一步" forState:UIControlStateNormal];
    
    [self.view addSubview:preStepBtn];
    
    // 下一步按钮
    UIButton *nextStepBtn = [[UIButton alloc] initWithFrame:CGRectMake(preNextBackgroundImageX + preNextBackgroundImageW * 0.5 +1, preNextBackgroundImageY, preNextBackgroundImageW * 0.5 -1, preNextBackgroundImageH)];
    [nextStepBtn setBackgroundImage:[UIImage imageWithName:@"next_step_background"] forState:UIControlStateHighlighted];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextStepBtn];
}

/**
 *  上一步
 
*/
- (void)preStep
{
    QJUserDateController *userDateVC = [[QJUserDateController alloc] init];
    [self presentViewController:userDateVC animated:YES completion:nil];
}

/**
 *  下一步
 
*/
- (void)nextStep
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end 

其中的图片自己更换 

posted @ 2015-07-21 09:33  aprogrammer  阅读(649)  评论(0编辑  收藏  举报