-2
class menuView
{
let View = UIView()
let resignView = UIView()
let tap = UITapGestureRecognizer()

    func makeView(view:UIView){
        makeResignView(view: view)
        view.addSubview(View)
        View.translatesAutoresizingMaskIntoConstraints = false
        View.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        View.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        View.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        View.widthAnchor.constraint(equalToConstant: view.frame.width - 100).isActive = true
        View.backgroundColor = UIColor.cyan

    }
    func makeResignView(view:UIView){
         print("resing view is activate")
         resignView.frame = view.frame
         view.addSubview(resignView)
         resignView.backgroundColor = UIColor.blue
         resignView.isUserInteractionEnabled = true
         let tap = UITapGestureRecognizer(target: self, action: #selector(handleDismiss(recog:)))
         resignView.addGestureRecognizer(tap)
   }
    @objc func handleDismiss(recog:UITapGestureRecognizer){
        print("rsing view is dismiss")
        View.removeFromSuperview()
     }
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.gray
}
@IBAction func PlaceView(_ sender: Any) {
    let NewView = menuView()
    NewView.resignView.frame = view.frame
    NewView.makeResignView(view: self.view)
    NewView.makeView(view: self.view)

}

}

  • gesture is not working.

In the menuView class i make a view and add a gesture to it .In the viewController class i add the menuView and run the code.the view is added but the gesture is not working.

Jai kalra
  • 21
  • 3
  • There is not such class as `uiTapGestureRecognizer`. Please post the code that you have on Xcode. – Rakesha Shastri Nov 20 '18 at 08:07
  • Did you changed the later in your code? Because currently from the code you shared it seems that the frame is zero and you cannot tap on a view with zero frame. – Aakash Nov 20 '18 at 08:11
  • it doesn't matter, if you closely look he is ultimately adding the tap gesture to the `ViewControllers`'s view. Naming convention is such a mess – Ratul Sharker Nov 20 '18 at 08:14
  • Possible duplicate of [How to call gesture tap on UIView programmatically in swift](https://stackoverflow.com/questions/28675209/how-to-call-gesture-tap-on-uiview-programmatically-in-swift) – rptwsthi Nov 20 '18 at 08:25

1 Answers1

-1

The correct way should have been to inherit subview with UIView class.

See below example -

override func viewDidLoad() {
    super.viewDidLoad()
    let newView = subView()
    newView.addGuesture()
    self.view.addSubview(newView)
    // Do any additional setup after loading the view, typically from a nib.
}
class subView:UIView{
    func addGuesture(){
        let tap = UITapGestureRecognizer()
        tap.addTarget(self,action:#selector(handleTap))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(tap)
        self.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        self.backgroundColor = UIColor.red;
    }
    @objc func handleTap(){
        print("tap is working")
    }

}
Tushar Limaye
  • 106
  • 2
  • 7