Reputation: 133
I have a UIImageView
with a UILongPressGestureRecognizer
that sends an action when a long press is detected.
I do have user interaction enabled on the UIImageView
. However, I also have a sort of manual scrolling where the UIImageView
can be moved using the touchesBegan
and touchesMoved
methods.
When the user interaction is disabled, only the scrolling works. When the user interaction is enabled, only the long press gesture recognizer works.
How can I make it so that both will work simultaneously?
Code can be shared if necessary, but I don't think this problem requires it.
Upvotes: 1
Views: 4989
Reputation: 4901
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[gestureRecognizer addTarget:self action:@selector(imgLongPressed:)];
gestureRecognizer.delegate = self;
[imgview addGestureRecognizer: gestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *view_ =(UIImageView*) sender.view;
CGPoint point = [sender locationInView:view_.superview];
if (sender.state == UIGestureRecognizerStateBegan)
{
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
}
}
Upvotes: 3
Reputation: 3260
First of all you will have to use gesture for moving the image like below.
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[vwBigImage addGestureRecognizer:panRecognizer];
the function which will be called is like below..
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:yourview];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_firstX = [yourimageview center].x;//declare CGFloat _firstX; in .h file
_firstY = [yourimageview center].y;//declare CGFloat _firstY; in .h file
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
[yourimageview setCenter:translatedPoint];
}
Now your both gestures will be called.
Let me know it is working or not!!!
Happy coding!!!!
Upvotes: 0
Reputation: 45598
Gesture recognizers always take priority and override touchesBegan:
style events. You will have to implement both behaviours using the same API/technique.
Upvotes: 0