NSNotificationCenter+ UITapGestureRecognizer

这里涉及比较多,包括手势,通知中心等,具体上代码
#import "RegistViewController.h"
#import "DetailViewController.h"
@interface RegistViewController ()<UITextFieldDelegate>
- (IBAction)goDetail:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *goDetailButton;

@end

@implementation RegistViewController{
    CGRect buttonOldRect;
    UITapGestureRecognizer *tapGR;//添加单击手势
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    
    /*
     数据传递
     1、首先,数据已经存在了!
     2、谁需要,就声明几个指针。
     3、在实例化2那个对象的时候,将声明的几个指针指向正确是数据。
     4、至此,数据传递完成,可以使用了。
     
     */
    
    //将自己加入通知中心
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradShow:) name:UIKeyboardWillShowNotification object:Nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradHide:) name:UIKeyboardWillHideNotification object:Nil];
    buttonOldRect=self.goDetailButton.frame;
    
    //实例化单击手势
    tapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
    
    self.nameTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
    self.passwordTextField.secureTextEntry=YES;
    
    self.nameTextField.delegate=self;
    [self.nameTextField becomeFirstResponder];//成为第一响应者,处于可编辑状态

}
#pragma mark--
#pragma mark 自定义函数
//单击手势的响应函数
-(void)onTap:(id)sender{
    //将输入框取消第一响应者,键盘就睡自动收回
    [self.nameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}
//收到键盘将要弹起的通知后,调用的函数
-(void)onKeyBoradShow:(id)sender{
    [self.view addGestureRecognizer:tapGR];
//    NSLog(@"%@",sender);
    //将sender转化成notice类型
    NSNotification *notice=sender;
    //获取通知里面的userinfo
    NSDictionary *userInfo = notice.userInfo;
    //在userinfo中获取键盘最后的状态rect
    id keyBoardRectNotice = userInfo[UIKeyboardFrameEndUserInfoKey];
    //将上面获得的rect对象,提取出cgrect
    CGRect keyBoardRect;
    [keyBoardRectNotice getValue:&keyBoardRect];
    //用获得的cgrect来设置button的frame;
    CGRect buttonNewFrame=buttonOldRect;
    //原来的按键frame减去键盘的高度,生成新的frame
    buttonNewFrame.origin.y-=keyBoardRect.size.height;
    [UIView animateWithDuration:0.2 animations:^{
        self.goDetailButton.frame=buttonNewFrame;
    }];
    
    
}
//收到键盘将要收回的通知后,调用的函数
-(void)onKeyBoradHide:(id)sender{
    [self.view removeGestureRecognizer:tapGR];
    [UIView animateWithDuration:0.3 animations:^{
        self.goDetailButton.frame=buttonOldRect;
    }];
}
//按键函数
- (IBAction)goDetail:(id)sender {
    NSString *name=self.nameTextField.text;
    NSString *password=self.passwordTextField.text;
    
    //生成数据模型
    UserModel *model=[[UserModel alloc]init];
    model.name=name;
    model.pass=password;
    
    DetailViewController *detailVC=[[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:Nil];
    //将数据模型传入
    detailVC.model=model;
    
    [self presentViewController:detailVC animated:YES completion:^{
        
    }];
    
}
#pragma mark--
#pragma mark textfiled代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"textFiled 是否可以开始编辑");
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textFiled 开始编辑");
}// became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"textFiled 是否可以结束编辑");
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"textFiled 结束编辑");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"textFiled 是否可以改变某个区域字符串");
    NSLog(@"%d-%d,%@",range.location,range.length,string);
    if (textField==self.nameTextField) {
        if (range.location>=8) {
            return NO;
        }else{
            if (textField.text.length>=8) {
                if (range.length==0) {
                    return NO;
                }else{
                    return YES;
                }
            }
        }
    }
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    NSLog(@"textFiled 是否清空");
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"textFiled 键盘的return键被点击");
    [textField resignFirstResponder];
    NSLog(@"开始搜索!!!");
    return YES;
}
//销毁函数,用于移除通知
-(void)dealloc{
    //在销毁的时候将自己在通知中心中移除
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

posted on 2014-05-23 15:44  超级庄子  阅读(211)  评论(0编辑  收藏  举报

导航