我正在开发一个需要 uiimageviews 的纸牌游戏。我有一个基于视图的应用程序。我在其中添加了 5 个图像视图。
每个 imageview 都有一张独特的卡片。nw 用户可以将一张卡片拖到存在的任何其他卡片上。为此,我需要知道选择了哪张卡。这该怎么做?
在我看来,启用触摸功能在所有地方都有效。我希望仅在 uiimageview 上启用触摸。
我怎样才能激活对我的视图控制器中单独存在的图像视图的触摸。
提前致谢..
我正在开发一个需要 uiimageviews 的纸牌游戏。我有一个基于视图的应用程序。我在其中添加了 5 个图像视图。
每个 imageview 都有一张独特的卡片。nw 用户可以将一张卡片拖到存在的任何其他卡片上。为此,我需要知道选择了哪张卡。这该怎么做?
在我看来,启用触摸功能在所有地方都有效。我希望仅在 uiimageview 上启用触摸。
我怎样才能激活对我的视图控制器中单独存在的图像视图的触摸。
提前致谢..
您可以在 touchesBegan: 方法中检查视图中的哪个位置被选中
CGPoint point = [recognizer locationInView:self];
然后做一个for循环来检查像这样选择了哪个imageview
for(i = 0; i < 5; i++)
{
if(CGRectContainsPoint(imageView.frame, point))
{
// do something.
}
}
UITouch *touch = [touches anyObject];
CGPoint offSetPoint = [touch locationInView:myImageView];
现在你只定义了 myImageView 对象来接收触摸。根据您的需要将这些行写入 touchesBegan 或 touchesEnded 方法...希望这会有所帮助....不要忘记将 imageview 的 userInteractionEnabled 属性设置为 YES 以使触摸工作。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pnt = [[touches anyObject]locationInView:self.view];
UIImageView *imgView;
if((pnt.x > CGRectGetMinX(imgView.frame) && pnt.x < CGRectGetMaxX(imgView.frame))&&
(pnt.y > CGRectGetMinY(imgView.frame) && pnt.y < CGRectGetMaxY(imgView.frame)) &&
ChkOfferCount==TRUE)
{
UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Image Touched"
message:@"Selected image is this" delegate:nil
cancelButtonTitle:@"cancel"
otherButtonTitles:@"ok",nil];
[obj show];
[obj release];
}
}
设置imageView.userInteractionEnabled=YES;
,您可以使用 imageView 本身的 touches 方法,例如,touchesBegan:
您可以使用这些方法来处理卡片的拖放。
您还可以创建 UIControl 视图,然后用 UIImageView 完全填充它,然后简单地在 UIControl 中捕获触摸事件。