0

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

        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 正确触发。

4

2 回答 2

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];
    }
}
于 2013-05-17T08:05:16.557 回答
-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];
    }
}
于 2013-05-17T08:09:53.873 回答