Using swipe gesture within UIScrollView without disabling horizontal and vertical scrolling - iphone

I looked through other solutions, yet I can't find the right solution for myself.
I have a UIImageView within a UIScrollView, where I show big pictures.
I have pinch gesture enabled within my UIScrollView as well as left and right swipe gesture recognizers.
Right now, scrollview's pan gesture seems to disable, (or corrupt) swipe gestures. I don't want to disable horizontal or vertical scrolling on my UIScrollView, and initial scaling of my photos are too large to disable horizontal scrolling.
What I want to do is to trigger swipe gesture when i come at the edge of my UIScrollView.
Here are some codes;
- (void)viewDidLoad
{
// recognizer for pinch gestures
UIPinchGestureRecognizer *pinchRecognizer =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(handlePinch:)];
[self.myScrollView addGestureRecognizer:pinchRecognizer];
[self.myScrollView setClipsToBounds:NO];
// recognizer for swipe gestures
UISwipeGestureRecognizer *recognizer;
// left and right swipe recognizers for left and right animation
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self myScrollView] addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self myScrollView] addGestureRecognizer:recognizer];
....
My left swipe handler, currently left and right swipes don't have any additional features
-(void)handleLeftSwipe:(UISwipeGestureRecognizer *)recognizer
{
if(!self.tableView.hidden) self.tableView.hidden = YES;
[self showRequiredStuff];
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
My initial screen size;
#define IMAGE_VIEW_WIDTH 320.0
#define IMAGE_VIEW_HEIGHT 384.0
I use scaling for pictures, to make them scale as small as they can be, but most of them are wide images, which case horizontal scrolling is enabled, and vertical scrolling is disabled. Although my swipe handlers are horizontal as well.
I suppose I have clearly explained what is going on and what I need. I have posted codes because I am a newbie on iphone application development, I both want to help other people to see as much code as they can, and maybe someone will point out any bad programming, and we all will benefit from it.
Additional findings from related solutions;
After setting;
#interface myViewController () <UIScrollViewDelegate>
and
self.myScrollView.delegate = self;
Detecting if a the edge is reached, horizontally
- (BOOL) hasReachedAHorizontalEdge {
CGPoint offset = self.myScrollView.contentOffset;
CGSize contentSize = self.myScrollView.contentSize;
CGFloat height = self.myScrollView.frame.size.height;
CGFloat width = self.myScrollView.frame.size.width;
if ( offset.x == 0 ||
(offset.x + width) == contentSize.width ) {
return YES;
}
return NO;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
if ( [self hasReachedAHorizontalEdge] ) {
NSLog(#"Reached horizontal edge.");
// required here
}
}
At this point, all I need to disable the scrolling at the reached end, etc. if I reached right edge of scroll, I need to disable only right scrolling, that way swipe will be triggered.

I added a thin UIView over the edges of my scrollView and have them recognize swipes. I could not get the scrollView to cooperate with another swipe gesture recognizer. This isn't ideal, but it works for now. I will have to investigate if there is a way to override the scrollView's swipe gesture recognizer.
The trick would be to check and see if the swipe starts near the edges, and then to decide whether to call the scrollView's or my swipe recognizer.

Related

Presenting ViewController from Top to Bottom Swift

I have googling a while for a solution. But didn't get anything what I needed. later I found a Library that works the same but in a reverse manner and the library is this LNPopupController
My Question is how Can I make a viewcontroller move from top to bottom on swipe Gesture ? Can I tweak this library to achieve it ?
You're looking to implement a UIPanGesture rather than a UISwipeGesture based of the project you linked.
Create a UIPanGestureRecognizer
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(viewDragged:)];
[yourCustomView addGestureRecognizer:panGestureRecognizer];
Then implement the "viewDragged:" method.
heightForTheDraggableRegionLeftInTheView is a value of how much of the view you want to leave on the screen when the user tries to dismiss youCustomView.
- (void) viewDragged: (UIPanGestureRecognizer *) gestureRecognizer {
CGRect viewFrame = gestureRecognizer.view.frame;
CGPoint locationOfTouch = [gestureRecognizer locationInView:self.view];
// Let the user's finger match with the bottom of the NotificationView
[gestureRecognizer.view setFrame:CGRectMake(viewFrame.origin.x, locationOfTouch.y + draggableHeightOfTheNotificationView - viewFrame.size.height, viewFrame.size.width, viewFrame.size.height)];
// If the yourCustomView is being dragged too high, make sure you leave a draggable area so that the user can drag it later
if (locationOfTouch.y >= self.view.frame.size.height - heightForTheDraggableRegionLeftInTheView) {
[gestureRecognizer.view setFrame:CGRectMake(viewFrame.origin.x, 0, viewFrame.size.width, viewFrame.size.height)];
}
// If the NotificationView will go to far below the screen keep it resting at the bottom of the screen
if (locationOfTouch.y <= draggableHeightOfTheNotificationView) {
[gestureRecognizer.view setFrame:CGRectMake(viewFrame.origin.x, draggableHeightOfTheNotificationView * 2 - self.view.frame.size.height, viewFrame.size.width, viewFrame.size.height)];
}
}

Pinchgesture not working for small images

Im using GestureRecognizer delegate for pinch,pan,rotate,longpress for images. I used UIPinchGestureRecognizer delegate for pinching.
But, when i pinch zoomIn it doesn't have any problem. When i zoomOut certain level the images are small, and i can't ZoomIn pinching the images. After that, when i apply pan, the pan is applying whole view and only the image while i release my finger. After release my finger,the pan is apply only image. After touch the image pan apply on whole view
code:
UIPinchGestureRecognizer *pinchGesture1 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(ahandlePinch1:)];
[myImageView addGestureRecognizer:pinchGesture1];
-(void)ahandlePinch1:(UIPinchGestureRecognizer*)sender {
mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];
if (sender.state == UIGestureRecognizerStateEnded)
{
mLastScale = 1.0;
}
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mCurrentScale, mCurrentScale);
myImageView.transform = newTransform;
}
You should modify your ahandlePinch1 method so that you don't reduce the size of the image below a certain amount. It is almost certainly getting so small that it can no longer detect two distinct touches (which are required for the pinch gesture).
Apple generally recommend allowing a minimum of 44x44 pts as a touchable area, so I would suggest you stop your image from resizing below 88x88.
Alternatively, if you actually need your image to be less than that then you should add the gesture recognizer to a different view (perhaps the superview), rather than the image itself.

UIScrollView and detecting subview of tap gesture

I've added a TapGestureRecognizer to my self.view:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
[tap release];
The view contains a single UIScrollView with images and labels. I want to detect if the user taps on a label or not.
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
UIView *v = [self.view hitTest:pt withEvent:nil];
if ([v isKindOfClass:[UILabel class]]) {
NSLog(#"label!");
return;
}
// else do other stuff if its not a label
However I don't see the label! in my log.
I think it's because userInteractionEnabled is by default NO on UILabels. Try turning that on.
EDIT: It was really a guess, but just to confirm, Apple docs on [UIView hitTest:withEvent:] state:
This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.
Your subviews, such as the labels themself, actually hide the user interactions from the underlying view.
Why don't you add the gesture recognizers to your label(s).
Alternatively you may want to use UIButton for the labels.
Or -
if you do not want to determine which label has been touched, you may want to add an invisible view (an empty view, neither a hidden one nor one with alpha=0) on top of all labels and add the gesture recognizers to those.

Move a button in the screen

I have an UIButton and I want to move that button by touching and swiping it in the screen. When I release the touch it will be in the current position. Explain clearly, please.
You can move a view by using touch moved event. There is a sample tutorial MoveMe by Apple which drags a view and after releasing the touch animate the view. Check specially the touch events (touchesBegan, touchesMoved, touchesEnded) in MoveMeView.m to get the idea how they have moved placardView. You can move your button just like the placardView.
Taken from 'drag' move a uibutton so it acts like uiScrollView
check this
you should make move frame from the points and move frame accordingly so that your button moves in places of touches
If you are scripting for iOS 3.2 and above, consider using UIPanGestureRecognizer.
Simply attach an instance of it like this,
...
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
[self.button addGestureRecognizer:panGesture];
[panGesture release];
...
and define handlePan: like this,
- (void)handlePan:(UIPanGestureRecognizer *)panGesture {
CGRect buttonFrame = self.button.frame;
CGPoint translation = [panGesture translationInView:panGesture.view];
buttonFrame.origin.x += translation.x;
buttonFrame.origin.y += translation.y;
[panGesture setTranslation:CGPointMake(0, 0) inView:panGesture.view];
self.button.frame = buttonFrame;
}

UISwipeGestureRecognizer not working

I have a UIView inside of a UIScrollView, both created using IB. The UIView scrolls horizontally inside the UIScrollView. I want to detect left and right 2 finger swipes.
Borrowing from the sample code I found in SmpleGestureRecognizers, I have put the following code in the viewDidLoad method of the UIScrollView's ViewController...
UIGestureRecognizer *recognizer;
UISwipeGestureRecognizer *swipeRightRecognizer, *swipeLeftRecognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRightRecognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.numberOfTouchesRequired = 2;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
[recognizer release];
I have set in the viewcontroller.h. and have the following delegate method...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
I am assuming this is a valid gestureRecognizer delegate method, but I cannot find any reference to it in the documentation.
I do not get any errors but nothing happens when I do a 2 finger swipe. The delegate method is not called and neither is my action method. I tried removing the numbeOfTouchesRequired call to see if it might work with a single finger swipe to no avail.
Am I adding the gestureRecognizers to the right view? I tried adding it to the UIView, the UIScrollView as well as self.view.superView.
The sample code runs great. The only difference I can see between my implementation of the code and the sample code is the fact that I used IB to create the views and the sample code did not. I suspect that something is consuming the swipe gesture before it gets to my recognizers.
What am I doing wrong.
Thanks,
John
I had the same problem and I solved by using UIPanGestureRecognizer instead of UISwipeGestureRecognizer.
To emulate the detection of swipe, we'll play with the speed of gesture in scrollview.
If the speed of x direction >= 3000 (for example) the swipe will be detected.
If x>0 it will be a right swipe.
The code I implemented to resolve your situation is:
In a uiscrollview named _scroll1:
UIPanGestureRecognizer *pan;
pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(Swipe4ScrollViews:)];
[pan setMinimumNumberOfTouches:2];
[_scroll1 addGestureRecognizer:pan];
[pan release];
With a global BOOL variable named _panning, Swipe4ScrollViews will do the hard job:
-(void)Swipe4ScrollViews:(UIPanGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan) _panning = NO;
CGPoint v =[sender velocityInView:_scroll1];
NSLog(#"%f, %f",v.x,v.y);
if( (abs(v.x) >= UMBRAL) && !_panning)
{
_panning = YES;
[sender cancelsTouchesInView];
if(v.x>0) NSLog(#"Right");
else NSLog(#"Left");
[self doSomething];
}
}
I encapsulated it on a UIGestureRecognizer subclass: UISwipe4ScrollGestureRecognizer
The biggest difference between the sample code and your code is that your code involves a UIScrollView.
Internally, scroll views, table views, and web views all use gesture recognizers to some degree. If you're expecting to receive gestures within those views – gestures that are similar to the ones already supported internally – they will almost certainly be consumed or significantly delayed before you can get to them. Receiving gestures outside or above those views should work fine, if your use case supports it.
Try setting the delayContentTouches-property of the UIScrollView to NO, maybe it'll help.