Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring UIImageView on top of the other UIImageViews in my main view

I have a lot of UIImageViews in my main view, some of these have images, others are blank. I have setup code that will check which UIImageView currently contains an image. At the same time, this code will take care of allowing a UIImageView with an image to be moved around.

Now what happens is this: when moving a selected UIImageView around (for some reason and quite randomly), the image will not stay on top of the other UImageViews in the screen. The reason why I say that this is random is because it will stay on top of some of the other views, but not on top of others.

The behavior is unexpected, several problems arise:

  1. Visually it looks bad; there is no reason why a touched UIImageView should slip under another.
  2. The way I have the code going is to allow UIImageViews to be moved only if they contain an image. So if the UIImageView goes under another who does not contain an image, I cannot touch and move it again. It looks like it is stuck in place.

Please note that I have not been setting subviews at all for this code, thus why this behavior occurs is beyond me.

So what my question boils down to, is there any way that I can tell the code to:

  • Get the object that was touched.
  • If it is a UIImageView with an image, then allow me to move the UIImageView.
  • allow this UIImageView to supersede (be on top of) all other UIImageViews.

Code reference:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{UITouch *touch;
    UITouch *touch;    
    CGPoint touchLocation;

    for (UIImageView *fruit in fruitArray)
    {
        // Check that the UIImageView contains an image
        if([fruit image] != nil)
        {
            // Get the touch event.
            touch = [touches anyObject];
            // Get the location for the touched object within the view.
            touchLocation = [touch locationInView:[self view]];

            // Bring the UIImageView touch location to the image's center.
            if([touch view] == fruit)
            {
                fruit.center = touchLocation;
            }
        }
    }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    //allow the selected event (in our case a UIImageView) to be dragged
    [self touchesBegan:touches withEvent:event];
}

Thank you for your help! Let me know if you need a better explanation

like image 706
AGE Avatar asked Dec 09 '25 00:12

AGE


1 Answers

[self.view bringSubviewToFront:imageView];

This was what you were looking for...

like image 139
lakshmen Avatar answered Dec 11 '25 12:12

lakshmen