I have an imageview and I want to tap on only one side of imageview. Is it possible to set frame for a gesture? Can anyone help with a solution?
Use UIGestureRecognizerDelegate, i think you can get the idea on how to compare:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch locationInView:yourview].x < somePoint.x) {
return false;
} else {
return true;
}
}
you could overlay a view on top of the imageview and add the tap recognizer to this new view, something like this will make the left hand side of the image tapable
UIView tapView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, imageView.frame.size.width/2, imageView.frame.size.height)];
[imageView addSubView:tapView]
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[tapView addGestureRecognizer:singleFingerTap];
You can simply put UIView on top of your imageView with frame as per your requirement and put tap gesture on that UIView.
You can do this by storyboard / xib or by programmatically.
By programmatically, for example - you want to tap only within the area with width of 50 px of your imageView. For this:
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y, 50, imgView.frame.size.height)];
// add gesture to view
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[vw addGestureRecognizer:tapGesture];
[imgView addSubview:vw];
now to handle your double tap
-(void)handleTapGesture:(UITapGestureRecognizer *)gesture
{
// handle double tap
}
Related
I'm trying to add UITableView inside UIView but I got problem with gesture recognizer. This is my code:
_subView = [[UIView alloc] initWithFrame:CGRectMake(0, screenRect.size.height-100, screenRect.size.width, 300)];
_subView.backgroundColor = [UIColor colorWithRed:0.110f green:0.192f blue:0.298f alpha:1.00f];
[self.view addSubview:_subView];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(subViewToggle)];
[_subView addGestureRecognizer:singleFingerTap];
_moreTable = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenRect.size.width, 240)];
//Transforme to HOR scroll ;
CGRect frame = _moreTable.frame;
_moreTable.transform = CGAffineTransformRotate(CGAffineTransformIdentity, k90DegreesCounterClockwiseAngle);
_moreTable.frame = frame;
[_moreTable registerNib:[UINib nibWithNibName:#"MoreTableViewCell" bundle:nil] forCellReuseIdentifier:#"moreCell"];
_moreTable.backgroundColor = [UIColor groupTableViewBackgroundColor];
_moreTable.delegate = self;
_moreTable.dataSource = self;
[_subView addSubview:_moreTable];
[_subView bringSubviewToFront:_moreTable];
The problem is when I click on table cell instead of executing didSelectRowAtIndexPath method he execute the subViewToggle Method. I tried to bring my tableView to front but didn't help
your tap gesture eat the touch. there are two way you can deal:
first, you can implement - gestureRecognizer:shouldReceiveTouch: delegate method, and decide if tap gesture should occur.
second, you also can set cancelsTouchesInView property of tap to NO, so tableView will receive touch. but tap will receive touch at the some time too.
Try implementing the UIGestureRecognizerDelegate protocol's gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
This should call both your didSelectRow and GestureRecognizer
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
Please try to use like this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view == _moreTable]) {
return NO;
}
return YES;
}
To solve this Problem I've changed the UIView Gesture from single finger tap to moveUP and moveDown gesture.
Note that I've tried only #dharmbir Solution and It haven't worked for me.
Update Also this solution, implementing the shouldReciveTouches, worked for the tapGesture and her's the code snippet :
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// UITableViewCellContentView => UITableViewCell
if([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
// UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
return YES;
}
//Create tap gesture for menu transparent view
UITapGestureRecognizer *rightTableTransparentViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rightTableTransparentViewTapMethod:)];
[rightTableTransparentViewTap setCancelsTouchesInView:NO];
[_rightTableTransparentView addGestureRecognizer:rightTableTransparentViewTap];
- (void)rightTableTransparentViewTapMethod:(UITapGestureRecognizer *)recognizer {
//Write your code here
}
I have a viewcontroller, which contains a web view.
I'd like to add two gesturerocignizers to the viewController's view:
One UITapGestureRecognizer and one UILongPressureGestureRecognizer.
Here's the code:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(backToDashboards:)];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(backToDashboards:)];
doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.delegate = self;
[self.view addGestureRecognizer:longPressGesture];
[self.view addGestureRecognizer:doubleTapGesture];
My view hierarchy:
-ViewController
|
|-View (added the gesture recognizers for this view)
|-WebView
I've added the shouldRecognizeSimultaneouslyWithGestureRecognizer method with return YES
But if I long press the screen nothing happens, only if the press' location is on a place where nothing can be scrolled in the webview.
The double tap recognizer doesn't work at all.
Any idea?
Thank you in advance!
I suspect that because your webview is on top of your view, the app doesnt know if you are tapping the webview or self.view. Check out this reply How to detect of tap gesture is from uiwebview or self.view?
try with below code.Where u r adding gester in you code?
below code is working for me.
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPress.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:longPress];
// if u want to add gesture for u webview,
//[self.webview addGestureRecognizer:longPress]
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
}
//You should enable simultaneous gesture recognition
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
I have a subview of imageview with a PanGestureRecognizer, and the main view has a LongPressGestureRecognizer. I have added the longpress only to the view like this:
screenRecognize = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(screenTaped:)];
screenRecognize.minimumPressDuration = 0.0;
self.userInteractionEnabled = YES;
[self addGestureRecognizer:screenRecognize];
And here's the imageview:
imageViewPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewPulled:)];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(310, 50, 10, 40)];
imageView.image = [UIImage imageNamed:#"image.png"];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:imageViewPanRecognizer];
[self addSubview:imageView];
When I touch the imageView, the UILongPressRecognizer is fired. Why is this?
The answer is already in your question. You set
screenRecognize.minimumPressDuration = 0.0;
that means, UILongPressGestureRecognizer will work as like UITapGestureRecognizer. By the line
[self addGestureRecognizer:screenRecognize];
you active this gesture on all over the self.
Now when you add imageView in the self, the imageView will also response to the UILongPressGestureRecognizer as well as UIPanGestureRecognizer which is only active on the imageView. As a result touching on the imageView is firing UILongPressGestureRecognizer.
To solve this problem you can try by increasing the minimumPressDuration value.
1) Why you use long gesture with minimumPressDuration=0? Can not properly use pan gesture?
2) If you want that gestures work together, try something like this:
longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
3) If you whan if user pan to UIImageView long gesture disable, try something like this:
self.tag = 1;
longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return touch.view.tag == 1;
}
I have a UIView and a tap gesture recognizer in it:
UIImageView *tabView = [[UIImageView alloc] initWithFrame:CGRectMake(41, 145, 702, 100)];
tabView.image = [UIImage imageNamed:#"inactive_tab"];
tabView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[tabView addGestureRecognizer:singleFingerTap];
[self.scrollView addSubview:tabView];
And I add another view on scrollview:
[self.scrollView addSubview:self.activeTab];
activeTab is over the inactiveTap. When I tap to activeTap, gesture recognizer fires, whis I dont want to be happened. How can I avoid this?
Use UIGestureRecognizerDelegate and its method gestureRecognizer:shouldReceiveTouch:.
You can check if the touch point is inside a view frame and return NO if you don't want the touch to happen on that view.`
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint touchLocation = [touch locationInView:self.view];
return !CGRectContainsPoint(self.activeTab.frame, touchLocation);
}
I have the following problem with a UIPanGestureRecognizer inside a UIScrollView:
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
sv.contentSize = CGSizeMake(200, 100 *100);
for (int i = 0; i < 100; i++) {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, i * 100, 200, 100)];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panTile:)];
[panGesture setDelegate:self];
[panGesture setEnabled:FALSE];
[newView addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[newView addGestureRecognizer:longPressRecognizer];
[sv addSubview:newView];
}
The complete scroll view is filled with small tiles, each of them implements a pan gesture in order to make them draggable. The problem is that - doing so - prevents the scroll view from scrolling. Dragging of the tiles instead works fine. When I disable the tiles pan gestures, scrolling works perfectly. The tiles pan gesture somewhat hides the scroll views own pan gesture. My idea was to disable the tiles pan gesture from the beginning. The gesture is enabled once the user does a long press on the tiles. The problem is that the user has to lift the finger and then touch the tile again to drag it. When the drag is finished, I enable the long press and disable the pan gesture again. So longPress: looks as follows:
- (void)longPress:(UILongPressGestureRecognizer *) gestureRecognizer {
for (UIGestureRecognizer *r in gestureRecognizer.view.gestureRecognizers) {
if ([r isKindOfClass:[UIPanGestureRecognizer class]]) {
[r setEnabled:TRUE];
}
}
//pan gesture should take over here...
}
Is there any possibility to glue the long press and the pan gestures together so that the user doesn't have to lift the finger? Or maybe another solution?