我在屏幕上有一张照片,当我按住时,我想以全尺寸显示。这是手势识别器:
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正确触发。
答案 0 :(得分:2)
这是因为您正在创建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];
}
}
答案 1 :(得分:-1)
您无需在动作的动作中创建新的imageview
。只需添加或删除此类self.photoImageView
-(void)hold:(UILongPressGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
NSLog(@"state began");
[self.view addSubview:self.photoImageView];
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
NSLog(@"state ended");
[self.photoImageView removeFromSuperview];
}
}