UISwipeGestureRecognizer inside UIScrollView does not swipe right - Objective C - ios

I am using two swipe gestures inside scrollview so i could be able to change photos and zoom in , swiping left works normally and smoothly while swiping right does not work properly , after many swipes it could be triggered once maybe,i am using this code:
//image zoom
self.scrollView.minimumZoomScale=1.0;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
self.scrollView.clipsToBounds = YES;
self.scrollView.scrollEnabled = false;
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
And these two methods to handle the swipe
-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"LEFTtttttt");
if(index > 0 && index < _photoCount){
index = index - 1;
[self loadImage];
}
}
-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"RIGHTttttt");
if(index >= 0 && index < _photoCount - 1){
index = index + 1;
[self loadImage];
}
}
I searched for a solution of course and tried different codes but it did not work , i added
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:swiperight];
And this one too,
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomScale!=1.0) {
// Zooming, enable scrolling
scrollView.scrollEnabled = TRUE;
} else {
// Not zoomed, disable scrolling so gestures get used instead
scrollView.scrollEnabled = FALSE;
}
}
The only difference is when i added the latter code swiping right worked normally when i zoomed in when it should work in the opposite case.
Did i miss something in my code?
Thanks.

Please add swipe gesture to the scrollView not on self.view to make it works good when user swipe on scrollView. like below
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.scrollView addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.scrollView addGestureRecognizer:swiperight];
this might be happening because scroll view is rendering above self.view.

Related

How to get zoom out from zoomed image in ios

I have code like this..
in loadView()
I have created the scrollview like this with no.of taps and max value
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.delegate = self;
self.scrollView.maximumZoomScale = 2;
self.scrollView.autoresizingMask = self.view.autoresizingMask;
[self.view addSubview:self.scrollView];
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[tapOnce setNumberOfTouchesRequired:1];
[doubleTap setNumberOfTapsRequired:2];
[tapOnce requireGestureRecognizerToFail:doubleTap];
[self.scrollView addGestureRecognizer:tapOnce];
[self.scrollView addGestureRecognizer:doubleTap];
Method implementation:
- (void) handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
//single tap in full screen mode it will dismiss the view
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
//double tap will zoom the view to scrollview max value
[self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:NO];
}
Now I am able to "zoom" on tapping to full screen image and it will go in maxZoom value, but how to come back from zoomed to min value on double tap on the same, because I have functionality to dismiss the view on single tap. I need to handle one more double tap in the "handledoubleTap" method itself.
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
if ( flag == 1 ){
flag = 0;
[self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:NO];
}
else {
flag = 1;
[self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:NO];
}
}
In the above method just set flag according to your need. Hope this helps.. :)

How to swipe more then 3 UIViews on one ViewController?

i want to swipe more than 3 views on one view controller using swipe Gesture.i am fresher on objective-c. this below code is to swipe 3 views only i want to swipe more views in sequence
- (void)ViewDidLoad
{
// creating custom First view and adding on view
firstFiveQuesView=[[UIView alloc]init];
firstFiveQuesView.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+54, self.view.frame.size.height, self.view.frame.size.width);
firstFiveQuesView.backgroundColor=[UIColor brownColor];
[self.view addSubview:firstFiveQuesView];
UILabel *Q1Label=[[UILabel alloc]init];
Q1Label.frame=CGRectMake(firstFiveQuesView.frame.origin.x+20, firstFiveQuesView.frame.origin.y,400, 20);
Q1Label.backgroundColor=[UIColor grayColor];
Q1Label.text=#"Q1: qqewuiqy qoeyq ooghj qyouiye o y";
[firstFiveQuesView addSubview: Q1Label];
// similarly second view created and added on main view...
// similarly third view created and added on main view...
// Swipe Gesture adding on the views
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];
[firstFiveQuesView addGestureRecognizer:swipeLeft];
[thirdFiveQuesView addGestureRecognizer:swipeRight];
}
// swipe left or right below
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe
{
// What we do write now:here
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(#"Right Swipe");
}
}
please help me with some samples and tutorials
Thanks in advance.....................
its better to add these 3 views in to one scrollView and set the contentOffset accordingly.then you can avoid adding of gestures.
use 3 views in uiscrollView with page controller
You can do this using a class called PageViewController.
This link should surely help you out.
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial

Trying to correctly move UIImageView using gestureRecognizer in iOS

I have a UIImageView that I am trying to move using a UIPanGestureRecognizer object. The UIImageView is positioned over top of a UITableView, and serves as a scrollbar. I want this UIImageView to be moved by the user up or down the UITableView, not sideways. To accomplish this, I have implemented UIGestureRecognizerDelegate, and I have the following methods:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
NSLog(#"Do I get called?");
//_startLocation is a property of type CGPoint that I declare in the .h file
_startLocation = [recognizer locationInView:_imageView];
NSLog(#"The point is: %d", _startLocation);
CGRect frame = [_imageView frame];
frame.origin.y += frame.origin.y - _startLocation.y;
[_imageView setFrame: frame];
_imageView.center = _startLocation;
}
The method, panGestureDetected is called from viewDidLoad as follows:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureDetected:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
panGesture.delegate = self;
[_table addGestureRecognizer:panGesture];
Unfortunately, my UIImageView moves all over the place frantically on the screen when I try to move it. I want to see a smooth scrolling UIImageView go up/down while the user drags it. Can anyone see what I am doing wrong?
In your handler method just keep this line and remove all other.
_imageView.center.y = [recognizer locationInView:[_imageView superView]].y;
You need to get location in superView, not imageView itself. And just change the y value of center.

gesture recognizers on both UITableViewcell and UITableVIew

I have a cutsom UITableViewCell implemention.
I have registered this subclass of UITableViewCell for a UIPanGestureRecognizer which i use to swiping the cells to the right or left.
// in the UITableViewCell subclass :
UIGestureRecognizer* recognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:
self
action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
recognizer.cancelsTouchesInView = NO;
Now I want to to present a view controller when the user does a two finger swipe "up"
on the screen.
So, I added a UISwipeGestureRecognizer to the tableview.
// code in the view controller containing the tableview reference.
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionUp];
[swipe setDelaysTouchesBegan:NO];
[[self tableView ]addGestureRecognizer:swipe];
swipe.cancelsTouchesInView= YES;
[swipe setNumberOfTouchesRequired:2];
swipe.delegate = self;
self.tableView.multipleTouchEnabled = YES;
But when I do a two finger swipe on the screen, the pan gesture gets triggered .
How can I solve this ?
As sooper's says, setting the maximumNumberOfTouches = 1 will probably work.
For others trying to deal with 2 gestureRecognizers at the same time that are both 1 touch gestures I found that making sure to set this delegate to yes
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
and then in the gesture recognizer action you can check for a certain translation or whatever you need and cancel one of the gesture recognizers.
Such as:
- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.superview];
if(panRecognizer.state == UIGestureRecognizerStateBegan)
{
if(fabsf(translation.x) < fabsf(translation.y))
{
//deactivate horizontal gesture recognizer
panRecognizer.enabled = NO;
panRecognizer.enabled = YES;
}
else //if(fabsf(translation.x) > fabsf(translation.y))
{
//deactivate vertical gesture recognizer
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
}
//other statements like stateChanged and stateBegan
}

Capture only UIView 2 finger UIPanGestureRecognizer

I have a couple of UIScrollViews in my view controller. I want to overlay a view that captures a 2 finger swipe via UIPanGestureRecognizer which will not record the UIScrollView swipe gestures.
When I put a transparent view over my content with a 2 finger pan gesture, my taps and 1 finger swipes are not detected. I tried overwriting the pointInside: method to return NO
but then it doesn't record my 2 finger swipe.
The effect is similar to the 4 finger swipe to change apps.
You don't need an overlay view.
First implement UIPanGestureRecognizer that will handle 2 finger pan and assign it to your view that contains UIScrollViews
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
Use UIGestureRecognizerDelegate to handle 2 finger pan with UIScrollView pan gesture
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And finally you are able to handle 2 fingers pan
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
NSLog(#"pan");
}
If you want to stop scrolling UIScrollView when two finger pan is detected you can disable and enable UIScrollView pan recognizers
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
_scrollView.panGestureRecognizer.enabled = NO;
}
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
_scrollView.panGestureRecognizer.enabled = YES;
}
NSLog(#"pan");
}
If you don't really need the overlay you can solve this with just gesture recognizers. I wrote this up as a test:
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
[green setBackgroundColor:[UIColor greenColor]];
UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
[blue setBackgroundColor:[UIColor blueColor]];
[_scrollView addSubview:green];
[_scrollView addSubview:blue];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(twoFingerPan:)];
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
[pan setDelaysTouchesBegan:YES];
[_scrollView addGestureRecognizer:pan];
[self.view addSubview:_scrollView];
}
- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
self.scrollView.scrollEnabled = NO;
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateFailed:
self.scrollView.scrollEnabled = YES;
break;
default:
break;
}
NSLog(#"2 Fingers!");
}
I get the twoFingerPan: call back for when 2 fingers are used. The scroll view's panGestureRecognizer is still working at that point so I disable scrolling on the scroll view to handle the 2 finger pan. I've found this method work's pretty well. One sort of wonky thing is if the scroll view is decelerating the 2 finger gesture recognizer isn't called. Hope that helps!

Resources