我的主视图中有很多UIImageViews,其中一些包含图片,其他为空白。我有安装代码,它将检查当前哪个UIImageView包含图像。同时,此代码将确保允许将带有图像的UIImageView移动。

现在发生的事情是:在移动选定的UIImageView时(由于某种原因并且是相当随机的),图像将不会停留在屏幕上其他UImageViews的顶部。我之所以说这是随机的,是因为它将停留在其他一些视图之上,而不是其他视图之上。

该行为是意外的,会出现一些问题:

  • 看起来很糟糕;没有理由为什么被触摸的UIImageView应该滑到另一个。
  • 使代码运行的方式是仅当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];
    }
    

    谢谢您的帮助!让我知道您是否需要更好的解释

    最佳答案

    [self.view bringSubviewToFront:imageView];
    

    这就是您想要的...

    10-06 05:24
    查看更多