removeFromSuperview not working

巧了我就是萌 提交于 2019-12-25 03:28:24

问题


To my view I add imageView but after I remove it this image stay on view, how remove it ?

Create:

(void)handleTapHold:(UILongPressGestureRecognizer *)gestureRecognizer{
UIImageView *pinkIm = [[UIImageView alloc]initWithFrame:CGRectMake(self.roundView.frame.origin.x + 70, self.roundView.frame.origin.y - 70, 60, 60)];
[pinkIm setImage:[UIImage imageNamed:@"pink_print"]];
pinkIm.layer.cornerRadius = pinkIm.frame.size.height / 2;
pinkIm.layer.borderWidth = 3.0f;
pinkIm.layer.borderColor = [UIColor whiteColor].CGColor;
pinkIm.clipsToBounds = YES;

[self.view addSubview:pinkIm];

 if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
  [pinkIm removeFromSuperview];
 }
}

later in same method I try to Remove but nothing happen:

 [pinkIm removeFromSuperview];

Find my problem, in start I don't add:

if (gestureRecognizer.state == UIGestureRecognizerStateBegan){

回答1:


It might be that by the time you try to remove it, *pinkIm is already nil - could you post the rest of the code so that we can make sure the pointer pinkIm is not nil?

In the case that it is nil, and there is nothing you can do about it, the other way to remove it would be to find it through the current view's children. Example:

for(UIView *child in self.children){
if([child isKindOfClass: [UIImageView class]]){
[child removeFromSuperView];
break;}}

Note: The above will not work if you have several image views. In that case, you may want to keep checking the state of "child" to ensure that you are talking about what *pinkIm was pointing to.

Please post rest of code of the method!




回答2:


Be sure that you're triggering the method on the main thread, try this:

 if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
         [pinkIm removeFromSuperview];
    })
 }


来源:https://stackoverflow.com/questions/25413666/removefromsuperview-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!