Search code examples
objective-cnstimersubviewcgrect

Removing objects from superview by clicking on subviews


I have a UIViewController which places it's view on the screen and when it loads it creates an array of CGRects. It then adds those CGRects onto its view as subviews by filling them with a UIView.

I then create another CGRect and place it within one of these subviews and fill it with a UIImageView. It's set up to use CGRectContainsPoint and if the point is within one of the UIView CGRects it passes data to that object saying that it is there.

I can then click the UIView and it will use handleTap: to remove the origin point of the UIImageView rect from the super view.

Now the problem, I recently implemented a NSTimer and have it dropping new UIImageViews in at certain times. I also used the CGRectContainsPoint for loop to iterate through the array of UIView rectangles to check for UIImageView rectangles within them every one second.

When I run the program, I can click to remove the initial one, but when I click another it may or may not remove itself, I don't know why. However, sometimes when I click the first ones blank square it will remove one of the other ones and vice versa.


Solution

  • Im not so sure about your problem. But i guess that you want to creat a view and when u click it ,it will be disappeared itself? And u try to add many of this views but it doesnt work directly u think. See if i can help. you can creat a news class extends to UIView and add an tapGesture to it, which will refer to a method -(void)removeItSelf:(id)sender;

    -(void)removeItself:(id)sender
    {
       [self removeFromSuperView];
    
    }
    

    and create any instance of this view, it will be removed no metter how many u create.