iOS中动态密码(倒计时按钮)按钮Block封装,选取器(UIPickerView)的Block封装,日期选取器(UIDatePicker)的Block封装

常用的一些控件,封装起来用的时候容易调用。

 

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://github.com/XiaoHeHe1/ControlBlockDemo/tree/master

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值