gpt4 book ai didi

iphone - 无法从 UIGestureRecognizer 中删除 imageView?

转载 作者:行者123 更新时间:2023-11-29 03:55:05 25 4
gpt4 key购买 nike

我的屏幕上有一张照片,当我拿着它时,我想以全尺寸显示。这是手势识别器:

        UILongPressGestureRecognizer *hold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hold:)];
hold.minimumPressDuration = 0;

[self.photoImageView addGestureRecognizer:hold];

这是监听器:

-(void)hold:(UILongPressGestureRecognizer *)sender{
UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.userInteractionEnabled = NO;

if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"state began");
[self.view addSubview:img];
} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"state ended");
[img removeFromSuperview];
}
}

图像已正确添加到屏幕,但是 [img removeFromSuperview] 似乎没有响应。发生这种情况有什么原因吗?结束状态的 NSLog 已正确触发。

最佳答案

这是因为您正在创建 UIImageView 的新实例,而不是删除显示的实例。

-(void)hold:(UILongPressGestureRecognizer *)sender{

if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"state began");

UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.tag = IMAGE_VIEW_TAG;
img.userInteractionEnabled = NO;
[self.view addSubview:img];

} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"state ended");
UIImageView *img = [self.view viewWithTag: IMAGE_VIEW_TAG];
[img removeFromSuperview];
}
}

关于iphone - 无法从 UIGestureRecognizer 中删除 imageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604249/

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