Bangalore
Bangalore

Reputation: 1

touchesBegan not working on UIView in iOS?

I have created a UIView Programmatically on button click like this

self.paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height+100)];
[self.paintView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.navigationController.view addSubview:self.paintView];

and added that to navigationController so i can see my UIView in fullscreen.

But when i tried to detect touch its not working

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touch.view==self.paintView && self.paintView ) {
        [UIView animateWithDuration:0.9 animations:^{
          [self.sideView setFrame:CGRectMake(-290, 0, self.sideView.frame.size.width, self.sideView.frame.size.height)];
     }];
    }

}

This method not at all detecting now.

Upvotes: 0

Views: 1086

Answers (4)

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

Can you try below code to add view and check:

self.paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height+100)];

[self.paintView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];


[self.window.rootViewController.view addSubview:self.paintView];//try this

The view created by the generic UIViewController fills the window and is in front of your SubView. Thus your SubView can't receive touches.

rootViewController.view be the single top-level view in your window, and add subviews under that and check.

Upvotes: 1

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

And give your UIView UserInteractionEnable = YES

Replace your -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { } code with mine one you get point.

CGPoint point = [touch locationInView:touch.view];
 CGPoint pointOnScreen = [touch.view convertPoint:point toView:nil];
 NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
 NSLog(@"Touch");

Upvotes: 0

Jamshed Alam
Jamshed Alam

Reputation: 12844

Make userInteractionEnabled enable on all superviews. If superviews have this property set to false, the touch won't be processed subviews.

You also can use UITapGestureRecognizer.

Upvotes: 0

Tushar
Tushar

Reputation: 3052

This can happen if 'userInteractionEnabled' NOT is enabled on all of the superviews of your view. If any of the superviews have this property set to false, the touch events won't be processed for them or any of their subviews. Check if that can be the issue.

Upvotes: 0

Related Questions