0
votes

I have made a custom view and is hidden by default. By pressing a button I am unhiding it. Now I want to again hide it by adding outside the custom view. I have used touchesbegan with event but that is not serving the purpose. This is the function.

override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

   let touch = UITouch()

    if touch.view?.tag == 1 {          // 1 is the tag value for custom view

        self.view1.hidden = false     // view1 is the outlet for custom view
    }
    else{
        self.view1.hidden = true
    }
}
1
in else condition change the tag and try - Anbu.Karthik
@Anbu.Karthik. thanx for suggestion but it is not working. It is unhidding the custom view but on tapping the custom view itself. - dvshrwt
@Anbu.Karthik you could not get outside tap event of custom view inside customview so rather than that you can add transparent button in your view and use its touchup outside event (this is not better way but you can may find your solution) - Parth Dabhi

1 Answers

0
votes

Add touch to superview of you'r custom view. On it's function call use this

func superView_Touch(){
    let childView = superView.viewWithTag(1)
    if(!childView.hidden){
        childView.hidden = true
    }
}