iOS中动态密码(倒计时按钮)按钮Block封装,选取器(UIPickerView)的Block封装,日期选取器(UIDatePicker)的Block封装
来源:互联网 发布:陈奕迅 爱情转移 知乎 编辑:程序博客网 时间:2024/06/05 00:35
常用的一些控件,封装起来用的时候容易调用。
1:动态密码(倒计时按钮)按钮
实现代码
////// BlockDynamicPasswordButton.h//// Created by yfc on 16/8/29.////#import <UIKit/UIKit.h>typedef void(^DynamicBtnBlock)(int touchUpNum);@interface BlockDynamicPasswordButton : UIButton{ int limmit; //记录秒数 int countDownNum; //点击的次数 int maxCountDownNum; //能够点击总次数 int timeInterval; //时间间隔}@property(nonatomic,copy)DynamicBtnBlock dynamicBtnBlock;@property(nonatomic,retain)NSTimer *timer_;-(instancetype)initWithFrame:(CGRect)frame andMaxTouchDownNum:(int)countDownNum_ andTimeInterval:(int)limit_ andClickedAction:(DynamicBtnBlock)block;@end
////// BlockDynamicPasswordButton.m//// Created by yfc on 16/8/29.////#import "BlockDynamicPasswordButton.h"@implementation BlockDynamicPasswordButton@synthesize timer_;-(instancetype)initWithFrame:(CGRect)frame andMaxTouchDownNum:(int)countDownNum_ andTimeInterval:(int)limit_ andClickedAction:(DynamicBtnBlock)block{ if (self = [super initWithFrame:frame]) { maxCountDownNum = countDownNum_; timeInterval = limit_; self.dynamicBtnBlock = block; UIImage *bimage = [UIImage imageNamed:@"btn.png"]; [self setBackgroundImage: bimage forState:UIControlStateNormal]; [self setBackgroundImage: bimage forState:UIControlStateDisabled]; [self setTitle:@"获取动态密码" forState:UIControlStateNormal]; [self setTitle:@"获取中..." forState:UIControlStateDisabled]; self.titleLabel.font = [UIFont systemFontOfSize: 12]; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; self.enabled = YES; [self addTarget:self action:@selector(dynamicPasswordButtonAction:) forControlEvents:UIControlEventTouchUpInside]; //记录点击的次数 countDownNum = 0; //记录秒数 limmit = 0; } return self;}- (void) dynamicPasswordButtonAction:(UIButton *)btn{ [self setTitle:@"重新发送" forState:UIControlStateNormal]; self.enabled = NO; countDownNum += 1; self.dynamicBtnBlock(countDownNum); if(!self.timer_){ NSTimeInterval timeInterval2 = 1.0; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval2 target:self selector:@selector(timerAction:) userInfo:self repeats:YES]; self.timer_ = timer; }else{ //开启定时器 [self.timer_ setFireDate:[NSDate distantPast]]; } }- (void) timerAction:(id)sender{ //-1因为1秒后才开的定时器 if (timeInterval-1-1 >= limmit ) { NSLog(@"%ld秒后可以继续操作",timeInterval-(long)limmit -1) ; limmit+=1; }else{ limmit = 0; NSLog(@"可以点了"); // 计时器可以停了 if (timer_) { //关闭定时器 [timer_ setFireDate:[NSDate distantFuture]]; } if (countDownNum >= maxCountDownNum) { UIImage *bimage = [UIImage imageNamed:@"pagebuttondisable.png"]; [self setBackgroundImage:bimage forState:UIControlStateDisabled]; [self setTitle:@"" forState:UIControlStateDisabled]; return; } self.enabled = YES; } }-(void)dealloc{ NSLog(@" PublicDynamicPasswordButton dealloc");// [timer_ release];// [super dealloc];}@end
调用方法
// //1.封装动态密码按钮 //参数说明:MaxTouchDownNum:最多能点击的次数 TimeInterval:多少秒后才可以点 ClickedAction:点击按钮的响应事件 // BlockDynamicPasswordButton *_dynamicButton = [[BlockDynamicPasswordButton alloc]initWithFrame:CGRectMake(80, 80, 80, 30) andMaxTouchDownNum:15 andTimeInterval:10 andClickedAction:^(int touchUpNum) { NSLog(@"提交页面获取到的点击次数%d",touchUpNum); }]; _dynamicButton.backgroundColor = [UIColor blackColor]; [self.view addSubview:_dynamicButton]; _dynamicButton.centerX = SCREEN_WIDTH_NEW/2;
2:选取器(UIPickerView)的Block封装
////// BlockPickerView.h//// Created by yfc on 16/8/26.////#import <UIKit/UIKit.h>typedef void(^PickerBlock)(id result);@interface BlockPickerView : UIView<UIPickerViewDataSource, UIPickerViewDelegate>{ NSArray *pickerArray;}@property(nonatomic,copy)UIView *backGroundview;@property(nonatomic,copy)PickerBlock pickerBlock;@property(nonatomic,retain)UIPickerView *pickerView;- (instancetype)initWithFrame:(CGRect)frame doneAction:(PickerBlock)block;- (void)show;- (void)setDataSource:(NSDictionary *)dic;@end
////// BlockPickerView.m//// Created by yfc on 16/8/26.////#import "BlockPickerView.h"#import "config.h"@implementation BlockPickerView-(instancetype)initWithFrame:(CGRect)frame doneAction:(PickerBlock)block{ if (self = [super initWithFrame:frame]) { // //确定取消按钮下面的白条+紫条 // UIImageView *purpleLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH_NEW, 45)]; purpleLineView.backgroundColor = [UIColor redColor]; purpleLineView.userInteractionEnabled = YES; // //取消 // UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside]; cancelButton.frame = CGRectMake(10, 5, 63, 32); // //确定 // UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(250, 6, 63, 34); rightButton.right = SCREEN_WIDTH_NEW - 10; [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside]; [rightButton setTitle:@"确定" forState:UIControlStateNormal]; [purpleLineView addSubview:cancelButton]; [purpleLineView addSubview:rightButton]; // //选取框 // _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH_NEW, 180)]; _pickerView.delegate = self; _pickerView.dataSource = self; _pickerView.backgroundColor = [UIColor whiteColor]; //是否展示中间的选中状态 _pickerView.showsSelectionIndicator = YES; [self addSubview:purpleLineView]; [self addSubview:_pickerView]; self.pickerBlock = block; } return self;}//确定按钮- (void)doneAction:(id)sender{ NSInteger row = [_pickerView selectedRowInComponent:0]; self.pickerBlock(pickerArray[row]); [self cancelAction:nil];}//取消按钮- (void)cancelAction:(UIButton *)sender{ [UIView animateWithDuration:0.5 animations:^{ CGRect rect = self.frame; rect.origin.y = SCREEN_HEIGHT_NEW; self.frame = rect; } completion:^(BOOL finished) { [self.backGroundview removeFromSuperview]; // //这行如果不写,此控件将不会被释放.如果不写延迟,则没有下收的效果 // [self removeFromSuperview]; }]; }- (void)show;{ kWeakObject(self); if (!_backGroundview) { _backGroundview = [[UIView alloc]initWithFrame:APPLICATION.window.bounds]; _backGroundview.backgroundColor = [UIColor blackColor]; _backGroundview.alpha = 0.5; [_backGroundview setTapActionWithBlock:^{ [weakObject cancelAction:nil]; }]; } _backGroundview.userInteractionEnabled = NO; [APPLICATION.window addSubview:_backGroundview]; [APPLICATION.window addSubview:self]; [UIView animateWithDuration:0.5 animations:^{ CGRect rect = self.frame; rect.origin.y = SCREEN_HEIGHT_NEW - self.frame.size.height; self.frame = rect; } completion:^(BOOL finished) { _backGroundview.userInteractionEnabled = YES; }];}- (void)setDataSource:(NSDictionary *)dic;{ if(((NSArray*)[dic objectForKey:@"array"]).count > 0){ NSLog(@"不是空数组"); pickerArray = [[NSArray alloc]initWithArray:[dic objectForKey:@"array"]]; }else{ NSLog(@"是空数组"); } [_pickerView reloadAllComponents];}-(void)dealloc{ NSLog(@"PublicPickerView dealloc");// [pickerArray release];// [_backGroundview release];// [_pickerView release];// [super dealloc];}#pragma mark - pickerdelegate- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return pickerArray.count;}//- (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{// //这个不居中// return [pickerArray[row] objectForKey:@"valueForShow"];//}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UIView *viewToUse = nil; if (component == 0) { NSString *text = [pickerArray[row] objectForKey:@"valueForShow"]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 260.0, 40.0)]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentCenter; label.text = text; label.font = [UIFont boldSystemFontOfSize:18.0]; return label; } return viewToUse;}@end
调用方法
- (void)pickerButtonAction:(UIButton *)btn{ // //说明:数据源是一个字典 key是@"array" ,value是一个数组,数组里是多个字典(必须有一个key是@"valueForShow",用作展示) // if (!pickerView) { pickerView = [[BlockPickerView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT_NEW,SCREEN_WIDTH_NEW,225) doneAction:^(id result) { NSLog(@"选中的是%@",result); }]; NSDictionary *dicc = @{@"array":@[@{@"valueForShow":@"111"}, @{@"valueForShow":@"222"}]}; [pickerView setDataSource:dicc]; } [pickerView show];}
3:日期选取器(UIDatePicker)的Block封装
//// // BlockDatePickerView.h//// Created by yfc on 16/8/27.////#import <UIKit/UIKit.h>typedef void(^DatePickerBlock)(id result);@interface BlockDatePickerView : UIView{ UIView *backGroundview; UIDatePicker *datePicker_;}@property(nonatomic,copy)DatePickerBlock datePickerBlock;- (void)show;- (void)setDoneAction:(DatePickerBlock)block;@end
////// BlockDatePickerView.m//// Created by yfc on 16/8/27.////#import "BlockDatePickerView.h"#import "config.h"@implementation BlockDatePickerView- (id)initWithFrame:(CGRect)frame { // frame.origin.y = [GlobalVariables sharedGlobalVariables].Screen_Height; if(self = [super initWithFrame:frame]){ self.userInteractionEnabled = YES; self.backgroundColor = [UIColor whiteColor]; datePicker_ = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 40.0, SCREEN_WIDTH_NEW, 180)]; datePicker_.datePickerMode = UIDatePickerModeDate; datePicker_.timeZone = [NSTimeZone systemTimeZone]; [self addSubview:datePicker_]; UIImageView *bar = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH_NEW, 45.0)]; bar.backgroundColor = [UIColor redColor]; [self addSubview:bar];// [bar release]; UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; [confirmButton setTitle:@"确定" forState:UIControlStateNormal]; [confirmButton addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside]; confirmButton.frame = CGRectMake(251, 6, 63, 34); confirmButton.right = SCREEN_WIDTH_NEW - 10; [self addSubview:confirmButton];// [confirmButton release]; UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside]; cancelButton.frame = CGRectMake(10, 6, 63, 34); [self addSubview:cancelButton];// [cancelButton release]; } return self;}//确定按钮- (void)doneAction:(id)sender{// NSString *dateString = [NSString stringWithFormat:@"%@",datePicker_.date];// dateString = [dateString substringToIndex:10];// // self.datePickerBlock(dateString);// [self cancelAction:nil]; //20161108 生活 3 testin NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit; NSDateComponents *comps = [calendar components:unitFlags fromDate:datePicker_.date]; NSInteger iYear = [comps year]; NSInteger imonth = [comps month]; NSInteger iDay = [comps day]; NSString *dateString = [NSString stringWithFormat:@"%4ld-%02ld-%02ld",(long)iYear,(long)imonth,(long)iDay]; self.datePickerBlock(dateString); [self cancelAction:nil];}//取消按钮- (void)cancelAction:(UIButton *)sender{ [backGroundview removeFromSuperview]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; CGRect rect = self.frame; rect.origin.y = SCREEN_HEIGHT_NEW; self.frame = rect; [UIView commitAnimations];}- (void)show{ kWeakObject(self); if (!backGroundview) { backGroundview = [[UIView alloc]initWithFrame:APPLICATION.window.bounds]; backGroundview.backgroundColor = [UIColor blackColor]; backGroundview.alpha = 0.5; [backGroundview setTapActionWithBlock:^{ [weakObject cancelAction:nil]; }]; } [APPLICATION.window addSubview:backGroundview]; [APPLICATION.window addSubview:self]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; CGRect rect = self.frame; rect.origin.y = SCREEN_HEIGHT_NEW - self.height; self.frame = rect; [UIView commitAnimations];}- (void)setDoneAction:(DatePickerBlock)block;{ self.datePickerBlock = block;}-(void)dealloc{ NSLog(@"PublicDatePickerView dealloc");// [backGroundview release];// [datePicker_ release];// [super dealloc];}@end
调用方法
- (void)dateTextFiledAction:(UIButton *)btn{ if (!datePickerView) { datePickerView = [[BlockDatePickerView alloc]initWithFrame:CGRectMake(0,APPLICATION.window.height,SCREEN_WIDTH_NEW,225)]; [datePickerView setDoneAction:^(id result) { NSLog(@"选中的是%@",result); }]; } [datePickerView show];}
示例demo下载地址:https://pan.baidu.com/s/1nuPHDyx
阅读全文
0 0
- iOS中动态密码(倒计时按钮)按钮Block封装,选取器(UIPickerView)的Block封装,日期选取器(UIDatePicker)的Block封装
- ios按钮事件的block封装
- ASI的封装 block
- iOS block与封装
- Block 的 基础 封装方法中遇到的block问题
- 封装顶部选择按钮(使用block可以在当前控制器下面添加方法)
- 封装顶部选择按钮(用block实现在当前控制器下面添加button点击方法)
- ASIFormDataRequest /AFNetworking GET/POST请求的简单封装(block)
- OC-034.block类型(封装代码)
- 使用block封装的下载类-iOS开发
- IOS 底层封装Block动画的原理解析
- Block封装http请求
- iOS开发 选择日期的view 一(UIDatePicker的封装)
- iOS-利用Block二次封装AFNetworking 3.0
- IOS 数据库底层封装和Block回调
- vi/vim的VISUAL BLOCK使用方法(文字选取粘贴功能)
- 对AFNetworking简单封装GET,POST请求(block方法)
- UIDatePicker 日期、时间选取器
- 查找n个元素中的第二小元素
- 读取excel
- A Discriminatively Trained, Multiscale, Deformable Part Model(判别训练的多尺度可变形部件模型——中文翻译版)
- UVA 11538 Chess Queen(数学)
- Kali更新源&dpkg:处理软件包dradis (--configure)时出错
- iOS中动态密码(倒计时按钮)按钮Block封装,选取器(UIPickerView)的Block封装,日期选取器(UIDatePicker)的Block封装
- 学会使用ES6中的箭头函数
- 磁疗是真的吗?恒定磁场对骨折愈合的影响
- python可变参数练习
- JavaWeb学习十二(自定义标签)
- 基于VS与IIS程序调试,银行支付接口调试操作
- html页面中止加载
- 自定义View实战--实现一个清新美观的加载按钮
- GCD