码迷,mamicode.com
首页 > 移动开发 > 详细

iOS- 自定义UIView (测试block和代理)

时间:2014-11-26 10:56:45      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   color   os   sp   for   on   

#import <UIKit/UIKit.h>

 

typedef void(^compeletionHandler) (NSInteger selectButtonIndex);

@class ZSDCustom;

@protocol ZSDCustomDelegate <NSObject>

 

 

-(void)showCustomView:(ZSDCustom *)customView andButtonClick:(NSInteger)selectIndex;

 

@end

@interface ZSDCustom : UIView

@property(nonatomic,copy)compeletionHandler myHandler;

@property(nonatomic,weak)id<ZSDCustomDelegate>delegate;

 

 

-(void)showCustomView:(ZSDCustom *)customView andDelegate:(id)delegate andCompeleteHandler:(compeletionHandler)handler;

@end

 

 

#import "ZSDCustom.h"

#define kDialogWidth 280.0f

#define kButtonSize CGSizeMake(115.0f,45.0f)

@implementation ZSDCustom

{

    UIView *dialogView;

}

-(id)initWithFrame:(CGRect)frame

{

    if (self=[super initWithFrame:frame])

    {

        [self setUp];

    }

    return self;

}

-(void)setUp

{

    self.frame = [UIScreen mainScreen].bounds;

    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];

    

    CGRect frect = CGRectZero;

    frect.size.width = kDialogWidth;

    dialogView = [[UIView alloc]initWithFrame:frect];

    dialogView.backgroundColor = [UIColor whiteColor];

    dialogView.layer.cornerRadius = 4.0f;

    dialogView.layer.masksToBounds = YES;

    [self addSubview:dialogView];

 

    

    //按钮

    frect.origin.x = 15.0f;

    frect.origin.y = 56.0f + 20.0f;

    frect.size = kButtonSize;

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    cancelBtn.frame = frect;

    [cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];

    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];

    [cancelBtn setBackgroundColor:[UIColor colorWithRed:243.0 / 255.0f green:243.0 / 255.0f blue:243.0 / 255.0f alpha:1.0]];

    [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [cancelBtn.layer setBorderWidth:1.0];

    [cancelBtn.layer setBorderColor:[UIColor grayColor].CGColor];

    [cancelBtn.layer setCornerRadius:4.0f];

    [cancelBtn setTag:1];

    [dialogView addSubview:cancelBtn];

    

    frect.origin.x = kDialogWidth - 15.0f - kButtonSize.width;

    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    okBtn.frame = frect;

    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [okBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];

    [okBtn setTitle:@"确认" forState:UIControlStateNormal];

    [okBtn setBackgroundColor:[UIColor colorWithRed:207.0 / 255.0f green:44.0 / 255.0f blue:65.0 / 255.0f alpha:1.0]];

    [okBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [okBtn.layer setCornerRadius:4.0f];

    [okBtn setTag:2];

    [dialogView addSubview:okBtn];

    

    float totalHeight = CGRectGetMaxY(okBtn.frame) + 20.0f;

    frect = dialogView.frame;

    frect.origin.x = (self.bounds.size.width - kDialogWidth) / 2.0f;

    frect.origin.y = (self.bounds.size.height - totalHeight) / 2.0f;

    frect.size.height = totalHeight;

    dialogView.frame = frect;

 

}

-(void)buttonClick:(UIButton *)sender

{

    if (_myHandler) {

        _myHandler(sender.tag);

    }

    if (sender.tag==2) {

        if (_delegate&&[_delegate respondsToSelector:@selector(showCustomView:andButtonClick:)]) {

            

           [_delegate showCustomView:self andButtonClick:sender.tag];

        }

    }

    else

    {

        [self hide];

    }

}

-(void)show

{

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

    [keyWindow addSubview:self];

    

    dialogView.alpha = 0;

    dialogView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);

    

    [UIView animateWithDuration:0.3f animations:^{

        dialogView.alpha = 1.0;

        dialogView.transform = CGAffineTransformMakeScale(1.0, 1.0);

    }];

}

 

-(void)hide

{

    [UIView animateWithDuration:0.3f animations:^{

        dialogView.alpha = 0;

        dialogView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];

    }];

}

-(void)showCustomView:(ZSDCustom *)customView andDelegate:(id)delegate andCompeleteHandler:(compeletionHandler)handler

{

    [customView show];

    _delegate=delegate;

    _myHandler=handler;

}

@end

 

#import "ViewController.h"

#import "ZSDCustom.h"

@interface ViewController ()<ZSDCustomDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)showViewBtnClick:(UIButton *)sender

{

    

    ZSDCustom *custom=[[ZSDCustom alloc]initWithFrame:CGRectZero];

    

    [custom showCustomView:custom andDelegate:self andCompeleteHandler:^(NSInteger selectButtonIndex) {

        

        NSLog(@"selectButtonIndex=%ld",selectButtonIndex);

    }];

}

-(void)showCustomView:(ZSDCustom *)customView andButtonClick:(NSInteger)selectIndex

{

 NSLog(@"selectIndex=%ld",selectIndex);

}

@end

 

bubuko.com,布布扣

 

iOS- 自定义UIView (测试block和代理)

标签:blog   http   io   ar   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/thbbsky/p/4122546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!