简体   繁体   English

响应者在UISearchBar上平移手势

[英]Pan gesture on UISearchBar while first responder

I have a scene that is very similar to the iOS Maps app - some content and a table view with a search bar at the top that you can pull up over the content. 我有一个非常类似于iOS Maps应用程序的场景-一些内容和一个表格视图,顶部带有一个搜索栏,您可以将其拉到内容上。 What I want is for the user to be able to pan starting on the search bar, regardless of whether it is active or not. 我要的是使用户能够在搜索栏上开始平移,而不管它是否处于活动状态。

On its own, this work, as long as the pan does not start inside the text field of the search bar. 只要平移未在搜索栏的文本字段内开始,这项工作就可以单独进行。 To overcome this, I put a dummy UIView mask with a gesture recogniser on top of the search bar, similar to this . 为了克服这个问题,我在搜索栏顶部放置了一个带有手势识别器的虚拟UIView遮罩,类似于this However, as soon as I tap and, thus, the searchBar becomes first responder, the mask goes to the background and does not accept touches (note that I do not set isHidden to false on the mask). 但是,一旦我点击并因此searchBar成为第一响应者,遮罩就会转到背景并且不接受触摸(请注意,我没有在遮罩isHidden设置为false )。 The only way to go back to panning is to searchBarContainerView.bringSubview(toFront: theMask) . 返回平移的唯一方法是searchBarContainerView.bringSubview(toFront: theMask) The problem is that unless the searchBar has resigned first responder, this has no effect. 问题在于,除非searchBar辞职了第一响应者,否则这无效。 I tried subclassing a UIView for the container and changed layoutSubviews to: 我尝试将UIView子类化为容器,并将layoutSubviews更改为:

super.layoutSubviews()
if let searchBarMask = self.searchBarMask {
   bringSubview(toFront: searchBarMask)
}

but without effect. 但没有效果。 Any suggestions? 有什么建议么? I am willing to depart from the UIView-mask pattern if necessary. 如果有必要,我愿意离开UIView-mask模式。

The hierarchy is something like: 层次结构类似于:

  • UIView UIView

    • ScrollView (the content) ScrollView(内容)
    • ContainerView (the pull up thingy) ContainerView(上拉东西)
  • UIView (from the Container View above, in a separate UIViewController, name it XController) UIView(在上面的Container View中,在单独的UIViewController中,将其命名为XController)

    • UIView - search placeholder UIView-搜索占位符
    • UITableView UITableView

The search bar is added in the XController as: 搜索栏在XController中添加为:

override func viewDidLoad() {
    self.controller = UISearchController(nil)
    self.searchPlaceholder.addSubview(self.controller.searchBar)
    ...
    self.mask = UIView(frame: self.controller.searchBar.frame)
    self.searchPlaceholder.insertSubview(self.mask, aboveSubview: self.controller.searchBar)
}

I ended up doing something similar, which is as follows. 我最终做了类似的事情,如下。 I put a UIView as a mask, but this time it was directly in the search bar view: 我将UIView用作遮罩,但这一次它直接在搜索栏视图中:

self.searchBar.addSubview(theMask)
self.searchBar.bringSubview(toFront: theMask)
// add pan gesture recogniser on theMask

I don't have any hide/unhide logic and it works great. 我没有任何隐藏/取消隐藏逻辑,并且效果很好。 There are some issues, for example you cannot pan starting on the Cancel button and tapping on the search field is totally ignored, but I think this has most to do with correctly configuring gesture recognising. 存在一些问题,例如,您无法在“取消”按钮上开始平移,而完全不理会在搜索字段上的点击,但是我认为这与正确配置手势识别有关。

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

相关问题 通过使用SWRevealViewController的平移手势识别器,UITextField退出了第一响应者 - UITextField resign first responder by using pan gesture recognizer for SWRevealViewController UIPopoverController中的UISearchBar没有得到第一响应者 - UISearchBar in UIPopoverController not getting first responder iOS 7 UISearchBar是第一个响应者时的自定义 - iOS 7 UISearchBar customization when it is first responder 当UISearchBar不再是第一响应者时,是否有委托通知您? - Does UISearchBar have a delegate that notifies you when it is no longer the first responder? 尝试选择UICollectionViewCell时,UISearchBar退出第一响应者 - UISearchBar resigns first responder when trying to select UICollectionViewCell 嵌入在导航栏中的UISearchController的UISearchBar:不成为第一响应者 - UISearchController's UISearchBar embedded in Navigation Bar: Does not become first responder 为什么我的UISearchBar在成为第一响应者之后的宽度为0? - Why is my UISearchBar's width 0 after becoming first responder? 为什么UISearchBar即使在被设置为第一响应者之后也没有获得焦点? - Why UISearchBar does not get Focus even after it is set as a first responder? 进行平移手势时更改选择器 - Change selector while a pan gesture is going on 自定义UIView调整大小,同时识别平移手势 - Custom UIView resizing while recognizing pan gesture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM