Reputation: 2741
I have a Image Gallery, If User Single Tap On a Picture, it appeares in full screen ImageView,
Now if user Uses UIPinchGesture Picture is zoom in / Out perfectly.
I want to acheive if picture is not zoomed UISwipeGesture Should work and on Swipe Gesture Next Image from gallery is called.
if Picture is zoomed then swipeGesture stop working and UIPanGesture should work.
what will be the best approach to acheive it,
i am doing it like this. but always mixing in PanGesture and SwipeGesture.
my Swipe Handling Method
- (void)handleSwipe:(UIGestureRecognizer *)sender
{
if (stopSwipe == true) // bool iVar (stopSwipe)
{
return;
}
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction)
{
case UISwipeGestureRecognizerDirectionLeft:
newIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row+1 inSection:myIndexPath.section];
[self ChangeImage];
myIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row+1 inSection:myIndexPath.section];
break;
case UISwipeGestureRecognizerDirectionRight:
newIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row-1 inSection:myIndexPath.section];
[self ChangeImage];
myIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row-1 inSection:myIndexPath.section];
break;
default:
break;
}
}
my Pinch Handling Method
-(void)handlePinch:(UIPinchGestureRecognizer*)sender
{
static CGRect initialBounds;
if (sender.state == UIGestureRecognizerStateBegan)
{
initialBounds = myLrgImageView.bounds;
}
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
myLrgImageView.bounds = CGRectApplyAffineTransform(initialBounds, transform);
}
my Pan Handling Method
-(void)handlePan:(UIPanGestureRecognizer*)recognizer
{
//after pinch 'myLrgImageView.frame' is changed.
//imgActualBoundry is the normal size with out zooming.
if ([NSValue valueWithCGRect:myLrgImageView.frame] > [NSValue valueWithCGRect:imgActualBoundry])
{
stopSwipe = true;
CGPoint translation = [recognizer translationInView:self.myLrgImageView];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.myLrgImageView];
if (recognizer.state == UIGestureRecognizerStateEnded)
{
CGPoint velocity = [recognizer velocityInView:self.myLrgImageView];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.myLrgImageView.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.myLrgImageView.bounds.size.height);
}
}
else
{
stopSwipe = false;
}
}
i have tried with or without this method of simultanious usage of multi gestures, but didn't get success.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Upvotes: 0
Views: 92
Reputation:
What i figure out from your question is : you want to use Pan and Swipe gestures on same UIImageView
but on specific conditions.
If i am saying being just a lazy person, simply take the actual size of image in an iVar at first Then Check on both delegate methods :
if size is changed then you will surely like to use Pan gesture on same Image.
&
if size is the same as actual image then Swipe.
I am just posting as an idea to proceed you on.
Sorry for posting not the code, actually facing electricity shortfall and laptop battery is about to dead. :P
Upvotes: 1