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

标签 iphone ios objective-c xcode uiimageview

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

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

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

  1. 视觉上看起来很糟糕;触摸的 UIImageView 没有理由滑到另一个下面。
  2. 我使用代码的方式是仅当它们包含图像时才允许移动 UIImageViews。因此,如果 UIImageView 位于另一个不包含图像的 View 下方,我将无法再次触摸和移动它。它看起来像是卡在原地。

请注意,我根本没有为这段代码设置 subview ,因此我无法理解为什么会出现这种行为。

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

  • 获取被触摸的对象。
  • 如果它是带有图像的 UIImageView,请允许我移动 UIImageView
  • 允许此 UIImageView 取代(位于其上)所有其他 UIImageView

代码引用:

-(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];

这就是您要找的...

关于iphone - 在我的主视图中将 UIImageView 置于其他 UIImageView 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17070896/

相关文章:

ios - 录制时实时上传音频片段?

ios - 使用playPrerollAdWithCompletionHandler方法通过MPMoviePlayerController实现iAd视频

objective-c - Objective C 中方法语法和命名约定的混淆

iphone - 在 iPhone 上的 UIWebView 中检测 HTML5 <video> 上的触摸

iphone - UITextfield 清除按钮

iphone - iOS将实体从类添加到核心数据

ios - 表格 View 数据以特定格式显示?

objective-c - 从类方法访问属性?

ios - 使用 iBeacon 打开应用程序

iphone - 更改了iPhone应用程序目标名称,现在应用程序“运行完成”