【问题标题】:Tab Gesture Recognizer for specific view特定视图的选项卡手势识别器
【发布时间】:2015-10-13 05:29:21
【问题描述】:

我正在尝试使用轻击手势识别器来隐藏键盘和下拉表格视图(在另一个视图中以编程方式创建并在需要时调用)。我在 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()
    }

调用下拉表视图的按钮操作是

@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 Gesture
self.view.removeGestureRecognizer(tap!)

但不是从特定的角度来看(这不是计划),这样我就可以按照我的意愿去做我的工作。我正在使用 Swift

谢谢

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    添加gestureDelegate:

     UIGestureRecognizerDelegate
    

    在 ViewDidLoad 中设置点击委托:

    tap.delegate = self
    

    然后调用这个委托

    斯威夫特 2

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

    斯威夫特 4

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

    【讨论】:

      【解决方案2】:

      如果你正在使用 textFiled 尝试使用 textFiledDelegate 方法来关闭键盘

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

      【讨论】:

      • 感谢您的建议,但我不打算将其仅用于文本字段,因为它不适用于下拉视图,这是我的主要关注点,应该像键盘一样隐藏跨度>
      【解决方案3】:

      如果您想识别多视图的点击手势。您需要为多个视图添加 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;
      }
      

      【讨论】:

        【解决方案4】:

        您可以检查它是否在 UITableView 中被点击。

        看看这个答案How get UITableView IndexPath from UITableView iphone?。这是一种更好更简单的方法。

        【讨论】:

          【解决方案5】:

          TapGestureRecognizer 拖放到Main.Storyboard 中的ViewController 你想要这个 然后到文件出口,选择TapGestureRecognizer,控制拖动它在ViewController.swift中创建IBOutlet

          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
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-10
            • 1970-01-01
            • 2013-09-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多