我正在尝试使用点击手势识别器来隐藏键盘和下拉表格视图(在另一个视图中以编程方式创建,并在需要时调用)。我在ViewDidLoad中使用的代码是
override func viewDidLoad() {
super.viewDidLoad()
tap = UITapGestureRecognizer(target: self, action:Selector("DismissKeyboard"))
view.addGestureRecognizer(tap!) }
和DismissKeyboard功能
func DismissKeyboard(){
view.endEditing(true)
subviewSchool.removeFromSuperview()
subviewPosition.removeFromSuperview()
}
调用Dropdown Table View的按钮操作是
@IBAction func dropDownPosition(sender: AnyObject) {
var frameForDropDownViewPosition = CGRect()
var framePosition = selectPositionTextField.frame
frameForDropDownViewPosition.origin.x = framePosition.origin.x
frameForDropDownViewPosition.origin.y = studentCell.frame.origin.y + framePosition.origin.y + framePosition.size.height
frameForDropDownViewPosition.size.width = framePosition.size.width
frameForDropDownViewPosition.size.height = 300
subviewPosition = DropDownView(frame: frameForDropDownViewPosition)
subviewPosition.delegate = self
subviewPosition.indicator = "positionStudent"
subviewPosition.checkposition = schoolKeyId
subviewPosition.schoolInfoArr = schoolInfoArr
self.view.addSubview(subviewPosition)
}
但问题是Tab Gesture确实有效,但是我无法选择下拉视图的包含(想要在被调用时执行某些任务,在索引路径上选择行),因为点击手势不允许我这样做。 如何从下拉表视图中删除Tab Gesture(或者有其他方法吗?),因为我可以使用
从所有视图中删除Tab Gestureself.view.removeGestureRecognizer(tap!)
但不是来自特定的观点(不是计划),所以我可以按照自己的意愿做我的工作。我正在使用Swift
谢谢
答案 0 :(得分:2)
添加gestureDelegate:
UIGestureRecognizerDelegate
在ViewDidLoad中设置点按委托:
tap.delegate = self
然后调用此委托
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
let p = touch.locationInView(view)
if CGRectContainsPoint(DropDownView.frame, p) {
return false
}
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let p = touch.location(in: view)
if DropDownView.frame.contains(p) {
return false
}
return true
}
答案 1 :(得分:0)
如果您使用textFiled尝试使用textFiledDelegate方法来解除键盘
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
答案 2 :(得分:0)
如果要识别多个视图的点按手势。您需要为多个视图添加Tap Gesture SELECTOR。
请检查以下代码,希望它适合您。
- (void)viewDidLoad {
[super viewDidLoad];
//Added Tap Gesture to remove keyboard
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processSingleTap:)];
[singleTapGesture setNumberOfTapsRequired:1];
[singleTapGesture setNumberOfTouchesRequired:1];
[self.tableViewObj addGestureRecognizer:singleTapGesture];
}
-(void)dismissKeyboard
{
[[[UIAlertView alloc]initWithTitle:@"Keyboard" message:@"Dismiss keyboard here..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
-(void)processSingleTap:(UITapGestureRecognizer*)gesture
{
CGPoint pointInTableView = [gesture locationInView:self.tableViewObj];
NSIndexPath *selectedIndexPath = [self.tableViewObj indexPathForRowAtPoint:pointInTableView];
UITableViewCell *selectedCell = (UITableViewCell*)[self.tableViewObj cellForRowAtIndexPath:selectedIndexPath];
if(selectedCell){
[[[UIAlertView alloc]initWithTitle:@"Cell Selected" message:[NSString stringWithFormat:@"Cell Selected Index...%@",@(selectedIndexPath.row)] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else
{
NSLog(@"Cell not selected tap of table view ...");
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"Cell %@",@(indexPath.row)];
return cell;
}
答案 3 :(得分:0)
您可以检查它是否在UITableView中被点击。
看看这个答案How get UITableView IndexPath from UITableView iphone?。这是一个更好,更简单的方法。
答案 4 :(得分:0)
将TapGestureRecognizer
拖放到您想要的ViewController
中的Main.Storyboard
然后转到文档插座,选择TapGestureRecognizer
,按住它拖动它以在IBOutlet
ViewController.swift
为TapGestureRecognizer
创建一个插座,将其命名为" tap
"
@IBOutlet var tap: UITapGestureRecognizer!
将UIGestureRecognizerDelegate
添加到ViewController
UIGestureRecognizerDelegate
在viewDidLoad()
或您感兴趣的@IBAction
tap.delegate = self
然后调用此委托函数
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
let p = touch.locationInView(view)
if CGRectContainsPoint(DropDownView.frame, p) {
return false
}
return true
}