Search code examples
objective-cuiviewxcode4uibuttonuser-interaction

how can enable and disable the user-interaction option of uiview in a button click


I am trying to implement an Ipad application with gesture recognizer. In my application has some small UIView and each UIView has a sub view (UIButton). My need is,if click on a button ,the super view of this button's user interaction option will become YES and the Others views userinteraction option will become NO. After implementing this, the super view over rule the sub view (i.e. if the user-interaction option of one view become NO,this will affect the sub view).How can avoid the above issue?


Solution

  • Well, assuming you have a predefined number of views and subviews set-up in a storyboard or xib file you can add a property for each view. Then use the gesture recognizer to determine if the user taped a view. If they did you can use the properties you set up to set the appropriate views enabled property to YES or NO.

    For example, add an IBAction to detect your gesture recognizer that does something similar to the following:

    - (IBAction)tap:(UITapGestureRecognizer *)gesture
    {
        CGPoint tapLocation = [gesture locationInView:self.aViewWhereYouMonitorGestures];
        for (UIView *view in self.aViewWhereYouMonitorGestures.subviews) {
            if (CGRectContainsPoint(view.frame, tapLocation)) {
                self.someView.enabled = YES; // or NO depending on what you want to do.
            }
        }
    }