ios - TapGesture 添加到 UIView+SomeCategory。我如何使用这个添加到我的 UIViewController 的 UIView?

标签 ios objective-c uiview uiviewcontroller objective-c-category

我有一个自定义的 UIView。我在这个 UIView 中添加了一个 tapGestureRecognizer。不是通过 UIViewController,而是在 UIView+Category 类中。我正在尝试在点击此 UIView 时使用 presentViewController() 。这是一个 UIView 类,它不允许 presentViewController()。我知道只有 UIViewController 允许调用此函数。那么我是否错误地构建了我的项目?如果我只是将 tapGestureRecognizer 添加到此 UIView 而不是使用类别而是直接从 UIViewController 中添加,会更好吗?这个 UIView 也将在其他地方使用,所以如果我能做到这一点,它将为我节省大量代码。提前致谢。

最佳答案

有两种直接的方法可以做到这一点 1) 通知(提供的示例是用 ObjC 编写的) 2) 委托(delegate)(提供的示例是用 Swift 编写的)

1)使用NSNotificationCenter通知UIViewController View 属于

在您的 UIView+Category 中定义了手势识别处理程序,发布通知并在您的 UIViewController 中添加一个观察者

在 UIViewController 中

// Define it at the top of your view controller
#define kViewDidClick @"ViewDidClick"

 - (void)viewDidLoad {
     ...

     [[NSNotificationCenter defaultCenter]       
             addObserver: self 
                selector: @selector(presentVC:) 
                    name: kViewDidClick 
                  object: nil]; 
}

- (void)presentVC: (NSNotification *)notification {
    [self.presentViewController: vc animated: false completion: nil];
}

在你处理手势的 UIView+Category 中(现在我假设它叫做 handleGesture)

-(void) handleGesture: (UIGestureRecognizer *)gestureRecognizer {
    [[NSNotificationCenter defaultCenter] postNotificationName: kViewDidClick object: nil];
}

2) 您需要定义一个处理手势的类和一个协议(protocol)。这样您就可以轻松处理所有手势,而无需在每次要处理手势时都在 UIView 或 UIViewController 中定义一些方法来处理手势

GestureHandlerHelper.swift

import UIKit

/**
Handle gesture recognized by a UIView instance
*/

class GestureHandlerHelper {
var delegate: SelectionDelegate?

// MARK: Initializer
init(delegate: SelectionDelegate) {
    self.delegate = delegate
}

// MARK: Gesture Handler

/**
Handle gesture recognized by a UIView instance
*/
@objc func handleGesture(gestureRecongnizer: UIGestureRecognizer) {
    println(self)
    println(__FUNCTION__)
    println(gestureRecongnizer)

    if .Ended == gestureRecongnizer.state {
        if nil != delegate? {
            delegate?.didClick(gestureRecongnizer)
        }
    }
  }
}

SelectionDelegate.swift

/**
 Define a general rule for classes that handle gesture on views
 */
protocol SelectionDelegate {
    func didClick(gestureRecognizer: UIGestureRecognizer)
}

在你的 UIViewController 中

class ViewController : UIViewController {
  ... 
  override func loadView() {
        self.view = View(frame: UIScreen.mainScreen().bounds,
          gestureHandlerHelper: GestureHandlerHelper(delegate: self))
  }
  ... 
}

extension ViewController : SelectionDelegate {

    func didClick(gestureRecognizer: UIGestureRecognizer) {
        self.presentViewController(SomeViewController(), animated: false, completion: nil)
     }
}

在你的 UIView 中

init(frame: CGRect, gestureHandlerHelper: GestureHandlerHelper) {
      ... 
    var tapGestureRecognizer = UITapGestureRecognizer(target: gestureHandlerHelper, action: "handleGesture:")
      ...
}

关于ios - TapGesture 添加到 UIView+SomeCategory。我如何使用这个添加到我的 UIViewController 的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998920/

相关文章:

iphone - iOS键盘点击用户偏好

iphone - iOS XMPP群聊实现

ios - 使用 Xcode/Objective C 订阅 FCM 主题时出现问题

ios - 从 superView 的 ViewController 之外的类调用时 willRemoveSubview 不会删除 subview ?

ios - 在 View 中来回切换

ios - 无法快速符合 objective-c 协议(protocol)

ios - iPad 导航 Controller 工具栏位置

objective-c - 使 NSWindow 的内容变灰

ios - UICollectionView 的 didSelectItemAtIndexPath 方法只被调用一次

ios - 无法在 Swift 中设置作为 UIScrollView subview 的 UIView 的背景颜色