如何为UIView扩展方法swift添加结构

如何为UIView扩展方法swift添加结构,swift,uiview,uiviewcontroller,swipe,Swift,Uiview,Uiviewcontroller,Swipe,您好,我想为UIViewControllers顶部的UIView提供panarchitecture滑动操作。对于该操作,我在视图控制器扩展中编写了一个常用方法,但它使整个viewcontroller都是滑动的 我只想对UIView执行刷卡操作,请帮助我将以下代码更改为UIView扩展名 extension UIViewController{ @objc func pangectureRecognizerDismissoneScreen(_ sender: UIPanGestureRecogn

您好,我想为
UIViewControllers
顶部的
UIView
提供
panarchitecture
滑动操作。对于该操作,我在视图控制器扩展中编写了一个常用方法,但它使整个
viewcontroller
都是滑动的

我只想对UIView执行刷卡操作,请帮助我将以下代码更改为
UIView扩展名

extension UIViewController{

@objc  func pangectureRecognizerDismissoneScreen(_ sender: UIPanGestureRecognizer){  
        var initialTouchPoint: CGPoint = CGPoint(x: 0,y: 0)
        let touchPoint = sender.location(in: self.view?.window)

        if sender.state == UIGestureRecognizerState.began {
            initialTouchPoint = touchPoint
        } else if sender.state == UIGestureRecognizerState.changed {
            if touchPoint.y - initialTouchPoint.y > 0 {
                self.view.frame = CGRect(x: 0, y: touchPoint.y - initialTouchPoint.y, width: self.view.frame.size.width, height: self.view.frame.size.height)
            }
        } else if sender.state == UIGestureRecognizerState.ended || sender.state == UIGestureRecognizerState.cancelled {
            if touchPoint.y - initialTouchPoint.y > 100 {
                self.view.backgroundColor = UIColor.clear
                self.dismiss(animated: true, completion: nil)
            } else {
                UIView.animate(withDuration: 0.3, animations: {
                    self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
                })
            }
        }
    }
}

使用这个类扩展

 //Gesture extention

extension UIGestureRecognizer {
    @discardableResult convenience init(addToView targetView: UIView,
                                        closure: @escaping () -> Void) {
        self.init()

        GestureTarget.add(gesture: self,
                          closure: closure,
                          toView: targetView)
    }
}

fileprivate class GestureTarget: UIView {
    class ClosureContainer {
        weak var gesture: UIGestureRecognizer?
        let closure: (() -> Void)

        init(closure: @escaping () -> Void) {
            self.closure = closure
        }
    }

    var containers = [ClosureContainer]()

    convenience init() {
        self.init(frame: .zero)
        isHidden = true
    }

    class func add(gesture: UIGestureRecognizer, closure: @escaping () -> Void,
                   toView targetView: UIView) {
        let target: GestureTarget
        if let existingTarget = existingTarget(inTargetView: targetView) {
            target = existingTarget
        } else {
            target = GestureTarget()
            targetView.addSubview(target)
        }
        let container = ClosureContainer(closure: closure)
        container.gesture = gesture
        target.containers.append(container)

        gesture.addTarget(target, action: #selector(GestureTarget.target(gesture:)))
        targetView.addGestureRecognizer(gesture)
    }

    class func existingTarget(inTargetView targetView: UIView) -> GestureTarget? {
        for subview in targetView.subviews {
            if let target = subview as? GestureTarget {
                return target
            }
        }
        return nil
    }

    func cleanUpContainers() {
        containers = containers.filter({ $0.gesture != nil })
    }

    @objc func target(gesture: UIGestureRecognizer) {
        cleanUpContainers()

        for container in containers {
            guard let containerGesture = container.gesture else {
                continue
            }

            if gesture === containerGesture {
                container.closure()
            }
        }
    }
} 
编辑1:-

像这样使用

 UIGestureRecognizer.init(addToView: yourView, closure: {
                  //  your code 
                })

您可以添加此UIView扩展

import UIKit

import RxSwift

struct AssociatedKeys {
static var actionState: UInt8 = 0
}

typealias ActionTap = () -> Void

extension UIView {

var addAction: ActionTap? {
    get {
        guard let value = objc_getAssociatedObject(self, &AssociatedKeys.actionState) as? ActionTap else {
            return nil
        }
        return value
    }
    set {
        objc_setAssociatedObject(self, &AssociatedKeys.actionState, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        self.tapWithAnimation()
    }
}

func tapWithAnimation() {
    self.gestureRecognizers?.removeAll()
    let longTap = UILongPressGestureRecognizer(target: self, action: #selector(viewLongTap(_:)))
    longTap.minimumPressDuration = 0.035
    longTap.delaysTouchesBegan = true
    self.addGestureRecognizer(longTap)
}

@objc
func viewLongTap(_ gesture: UILongPressGestureRecognizer) {
    if gesture.state != .ended {
        animateView(alpha: 0.3)
        return
    } else if gesture.state == .ended {
        let touchLocation = gesture.location(in: self)
        if self.bounds.contains(touchLocation) {
            animateView(alpha: 1)
            addAction?()
            return
        }
    }
    animateView(alpha: 1)
}

fileprivate func animateView(alpha: CGFloat) {
    UIView.transition(with: self, duration: 0.3,
                      options: .transitionCrossDissolve, animations: {
                        self.subviews.forEach { subView in
                            subView.alpha = alpha
                        }
    })
}
}
示例:

myView.addAction = {
print("click")
}

我们如何使用这个one@srikanthkumar看一看,我使用下面的方法,但它没有得到任何操作let get=UIPanGestureRecognizer(目标:self,操作:#选择器(PangArchitectureRecognizerDismissOneScreen(:))UIGestureRecognizer.init(addToView:uiview){self.view.backgroundColor=UIColor.darkGray.withAlphaComponent(0.1)self.uiview.addgestureRecognitizer(get)}您在我的代码中看到,无论您将传递到“addToView”的视图是什么,它都将在完成(关闭)时执行手势识别器您可以执行任何操作,如果您仍有疑问,请检查此链接@srikanthkumarwhat isRxSwift@srikanthkumar忽略这个导入,我不会在这个扩展中使用这个导入。RxSwift是一个pod~>Ok,但是你的代码不会向下滑动并关闭视图。