gpt4 book ai didi

ios - 在同一个 UIImageView 上处理/检测 UIPanGestureRecognisor 和 UISwipeGestureRecognisor

转载 作者:行者123 更新时间:2023-11-29 02:49:12 25 4
gpt4 key购买 nike

我有一个图片库,如果用户单击图片,它会出现在全屏 ImageView 中,

现在如果用户使用 UIPinchGesture 图片可以完美放大/缩小。

如果图片未缩放,我想实现 UISwipeGesture 应该工作,并且在滑动手势上调用图库中的下一张图像。

如果图片被缩放,则 swipeGesture 停止工作,而 UIPanGesture 应该工作。

实现它的最佳方法是什么,

我是这样做的。但总是混合使用 PanGesture 和 SwipeGesture。

我的滑动处理方法

- (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;
}
}

我的夹点处理方法

-(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);
}

我的 Pan 处理方法

-(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;
}
}

我尝试过使用或不使用这种同时使用多种手势的方法,但都没有成功。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

最佳答案

我从你的问题中得出的是:你想在相同的UIImageView上但在特定条件下使用平移和滑动手势。

如果我说的是一个懒人,只需首先获取 iVar 中图像的实际大小,然后检查两个委托(delegate)方法:

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.

我发帖只是为了让您继续下去。

抱歉没有发布代码,实际上面临着电力短缺和笔记本电脑电池即将耗尽的情况。 :P

关于ios - 在同一个 UIImageView 上处理/检测 UIPanGestureRecognisor 和 UISwipeGestureRecognisor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com