如何在swift

时间:2017-04-19 19:59:24

标签: swift

我有以下代码:

extension ViewController {


func AddLeftGesture(){
    let SwipeLeft:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyDismissOnSwipeLeft))

    self.view.addGestureRecognizer(SwipeLeft)
}

func MyDismissOnSwipeLeft(){

    self.dismiss(animated: true, completion: nil)
}

我想要完成的是覆盖viewDidLoad和 调用AddLeftGesture方法,使其成为我制作的每个VC的一部分 而且我不必在每个viewDidLoad中再次输入它,

这可能吗?或者你们还有其他建议吗?

3 个答案:

答案 0 :(得分:1)

我认为这不是一个好主意,因为通常mouse.accepted用于设置大多数属性,如果您想在视图控制器中覆盖它,则应该再次编写它。我可以建议创建一个基础viewDidLoad并在其ViewController中添加此代码,然后从基本视图控制器继承每个viewController,这样每当您想要更改任何您刚刚调用的内容时viewDidLoad

super.viewDidLoad

答案 1 :(得分:0)

AFAIK有两种选择。您可以将UIViewController子类化,然后使所有控制器继承自子类控制器,或者您可以调动UIViewController viewDidLoad()

我个人会选择调配,虽然它有一个缺点 - 它隐藏了实现,并且可能会让一个新的开发人员进入项目时感到困惑。因此,请确保在项目自述文件和代码中的某处正确记录。

现在有一些代码示例:

子类化UIViewController

MyViewController.swift

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        addGesture()
    }

    func addGesture() {
        // Do what you need
    }
}

class OtherViewController: MyViewController {
    // Automatically will add gesture because it's a subclass of  MyViewController
}

Swizzling viewDidLoad

调配的方法是,它交换方法的实现。这只是意味着函数的名称指向来自不同函数的代码。有关此主题的更多信息read this article

UIViewController+Swizzle.swift

static func swizzle(selector originalSelector: Selector,
                    with newSelector: Selector,
                    on targetClass: AnyClass) {
    let originalMethod = class_getInstanceMethod(targetClass, originalSelector)
    let swizzledMethod = class_getInstanceMethod(targetClass, newSelector)

    // If we were able to add the swizzled function, replace methods.
    // Otherwise exchange implementations if method already exists.
    if class_addMethod(targetClass, originalSelector,
                       method_getImplementation(swizzledMethod),
                       method_getTypeEncoding(swizzledMethod)) {

        class_replaceMethod(targetClass, newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod))
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

extension UIViewController {

    // This function is getting called automatically by the runtime, 
    // when this class is loaded to perform some additional intiialization. 
    // However, this has now been deprecated in Swift, so only option is to 
    // declare a static function which you need to remember to call from 
    // somewhere, preferably early in your app initialization, like your 
    // didFinishLaunching function in AppDelegate or even AppDelegate's init 
    // function. I kept the initialize function in the code as a reference, 
    // however you would probably want to write it like in the comment 
    // below, to silence the warning.
    //
    // class func swizzle()
    //
    open override class func initialize() {
        if self != UIViewController.self { return }

        let swizzlingClosure: () = {
            swizzle(selector: #selector(UIViewController.viewDidLoad),
                             with: #selector(UIViewController.swizzled_viewDidLoad),
                             on: UIViewController.self)
        }()

        swizzlingClosure
    }

    @objc private func swizzled_viewDidLoad() {
        // Calls the original implementation,
        // because implementations are switched.
        swizzled_viewWillAppear(animated)

        // Do whatever you need
        addGesture()
    }

    @objc func addGesture() {
        // Add your gesture
    }
}

答案 2 :(得分:0)

创建继承UITapGestureRecognizer

的此类
open class BlockTap: UITapGestureRecognizer {
    fileprivate var tapAction: ((UITapGestureRecognizer) -> Void)?

public override init(target: Any?, action: Selector?) {
    super.init(target: target, action: action)
}

public convenience init (
    tapCount: Int = 1,
    fingerCount: Int = 1,
    action: ((UITapGestureRecognizer) -> Void)?) {
        self.init()
        self.numberOfTapsRequired = tapCount

        #if os(iOS)
        self.numberOfTouchesRequired = fingerCount
        #endif

        self.tapAction = action
        self.addTarget(self, action: #selector(BlockTap.didTap(_:)))
}

open func didTap (_ tap: UITapGestureRecognizer) {
    tapAction? (tap)
}
}

然后对UIView进行扩展

extension UIView {
        public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) -> ())?) {
            let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action)
            addGestureRecognizer(tap)
            isUserInteractionEnabled = true
        }
    }

然后您可以将其用作

override func viewDidLoad() {
        super.viewDidLoad()
         self.view.addTapGesture(action: {[unowned self] (_) in
                //Do whatever on click of View
         })
}

希望它有所帮助!