I am trying to detect touches inside a subview
In my main viewcontroller I am adding a subview called:SideBarForCategory, it takes 30% of the screen from the left – as a sidebar.
SideBarForCategory *sideBarForCategory = [[SideBarForCategory alloc] initWithNibName:@"SideBarForCategory" bundle:nil];
[sideBarData addSubview:sideBarForCategory.view];
inside SideBarForCategory, I would like to test for touches
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(@"Map Touch %f",location.x);
}
The above code (touchesMoved) works perfectly on the main view (viewcontroller) but does not work inside my subview (SideBarForCategory) – why and how can I fix it?
Two possible solutions i can think of :
Either use GestureRecognizers (eg. UITapGestureRecognizer, UISwipeGestureRecognizer) and add those recognizers to your SideBarForCategory view.
Generic gesture handling : Create your own custom subclass of UIView for example MyView and add those touch methods inside it. Then create SideBarForCategory view as an instance of MyView.
Hope it works 🙂
Updated :
For second option :
Update :
And then in your SideBarCategoryView class implementation , inside loadView()