for_2012
2013-05-20 08:07
采纳率: 70.7%
浏览 4.0k

IOS-移除imageView

屏幕上有一张图片需要在停留的时候全屏。

    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也正常。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • AldisZhan 2013-05-20 15:53
    最佳回答
    -(void)hold:(UILongPressGestureRecognizer *)sender{
        //每次响应函数 img都是一个新alloc出来的UIImageView
        UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
        img.userInteractionEnabled = NO;
    
        if(sender.state == UIGestureRecognizerStateBegan){
            NSLog(@"state began");
            //添加新alloc出来的UIImageView,可以看到界面效果
            [self.view addSubview:img];
        } else if (sender.state == UIGestureRecognizerStateEnded){
            NSLog(@"state ended");
           //remove 新alloc出来的UIImageView当然没有效果,它还没有被add进去
            [img removeFromSuperview];
        }
    }
    

    改成这样

    -(void)hold:(UILongPressGestureRecognizer *)sender{
        if(sender.state == UIGestureRecognizerStateBegan){
            NSLog(@"state began");
            UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
            img.userInteractionEnabled = NO;
            img.tag = kTAG_PHOTO_IMAGE_VIEW
            [self.view addSubview:img];
        } else if (sender.state == UIGestureRecognizerStateEnded){
            NSLog(@"state ended");
            UIImageView *img = (UIImageView *)[self.view viewWithTag:kTAG_PHOTO_IMAGE_VIEW];
            [img removeFromSuperview];
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题