关于IOS-移除imageView 的问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

展开
收起
爵霸 2016-03-23 08:33:07 2919 0
1 条回答
写回答
取消 提交回答
  • 爵霸
    -(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];
        }
    }
    2019-07-17 19:10:11
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
From Java_Android to Swift iOS
立即下载
From Java/Android to Swift iOS
立即下载
深入剖析 iOS 性能优化
立即下载