注釋:UITextFiled才有輸入框,是以先建立一個UITextFiled對象,點選他會出現輸入框,利用UITapGestureRescognizer 類實作操作,上代碼:
.h檔案中不用操作
//
// LYXViewController.h
// UITap
//
// Created by liyongxing on 13-7-6.
// Copyright (c) 2013年 liyongxing. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LYXViewController : UIViewController
@end
在.m中建立對象
//
// LYXViewController.m
// UITap
//
// Created by liyongxing on 13-7-6.
// Copyright (c) 2013年 liyongxing. All rights reserved.
//
#import "LYXViewController.h"
@interface LYXViewController ()
@property (nonatomic,strong) UITextField * text;
@end
@implementation LYXViewController
-(void)viewDidLoad
{
[super viewDidLoad];
//建立UITextField的對象,并給他定位制定大小,位置
self.text = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 60)];
//設定背景色,預設為透明色
self.text.backgroundColor = [UIColor grayColor];
//将對象添加到視圖上
[self.view addSubview:self.text];
//調用點選背景方法
[self tapGesture];
}
-(void)tapGesture
{
//建立一個點選對象,并将其關聯一個點選方法
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tapResign)];
//設定點選多少次退出鍵盤,一般設定為1次
tapGestureRecognizer.numberOfTapsRequired = 1.0;
//将點選對象添加到目前視圖
[self.view addGestureRecognizer:tapGestureRecognizer];
//是否取消點選背景視圖的動作,設定為否
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
//點選背景視圖的時候發生的時間,退出第一響應者,到此就能完成我們想要的效果
-(void)tapResign
{
[self.text resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
轉自:http://blog.csdn.net/u011199592/article/details/9260613