简体   繁体   English

Tab手势识别器可提供特定视图

[英]Tab Gesture Recognizer for specific view

I am trying to use tap gesture recognizer to hide keyboard and drop down table view (which is created programatically in another view and is called when needed). 我正在尝试使用轻击手势识别器来隐藏键盘和下拉表视图(该视图是在另一个视图中以编程方式创建的,并在需要时调用)。 The code I used in ViewDidLoad is 我在ViewDidLoad中使用的代码是

override func viewDidLoad() {
        super.viewDidLoad()

        tap = UITapGestureRecognizer(target: self, action:Selector("DismissKeyboard"))

        view.addGestureRecognizer(tap!) }

and DismissKeyboard function is 并且DismissKeyboard函数是

func DismissKeyboard(){       
        view.endEditing(true)
        subviewSchool.removeFromSuperview()
        subviewPosition.removeFromSuperview()
    }

Button action to call Dropdown Table View is 调用下拉列表视图的按钮动作是

@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)
    }

But the problem is that Tab Gesture did work but I am Unable to Select the contain of Drop Down view (want to perform certain task when called did select row at index path) as tap gesture is not allowing me to do so. 但是问题是Tab手势确实起作用了,但是我无法选择下拉视图的包含项(当调用在索引路径中选择行时希望执行某些任务),因为点击手势不允许我这样做。 How can I remove Tab Gesture from Drop Down Table View (or is there an alternative way?), as I can remove Tab Gesture from all view using 我如何从下拉表视图中删除“选项卡手势”(或者有其他方法?),因为我可以使用以下方法从所有视图中删除“选项卡手势”:

self.view.removeGestureRecognizer(tap!)

but not from specific view (which is not as plan), so that I can do my work as I desire. 但不是从特定角度来看(并非按计划),这样我就可以按自己的意愿去做。 I am using Swift 我正在使用Swift

Thank you 谢谢

Add gestureDelegate: 添加gestureDelegate:

 UIGestureRecognizerDelegate

In ViewDidLoad set tap delegate: 在ViewDidLoad中设置水龙头代表:

tap.delegate = self

Then call this delegate 然后致电此代表

Swift 2 迅捷2

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    let p = touch.locationInView(view)
    if CGRectContainsPoint(DropDownView.frame, p) {
      return false
    }
    return true
  }

Swift 4 斯威夫特4

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    let p = touch.location(in: view)
    if DropDownView.frame.contains(p) {
        return false
    }
    return true
}

if you are using the textFiled try by using textFiledDelegate Method for dismissing keyboard 如果您使用的是textFiled,请尝试使用textFiledDelegate方法关闭键盘

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
} 

If you want to recognise Tap gesture for multiple view. 如果要识别点击手势以查看多个视图。 You need to add Tap Gesture SELECTOR for multiple views. 您需要为多个视图添加Tap Gesture SELECTOR。

Please check below code hope it works for you. 请检查以下代码,希望它对您有用。

- (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;
}

You can check if it was tapped inside UITableView. 您可以检查它是否在UITableView中被窃听。

Just have a look at this answer How get UITableView IndexPath from UITableView iphone? 看看这个答案。 如何从UITableView iPhone获得UITableView IndexPath? . It's a much better and a simple way. 这是一种更好,更简单的方法。

Drag and drop, the TapGestureRecognizer on to the ViewController in Main.Storyboard you want this then go to documents outlet, select the TapGestureRecognizer , Control drag it to create IBOutlet in the ViewController.swift 拖放,在TapGestureRecognizerViewControllerMain.Storyboard你想这个然后去文件出口,选择TapGestureRecognizer ,控制拖动它来创建IBOutletViewController.swift

create an outlet for TapGestureRecognizer , name it " tap " TapGestureRecognizer创建一个插座,将其命名为“ tap

@IBOutlet var tap: UITapGestureRecognizer!

add UIGestureRecognizerDelegate to the ViewController UIGestureRecognizerDelegate添加到ViewController

UIGestureRecognizerDelegate

in viewDidLoad() or in the @IBAction of your interest viewDidLoad()或您感兴趣的@IBAction

tap.delegate = self

Then call this delegate function 然后调用此委托函数

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    let p = touch.locationInView(view)
    if CGRectContainsPoint(DropDownView.frame, p) {
      return false
    }
    return true
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM