SRI
SRI

Reputation: 1532

Enable UserInteraction to some part of UIViewController

I have been researching about it since last 3 to 4 hours but I didn't get any information. My issue is I want to enable userInteraction to some part of the UIViewController.

Description:

I have a UIViewController. I have added 30 tableviews. I have stored one value in the application. If that value is 1 then I have to enable user interaction for tableview1 only and if the value is 2 then tableview2 only ........etc

. Please let me know if I am not clear. Thank you for spending your valuable time. Thanks in advance

Upvotes: 0

Views: 313

Answers (2)

meth
meth

Reputation: 1885

Another solution without - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event. is to adding UIButton as a subview to the part of UIView you want to close interaction. for example if you want to close interaction of the bottom half of view.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, self.view.frame.size.height*0.5f, self.view.frame.size.width, self.view.frame.size.height*0.5);
    [self.view addSubview:button];

since it will get the touch events, half of the view will be closed to user interaction.

EDIT

IBOutletCollection(UITableView) NSArray *allTableViews;// get all your tableviews reference to this array. set tag in interface builder for each array to reach later.

then when you want to enable/disable interaction of related tableview

int tagToOpenInteraction = 1;//or whatever it is
for(UITableView *t in allTableViews)
{
     if(t.tag == tagToOpenInteraction)
         [t setUserInteractionEnabled:YES];
     else
        [t setUserInteractionEnabled:NO];  
}

Upvotes: 1

Tanguy G.
Tanguy G.

Reputation: 2303

A simple way to do it is to subclass UIView and override - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event.

Return NO for the part of the UIView (represented as ignoreRect in the sample) you want the subviews to ignore touches.

@interface InteractionView ()

@property (nonatomic) CGRect ignoreRect;

@end

@implementation InteractionView

- (void)awakeFromNib {
    self.ignoreRect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.ignoreRect, point)) {
        return NO;
    }

    return [super pointInside:point withEvent:event];
}

@end

If you need more tweaking for an expected behavior : for exemple return a specific view for a specific zone, return the top view of a specific zone, ... you may use

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.ignoreRect, point)) {
        return nil; // Edit that part if you want to return a chosen view
    }

    return [super hitTest:point withEvent:event];
}

Upvotes: 1

Related Questions