iOS scroll view childs are not getting touched - ios

I'm new to iOS so I'm getting problem with scroll view.
My scroll view having image views as subviews. Whenever I'm trying to touch image inside scroll view, scroll view touch is getting enable instead of subview.
How to resolve this problem?
Thanks

Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(methodName:)];
singlTap.cancelsTouchesInView = NO;
[scrollViewName addGestureRecognizer:singlTap];
then write your code in specified method.
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
Thats all

Related

How to implement common tap Gesture which will work for all View controllers

i want to use one common tap gesture for show/hide slider view in view controller with multiple View controller classes.
please provide solution how to implement this for ios .
slider view will show / hide by tap of view of View Controller.
and slider view contain tableview so tableview cell also be select when user click on tableview cell in ios.
is there any way to create a Abstract class for that.
thanks in advance.
You can add tapgesture to main content view of VC like,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:recognizer];
And hanlde tap in method like,
-(void)handleTap : (UITapGestureRecognizer*)recognizer {
NSLog(#"tap detect");
}
Hope this will help :)

SubView not Considered a part of MainView

I have many SubViews in my UIView, and many of them have UIButtons in them. One of the subviews- _bottomView (Coordinates- (0,519,320,49)) has an error. It does not recognise the click events on the buttons placed inside it.
I tried placing a UIButton covering the entire _bottomView and the click event from that Button (testButton) is not being recognised either.
I tried adding a tapRecogniser to the code and the tap from every point, EXCEPT the points within the _bottomView are recognised. TapRecogniser Code below
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
_bottomView.userInteractionEnabled=true;
-(void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
NSLog(#"got a tap in the region i care about");
}
I tried [self.view addSubview:_bottomView]; and that didn't help either. What could be the issue?
If your view is not receiving touches
Make sure that your UIView has userInteractionEnabled set to YES
Make sure that the view is within the bounds of its superview, and the superview is within the bounds of window.You can print the frames of views using NSLog(#"%#",NSStringFromCGRect(view.frame));
Nothing helped. I had to delete the entire View Controller and redo the whole thing. That obviously solved the problem.

iOS: propagate pan gesture to UITableView

I have a UITableView, each cell contains a UIScrollView inside. I want to capture the pan gesture inside the UIScrollView to do some stuff, and without affecting the normal behaviour of the table view. eg) swipe up cause the table view to scroll up.
The code snippet:
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
NSLog(#"handleSwipe");
}
In UITableView's cell for row at index path function:
UIPanGestureRecognizer *swipeLeft = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[picScrollView addGestureRecognizer:swipeLeft];
Now I can see the handleSwipe is called but the UITableview stop functioning - it just can't scroll now.
I think the pan event is eaten by the scrollview but I just want it to be propagate to the table view.

Moving view but keeping imageview in fixed position

I'm moving a UIView up with a swipe, but I want the imageView within the UIView to stay in its position. I got this working by placing my imageView under a separate UIView ( I needed to include a label under the imageview) by doing [self.view insertSubview:self.imageViewHolder aboveSubview:self.panedView];. The problem now is that when i swipe down over the imageView, my view that I swiped up won't come back down unless I swipe around the imageView. This is because the UIGestureRecognizer is associated with the view under the imageView. Any simple ways around this?
Gesture recognizer code:
UISwipeGestureRecognizer * swipeUpRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleUpSwipe:)];
[self.panedView addGestureRecognizer:swipeUpRec];
[self.imageViewHolder addGestureRecognizer:swipeUpRec];

Hide UITableView on touches outside the tableview

I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
Best Approach
Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.
Show Overlay View first - alpha will be 0.5f and background color should be clear color.
show the table view.
NOTE: over lay view should have tap recogniser which will hide the overlay and table view
in View did load
UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(overlayDidTap:)];
[myOverLayView addGestureRecognizer:tapRecog];
- (void)overlayDidTap:(UITapGestureRecognizer *)gesture
{
//hide both overlay and table view here
}
Bad Approach
We should not add tap recogniser on main view itself. Because it may have lots of
controls inside of it. so when user tap on it. it will perform its operation. So to avoid
it we can simulate the same behaviour by above approach
You can get touch position by this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[self.view addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:self.View];
}
Then just check if the point is in tableview frame. If not then hide the tableview. Hope this help. :)
Subclass UITableView, override pointInside:withEvent:. It is templated for this reason.
Something like:
-(BOOL)pointInside:(CGPoint) point withEvent:(UIEvent*) event
{
BOOL pointIsInsideTableView = [super pointInside:point withEvent:event];
BOOL pointIsOutsideTableView = // Some test
return pointIsInsideTableView || pointIsOutsideTableView;
}
So you can catch the touch in table view implementation, where it belongs logically in this case.

Resources