UIImageView 无法识别第二次手势

发布于 2024-12-11 04:00:02 字数 1831 浏览 0 评论 0原文

我已经在 UIImageView 上实现了 UITapGestureRecognizer,它在第一次点击时就可以工作。在第一次点击时,我隐藏该图像并开始动画。动画完成后,我将再次显示图像。但是设置 setHidden:FALSE 后,我没有收到该 UIImageView 的 Tap 事件。

以下是我正在使用的代码:

- (void)viewDidLoad{

[super viewDidLoad];

defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:@"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];


[self.view addSubview:defaultDogView];
}

- (void)addGestureRecognizersToPiece:(UIImageView *)piece 
{
NSLog(@"in Gesture");
   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapPiece:)];
[tapGesture setDelegate:self];

[piece addGestureRecognizer:tapGesture];
[tapGesture release];

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];

NSLog(@"%@", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(@"Image Tapped");

/** Hide the default Image and start the animation ***/

[defaultDogView setHidden:TRUE];

/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:@"dog" withDuration:(12) ofType:@"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];



[self performSelector:@selector(callBubbleUpdater) withObject:nil afterDelay:5.5];


}
-(void)showDogFrame{
NSLog(@"%@",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;

}

I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.

Following is the code I am using :

- (void)viewDidLoad{

[super viewDidLoad];

defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:@"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];


[self.view addSubview:defaultDogView];
}

- (void)addGestureRecognizersToPiece:(UIImageView *)piece 
{
NSLog(@"in Gesture");
   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapPiece:)];
[tapGesture setDelegate:self];

[piece addGestureRecognizer:tapGesture];
[tapGesture release];

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];

NSLog(@"%@", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(@"Image Tapped");

/** Hide the default Image and start the animation ***/

[defaultDogView setHidden:TRUE];

/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:@"dog" withDuration:(12) ofType:@"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];



[self performSelector:@selector(callBubbleUpdater) withObject:nil afterDelay:5.5];


}
-(void)showDogFrame{
NSLog(@"%@",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-12-18 04:00:02

view处于隐藏或其alpha组件时,该视图将不会接收任何UIGestureRecognizers< /代码>。

如果您需要隐藏某些视图(让我们将其命名为 touchableView)但希望它响应手势,我建议使用下一种方法:

  1. 使用相同的方法创建 backgroundView框架为 touchableView

    UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];

  2. 设置backgroundView的背景颜色为clearColor

    backgroundView.backgroundColor = [UIColor clearColor];

  3. 重置touchableView的位置:

    CGRect框架=touchableView.frame;
    框架.origin.x = 0;
    frame.origin.y = 0;

  4. 禁用touchableView的用户交互:

    touchableView.userInteractionEnabled = NO;

  5. 添加touchableView 作为 backgroundView 的子视图:

    [backgroundView addSubview:touchableView];

  6. backgroundView 添加适当的手势识别器。

  7. 添加 backgroundView 以查看您想要的视图。

现在您可以隐藏 touchableView 但您仍然会收到手势识别器。

我没有测试这个,但我认为它应该有效。

When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.

I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:

  1. Create backgroundView with the same frame as touchableView:

    UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];

  2. Set background color of backgroundView to clearColor:

    backgroundView.backgroundColor = [UIColor clearColor];

  3. Reset position of touchableView:

    CGRect frame = touchableView.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;

  4. Disable user interaction of touchableView:

    touchableView.userInteractionEnabled = NO;

  5. Add touchableView as subview to backgroundView:

    [backgroundView addSubview:touchableView];

  6. Add appropriate gesture recognizers to backgroundView.

  7. Add backgroundView to view that you want.

Now you can hide touchableView but you will still receive gesture recognizers.

I don't test this but I think it should work.

夏花。依旧 2024-12-18 04:00:02

确定

UIImageView 何时隐藏。它不接收任何触摸事件,

为 uiimageview 设置 alpha 零

sure

when UIImageView is hidden. it does not receive any touch events

set alpha zero for uiimageview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文