隱藏鍵盤的幾種方法和手勢練習

//註冊事件和手勢

- (void)viewDidLoad

{

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShowNotify:) name:UIKeyboardDidShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHideNotify:) name:UIKeyboardWillHideNotification object:nil];

    myTextFeild.delegate = self;


    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doDoubleTap)];

    //雙擊,寫爲1變爲單擊

    doubleTap.numberOfTapsRequired = 2;

    doubleTap.numberOfTouchesRequired = 1;

    [myScrollView addGestureRecognizer:doubleTap];


}

 

//打開軟鍵盤時調用

- (void)keyboardShowNotify:(NSNotification *)notify {


    myScrollView.center = CGPointMake(myScrollView.center.x, myScrollView.center.y/2);

}


//關閉軟鍵盤時調用

- (void)keyboardHideNotify:(NSNotification *)notify {

    myScrollView.center = CGPointMake(myScrollView.center.x, myScrollView.center.y*2);

}



//點擊鍵盤return鍵的時候調用
- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

//雙擊scrollView的時候調用

- (void)doDoubleTap

{

    [myTextFeild resignFirstResponder];

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章