gpt4 book ai didi

xcode - UIImageView 的平移手势干扰滚动

转载 作者:行者123 更新时间:2023-12-04 05:02:36 25 4
gpt4 key购买 nike

我想实现对 ImageView 的平移手势功能,这个 ImageView 是 ScrollView 的 subview 。问题是当我尝试滚动 ScrollView 时, ImageView 的平移手势被识别,并且 ScrollView 不滚动。
请帮我解决一下这个。

这是将 pan 初始化为 uiimageview 的代码:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[[[scrollView subviews] objectAtIndex:i] addGestureRecognizer:panRecognizer];

imageview 将用户设置为“YES”

以及为此采取的行动
-(void)move:(UIPanGestureRecognizer *)sender
{
[[[[[sender view] superview] superview] superview] bringSubviewToFront:sender.view];


if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

if ([[[sender view] superview] superview] == itemView) // adds the imageview in the item scroll
{
int tag = [[sender view] tag] / 100;
UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

CGAffineTransform transform = temp.transform;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage: [(UIImageView *) [sender view] image]];
tempImageView.tag = [[sender view] tag];
//NSLog(@"[sender view] superview] : %@", [[sender view] superview]);
tempImageView.frame = frame;
[tempImageView setTransform:transform];
[tempImageView setFrame:frame];
[tempImageView setUserInteractionEnabled:YES];
[tempScroll addSubview:tempImageView];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[tempImageView addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[tempImageView addGestureRecognizer:singleTapGestureRecognizer];
}
}

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged)
{
CGPoint translatedPoint = [sender locationInView:[[sender view] superview]] ;
newTranslatedPoint = [[[sender view] superview] convertPoint:translatedPoint toView:self.view];

if ([sender view].frame.size.width == 35 && [sender view].frame.size.height == 35)
[[sender view] sizeToFit];

[sender view].center = CGPointMake(newTranslatedPoint.x, newTranslatedPoint.y); //startFrame;

[self.view addSubview:[sender view]];
}

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
CGPoint newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView];
if (newPoint.y < 0)// || !viewUp)
{
//NSLog(@" baseView");
[baseView addSubview:[sender view]];

UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTapGestureRecognizer setNumberOfTapsRequired:2];
[[sender view] addGestureRecognizer:doubleTapGestureRecognizer];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[[sender view] addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[[sender view] addGestureRecognizer:rotationRecognizer];
}

else if (newPoint.y > 0)// && viewUp )
{
//NSLog(@" itemView");
int tag = [[sender view] tag] / 100;
UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

CGAffineTransform transform = temp.transform;
[[sender view] setTransform:transform];
[[sender view] setFrame:frame];
[tempScroll addSubview:[sender view]];
}
}
}

最佳答案

我不确定我是否完全理解您通过使用 UIPanGestureRecognizer 想要达到的目的。在 UIImageView已经在 UIScrollView 中.但从你的描述来看,听起来你想要 UIScrollView做这件事并为UIPanGestureRecognizer不要阻止它。我相信你需要为你的 UIPanGestureRecognizer 实现这个委托(delegate)方法。 :

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

请阅读 UIGestureRecognizerDelegate Protocol reference关于允许同时进行手势识别。根据文档:

The default implementation returns NO—no two gestures can be recognized simultaneously.



/* 编辑 */

来自 UIPanGestureRecognizer - Only vertical or horizontal:
只为 UIPanGestureRecognizer 实现一个方向.这是垂直平移:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint translation = [panGestureRecognizer translationInView:someView];
return fabs(translation.y) > fabs(translation.x);
}

关于xcode - UIImageView 的平移手势干扰滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15963387/

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