I have a UIScrollView with multiple UIImageViews in it created like this.
frame = [[UIImageView alloc] initWithImage:bg];
frame.frame = CGRectMake(FRAME_SEPARATOR + numPage*1024 + numColumn*(FRAME_SEPARATOR+230), 10 +numRow*(FRAME_SEPARATOR+145), 230, 145);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[frame addGestureRecognizer:tap];
[tap release];
[scroll addSubView:frame];
The problem is that imageTapped is not being called when tapping over an image.
If I add the gesture recognizer to the scrollview like this:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scroll addGestureRecognizer:tap];
[tap release];
imageTapped is called.
How can I detect the taps over the UIImageViews?
Thanks
Make sure userInteractionEnabled is set to YES on the UIImageView:
frame.userInteractionEnabled = YES;
I'd also recommend using a different name for the UIImageView variable (eg. imageView instead of frame). Otherwise, you can easily confuse it with the view's frame property.
Related
I have an UIImageview with tap gesture on it, such that any tap over it creates a rectangular view with white background. Which I can move any where over imageview. Like this I can create infinite views over that imageview.
Now my problem is that I want to add double tap gestures to newly created views but when I click on any of those views then a new view is created over that view.
But what I want is that if I tap on a view then no new view must be created over that view.
Currently gestureRecognizer recognises UIImageView even if I have created a new view over UIImageView.
ImageView,
UIImageView *imageView = [[UIImageView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(createNewView)];
[tapRecognizer setNumberOfTapsRequired:1]; // allow to create new view
UITapGestureRecognizer * tapRecognizerToFail = [[UITapGestureRecognizer alloc]init];
[tapRecognizer setNumberOfTapsRequired:2]; // to disallow more than 1 tap.
[tapRecognizer requireGestureRecognizerToFail:tapRecognizerToFail];
[imageView setGestureRecognizers:[NSArray arrayWithObjects:tapRecognizer,tapRecognizerToFail,nil]];
// New View Creation over ImageView
-(void)createNewView
{
UIView * view = [[UIView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(newViewAction)];
[tapRecognizer setNumberOfTapsRequired:2];
[view addGestureRecognizer:tapRecognizer];
[imageView addSubview:view];
}
// Double tap action over newly created view
-(void)newViewAction
{
// goes here
}
You can use requireGestureRecognizerToFail: to declare a dependency between your two recognizers.
[secondaryGesture requireGestureRecognizerToFail:primaryGesture];
I am using Apple code for just zoom the UIImageView on taps and gestures. But its not work ?
Please see this Apple's link apple code for image zooming by taps and gestures
-(void)veiwDidLoad
{
[super viewDidLoad];// Edited my self
imageView.userInteractionEnabled = YES; // Edited my self
//All code below same.... like Apple's code
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[imageView addGestureRecognizer:singleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:twoFingerTap];
[singleTap release];
[doubleTap release];
[twoFingerTap release];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
}
Make sure that you set your imageView to be userInteractionEnabled = YES; .. its assign to NO by default.
change the function name from
-(void) veiwDidLoad
to
-(void) viewDidLoad
I has a problem like that as well, does that fix it?
For UITapGestureRecognizer, you can set number of taps required to control the recognition of the UITapGestureRecognizer. If you set numberOfTapsRequired to 2 and user taps only once, then the UITapGestureRecognizer won't be triggered.
My question is How about for UIPanGestureRecognizer? How to control its recognition?
I have a view. Once I set a UIPanGestureRecognizer to it, any dragging will trigger the action. But what I want is only the dragging in X-axis. And for non-X-axis dragging, all touch events should be sent to other views underneath.
How can I do it?
THanks
Set its delegate and implement
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
Then use
- (CGPoint)velocityInView:(UIView *)view;
on the gesture recogniser to calculate whether the gesture recognizer should handle it or not.
Check How to stop UIPanGestureRecognizer when object moved to certain frame. It may help you.
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
[self.view addSubview:holderView];
Raees
Assume that your gestureRecognizer triggers the action _panRecogPanned below. You can see how the center of a subview ( the view that carries the gesture recognizer itself) moves following the transition. To disable panning on y axis, you simply set the center as the calculated new center, whereas the translation.y is omitted.
To move other subviews on the y axis, get their frame, update their origin.x property and reset the frame, they should follow your finger only on the y axis.
- (IBAction)_panRecogPanned:(id)sender{
CGPoint translation = [_panRecog translationInView:_statementFilterView];
//This subview only moves horizontally
_panRecog.view.center = CGPointMake(translation.x + _panRecog.view.center.x, _panRecog.view.center.y);
//This subview only moves vertically
CGRect newFrame = anotherSubview.frame;
newFrame.origin.y = anotherSubview.frame.origin.y + translation.y;
anotherSubview.frame = newFrame;
[_panRecog setTranslation:CGPointMake(0, 0) inView:self.view];
}
I have an UIImageView inside a subclass of UITableViewCell in which I wired up with UITapGestureRecognizer. However, when I press on the image the action is not triggered. What's being triggered is the didSelectRowAtIndexPath.. why is this?
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture]; [tapGesture release];
Also one minor question is that I have a UITextView in the cell, however when I click on the text it did not trigger the didSelectRowAtIndexPath, why is this and how do I change it?
UIImageView has userInteractionEnabled set to NO by default. Try setting it to YES
imageView.userInteractionEnabled = YES;
For your other question, try setting the editable property to NO for the UITextView.
textView.editable = NO;
i got a strange problem. I got a Superview with customcontrols as subviews.
Their are GestureRecognizer both in superview and subviews.
If i tap a subview its GestureRecognizer gets called and on tap on the superview its tap gets called.
but on long press in the subview SOMETIMES the GestureRecognizer of the superview gets called. I add the GestureRecognizers in the same functions but there is a different attitude.
Superview
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPiece:)];
tapGesture.numberOfTapsRequired = 1;
//contenView is the area where my controls can be
[self.contentView addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longTapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTapPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self.contentView addGestureRecognizer:longTapGesture];
[longTapGesture release];
Subviews
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPiece:)];
longPressGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:longPressGesture];
Can anyone tell me why the longtap doen't response to my subview and how to fix it.
THANK YOU
got a solution but this is not what i wanted i set the duration of the control lower than the one of the superview [longPressGesture setMinimumPressDuration:0.4]; But the gestureRecognizer should be independend