在我的主视图中将UIImageView置于其他UIImageViews之上

AGE*_*AGE 2 iphone xcode objective-c uiimageview ios

UIImageViews在主视图中有很多,其中一些有图像,另一些是空白.我有设置代码,将检查UIImageView当前包含图像的内容.同时,此代码将负责允许UIImageView移动图像.

现在会发生这样的事情:当移动选定的UIImageView周围(由于某种原因并且相当随机)时,图像将不会UImageViews在屏幕中保持在另一个上面.我之所以说这是随机的,是因为它会保留在其他一些视图的顶部,但不会在其他视图之上.

行为是出乎意料的,出现了几个问题:

  1. 视觉上它看起来很糟糕; 没有理由为什么触摸UIImageView应该滑到另一个之下.
  2. 我有代码的方式是只允许UIImageViews在它们包含图像时移动.因此,如果UIImageView在另一个不包含图像的人之下,我无法触摸并再次移动它.它看起来像是卡在原地.

请注意,我没有为此代码设置子视图,因此出现这种情况的原因超出了我的范围.

那么我的问题归结为,有什么方法可以告诉代码:

  • 获取被触摸的对象.
  • 如果它是UIImageView带图像的,那么请允许我移动UIImageView.
  • 允许它UIImageView取代所有其他的(在其上)UIImageViews.

代码参考:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!如果您需要更好的解释,请告诉我

lak*_*esh 10

[self.view bringSubviewToFront:imageView];
Run Code Online (Sandbox Code Playgroud)

这就是你要找的......