我的主视图中有很多 UIImageViews
,其中一些有图像,另一些是空白的。我有设置代码来检查哪个 UIImageView
当前包含图像。同时,此代码将负责允许带有图像的 UIImageView
移动。
现在发生的事情是这样的:当移动选定的 UIImageView
时(出于某种原因并且相当随机),图像将不会停留在其他 UImageViews
的顶部屏幕。我之所以说这是随机的,是因为它会停留在其他一些 View 之上,而不是在其他 View 之上。
行为出乎意料,出现了几个问题:
- 视觉上看起来很糟糕;触摸的
UIImageView
没有理由滑到另一个下面。 - 我使用代码的方式是仅当它们包含图像时才允许移动
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/