0

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
    }
}
3
  • in else condition change the tag and try Commented Feb 2, 2016 at 10:06
  • @Anbu.Karthik. thanx for suggestion but it is not working. It is unhidding the custom view but on tapping the custom view itself. Commented Feb 2, 2016 at 10:10
  • @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) Commented Feb 2, 2016 at 10:53

1 Answer 1

0

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@ankita.thnx for suggestion. But the problem is still there. Custom view is hiding but on tapping custom view itself. Logic is correct but i cannot understand what am i doing wrong.
You need to add touch on the main view. Suppose you main view is 200*200 and subview is 100*100, then you need to add touch to view with 200*200. This method will be in touch selector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.