【发布时间】:2013-06-12 16:42:24
【问题描述】:
我的主视图中有很多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];
-
好伙伴!介意把这个作为答案吗?我想给你功劳
-
@Vin 这也太棒了,谢谢你的贡献,从现在开始我会用这个方法。
标签: iphone ios objective-c xcode uiimageview