Change Image in ImageView within UIPanGestureRecognizer? - ios

I have an ImageView, and Images URL in array. I want to download and show these images on UIImageView, but issue this that I am facing Two Gestures on that View, so inner Gesture which is on only ImageView is not looking to work.
My code is like below
[self.imageView setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.imageView addGestureRecognizer:swipeLeft];
[self.imageView addGestureRecognizer:swipeRight];
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(#"Right Swipe");
}
}
The method handleSwipe is not called and also when I move it opens side menu, as there is Gesture applied same as facebook side menu opens in its iOS app.
How can I change images in ImageView when whole parent view is also using Gesture.
Thanks

try this. It solves both ypur problems
[self.imageView setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.imageView addGestureRecognizer:swipeLeft];
[self.imageView addGestureRecognizer:swipeRight];
- (void)handleLeftSwipe
{
NSLog(#"Left Swipe");
}
- (void)handleRightSwipe
{
NSLog(#"Left Swipe");
}

Related

How to implement Swipe and Hold gesture recognizer in Objective-C?

I am working on an application which works like a remote control of different devices like Game Console, Set-top Box, etc.
Application contains different type of controls like gesture and buttons to perform events as remote.
Single gesture view contains multiple gesture recognizer as follow:
1 finger Tap using UITapGestureRecognizer
2 finger Tap using UITapGestureRecognizer
1 finger swipe using UISwipeGestureRecognizer
2 finger swipe using UISwipeGestureRecognizer
My requirement is to implement 1 finger Swipe gesture recognizer along with hold event. So, that will continue my swipe action until user will not remove its finger from the view.
I have tried UILongPressGestureRecognizer after UISwipeGestureRecognizer but this didn’t work for me because it is executing along with Swipe movement. And I want this after Swipe end, but finger will not remove.
My code snippet :
-(void) gestureView:(UIView*)viewGesture {
UISwipeGestureRecognizer *swipeGestureSingleRight;
UISwipeGestureRecognizer *swipeGestureSingleLeft;
UISwipeGestureRecognizer *swipeGestureSingleUp;
UISwipeGestureRecognizer *swipeGestureSingleDown;
swipeGestureSingleUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleUp setDelegate:self];
[swipeGestureSingleUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
[swipeGestureSingleUp setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleUp];
swipeGestureSingleDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleDown setDelegate:self];
[swipeGestureSingleDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[swipeGestureSingleDown setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleDown];
swipeGestureSingleRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleRight setDelegate:self];
[swipeGestureSingleRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[swipeGestureSingleRight setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleRight];
swipeGestureSingleLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleLeft setDelegate:self];
[swipeGestureSingleLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[swipeGestureSingleLeft setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleLeft];
longPressGestureOnSwipe = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGesture_Handle:)];
longPressGestureOnSwipe.minimumPressDuration = 0.03;
longPressGestureOnSwipe.delegate = self;
[viewGesture addGestureRecognizer:longPressGestureOnSwipe];
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
- (void)swipeGestureSingle_Handle:(UISwipeGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
singleTouchSwipeSenderDirection = sender.direction;
isSwipeEnd = true;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
-(void)longPressGesture_Handle:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (isSwipeEnd) {
[super touchesBegan:touches withEvent:event];
} else {
return;
}
}
Please suggest me correction or solution for my requirement.
Thanks in advance!

How to create swipe gesture for right/left in detailed view UIimage

I am working on a food app in which each tableViewCell shows different recipes names. When a user taps on a recipe (in a cell), a detailed view will be opened.
I am now trying to implement a swipe gesture for UIImageView which shows different images on swipe. How do I implement that?
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeImage:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeImage:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[Allimages addGestureRecognizer:swipeLeft];
[Allimages addGestureRecognizer:swipeRight];
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe{
NSInteger indexPath;
if (indexPath==0) {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
arrayimage=[[NSMutableArray alloc]initWithObjects:# "BBQ Chicken Pizza.jpg",# "roastchickensteaks.jpg", nil];
swipee = (swipee > 0)? ([arrayimage count]-1):
swipee%[arrayimage count];
Allimages.image=[UIImage imageNamed: [arrayimageobjectAtIndex:indexPath]];
}}
http://i.stack.imgur.com/apDEw.png
http://i.stack.imgur.com/EiURG.png
http://i.stack.imgur.com/1q799.png
Add this
[imageview setUserInteractionEnabled=YES];
create object for swipegesture recognizer class
UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(onSwipe)];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
Add swipeGesture to imageView
[imageView addGestureRecognizer:leftSwipe];
-(void)onSwipe:(UISwipeGestureRecognizer)swipe{
if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(#"leftSwipe");
}
}
if you want more information: just check
Gesture recognizer (swipe) on UIImageView
In your detail View Make UserInteractionEnabledt to YES of your imageview.
[imageView setUserInteractionEnabled:YES];
Adding a Swipe Gesture Events
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeLeft];
[imageView addGestureRecognizer:swipeRight];
And Handle Swipe Gesture Events using below code.
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(#"Right Swipe");
}
}
Or else You can use Third Party Tool
https://www.cocoacontrols.com/controls/pagedflowview

Handling UIGestureRecognizer Swipe Left

I am trying to add a gesture recognizer using the following code.
However, the gesture is not getting recognized. Is there something else I have to do in order to make the view controller a delegate of the view--or something to that effect?
I have implemented the UIGestureRecognizerDelegate protocol in the .h file
//in view did load
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[_myImage addGestureRecognizer:rightRecognizer];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[_myImage addGestureRecognizer:leftRecognizer];
//new methods
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
NSLog(#"Right Swipe performed");//not appearing
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
NSLog(#"Left Swipe performed");//not appearing
}
1st you need
_myImage.userInteractionEnabled=YES;
then
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[_myImage addGestureRecognizer:swipeLeft];
[_myImage addGestureRecognizer:swipeRight];
then call method like
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(#"Right Swipe");
}
}
i hope this will work..
You have to enable user interaction of "_myImage" before adding gestures to image view as:
_myImage.userInteractionEnabled = YES;

iOS: webview swipe gesture

I have 2UIWebView controls. Using these 2 webviews, I have successfully implemented swipe gesture animation.
But the problem is, when I click on next or previous button(oh yes, I also have next, previous, first and last buttons to read a book), swipe works perfectly.
But on webview it works weirdly. Following happens:
Swipe doesn't work on webview.
When I click on next or previous buttons and then swipe the webview, swipe on webview works.
Following is my code snippet:
In viewDidLoad:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionLeft];
[webViewPage addGestureRecognizer:swipeRight];
[_webview2 addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[webViewPage addGestureRecognizer:swipeLeft];
[_webview2 addGestureRecognizer:swipeLeft];
To enable swipe on UIWebView:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// NSLog(#"shouldRecognizeSimultaneouslyWithGestureRecognizer");
return YES;
}
- (IBAction)btnPrevious_click:(id)sender {
//some code
}
- (IBAction)btnNext_click:(id)sender {
//some code
}
Where am I getting wrong?
From your code, you have to added 2 UISwipeGestureRecognizer in only one UIWebView name is _webview2 please change it as per your requirement.
Like iPatel mentioned. Each of the webview should has its own GestureRecognizers. So, you should have 4 GestureRecognizers in total.
Try this:-
UISwipeGestureRecognizer *swipeRight1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight1 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft1 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview1 addGestureRecognizer:swipeRight1];
[_webview1 addGestureRecognizer:swipeLeft1];
UISwipeGestureRecognizer *swipeRight2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight2 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft2 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview2 addGestureRecognizer:swipeRight2];
[_webview2 addGestureRecognizer:swipeLeft2];

UISwipeGestureRecognizer not responding to Up

I have the following UIGestureRecognizer which detects swipe up properly
- (void) addGestureRecognizer {
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
}
- (void) didSwipe:(id)sender{
NSLog(#"didSwipe");
}
I then modified the direction to include left and right
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
and it no longer responds to swiping up (swiping left or right works). What am I doing wrong? (tried on simulator, iphone5, ipad3)
Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe. Thanks.
Try this way
UISwipeGestureRecognizer *_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
EDIT
As #LearnCocos2D suggested " Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags. "
And As per your "Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe.", You just need to detect swipe not the direction so combining right-left as one and up-down as other gesture will work.
swipeGesture is discree gesture, and swipe up and down is two different gesture.
You must create 4 gesture and send it to one action.
- (void) addGestureRecognizer {
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeLeft setDelegate:self];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeRight setDelegate:self];
[self.view addGestureRecognizer:swipeRight];
}
- (void) didSwipe:(UISwipeGestureRecognizer *)sender{
NSLog(#"didSwipe");
}

Resources