码迷,mamicode.com
首页 > 其他好文 > 详细

block 传值的小程序

时间:2015-04-01 14:54:29      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//

//  ViewController.m

//  block_chuanzhi

//

//  Created by imac on 15-4-1.

//  Copyright (c) 2015年 imac. All rights reserved.

//

#import "ViewController.h"

#import "InputView.h"

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *labelText;

- (IBAction)btnAction:(UIButton *)sender;

 

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

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

}

 

 

- (IBAction)btnAction:(UIButton *)sender {

    InputView *inputView = [[InputView alloc] init];

    inputView.block = ^(NSString *text)

    {

        _labelText.text = text;

    };

    [inputView show];

}

@end

———————————————————————————————————————————————————————————— 

——————————————————————————————————————————————————————————————

 

#import <UIKit/UIKit.h>

typedef void(^InputBlock)(NSString *);

@interface InputView : UIView

@property (nonatomic , copy)InputBlock block;

- (void)show;

@end

——————————————————————————————————————————————————————————————

——————————————————————————————————————————————————————————————

 

//

//  InputView.m

//  block_chuanzhi

//

//  Created by imac on 15-4-1.

//  Copyright (c) 2015年 imac. All rights reserved.

//

 

#import "InputView.h"

#define kSceenWidth self.bounds.size.width

#define kSceenHeight self.bounds.size.width

@implementation InputView

{

    UITextView *_textView;

}

 

- (id)initWithFrame:(CGRect)frame

{

    frame = CGRectMake(25, 20, 300, 300);

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor orangeColor];

        

        self.layer.cornerRadius = 5;

        self.layer.borderWidth = 2;

        self.layer.borderColor = [UIColor redColor].CGColor;

        // 设置阴影颜色

        self.layer.shadowColor = [UIColor blackColor].CGColor;

        // 设置引用的透明度

        self.layer.shadowOpacity = 0.7;

        // 设置阴影的偏移量

        self.layer.shadowOffset = CGSizeMake(20, 20);

        

        _textView = [[UITextView alloc] initWithFrame:CGRectMake((kSceenWidth - 280)/ 2, 10, 280, 200)];

        [_textView becomeFirstResponder];

        [self addSubview:_textView];

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake((kSceenWidth - 100) / 2, kSceenHeight - 30, 100, 30);

        [btn setTitle:@"sender" forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];

    }

    return self;

}

- (void)btnAction:(UIButton *)btn

{

    if (_block != nil) {

        _block(_textView.text);

    }

    [self removeFromSuperview];

}

- (void)show

{

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

       //设置动画

    self.transform = CGAffineTransformMakeScale(.7, .7);

    [UIView animateWithDuration:.5

                     animations:^{

                         self.transform = CGAffineTransformMakeScale(1.1, 1.1);

                     } completion:^(BOOL finished) {

                         if (finished) {

                             [UIView animateWithDuration:.5 animations:^{

                                 self.transform = CGAffineTransformIdentity;

                             }];

                         }

                     }];

    

    [window addSubview:self];

}

@end

 

block 传值的小程序

标签:

原文地址:http://www.cnblogs.com/zhangxi1017/p/4383773.html

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