从多个图像视图中识别特定图像视图
我有一个图像视图,并在其上设置其他图像视图并将它们放在不同的位置。我想在法师视图上应用翻转动画。怎样才能做到呢?当 if(i==1) 时,我的图像视图被翻转。 img1
是我的图像视图,我想识别它。我在`imagearr 中分配了 52 个图像
我的代码:
-(void)setCards
{
int k=0;
CGFloat dx = self.view.frame.size.width/2;
CGFloat dy = self.view.frame.size.height/2;
int cnt = [imagearr count];
for (int j=1; j<3; j++) // Number of cards
{
for (int i=1; i<5; i++) // Number of players
{
int rnd = (arc4random() % cnt)-1;
UIImage *img = [imagearr objectAtIndex:rnd];
UIImageView *img1 = [[UIImageView alloc] initWithImage:img];
CGRect frame = img1.frame;
frame.size.width = frameX;
frame.size.height = frameY;
[img1 setFrame:frame];
[img1 setTag:k+i];
[UIView beginAnimations:@"Move" context:nil];
[UIView setAnimationDuration:0.50];
if (i==1)
{
[img1 setCenter:CGPointMake(dx+100, dy+(j*15))];
}
if (i==2)
{
[img1 setCenter:CGPointMake(dx+(j*15), dy+60)];
}
if (i==3)
{
[img1 setCenter:CGPointMake(dx-130, dy-(j*15))];
}
if (i==4)
{
[img1 setCenter:CGPointMake(dx-(j*20), dy-95)];
}
[self.view addSubview:img1];
[UIView commitAnimations];
[img1 release];
}
k=k+10;
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
}
我仍然无法识别特定的图像视图。请帮我解决这个问题。
I have a single image view, and I set other image views on it and put them in different locations. I want to apply FLIP ANIMATION on mage view. How can do it? when if(i==1) then my imageview is flipped. img1
is my image view and I want to identify it. I allocate 52 images in `imagearr
My code:
-(void)setCards
{
int k=0;
CGFloat dx = self.view.frame.size.width/2;
CGFloat dy = self.view.frame.size.height/2;
int cnt = [imagearr count];
for (int j=1; j<3; j++) // Number of cards
{
for (int i=1; i<5; i++) // Number of players
{
int rnd = (arc4random() % cnt)-1;
UIImage *img = [imagearr objectAtIndex:rnd];
UIImageView *img1 = [[UIImageView alloc] initWithImage:img];
CGRect frame = img1.frame;
frame.size.width = frameX;
frame.size.height = frameY;
[img1 setFrame:frame];
[img1 setTag:k+i];
[UIView beginAnimations:@"Move" context:nil];
[UIView setAnimationDuration:0.50];
if (i==1)
{
[img1 setCenter:CGPointMake(dx+100, dy+(j*15))];
}
if (i==2)
{
[img1 setCenter:CGPointMake(dx+(j*15), dy+60)];
}
if (i==3)
{
[img1 setCenter:CGPointMake(dx-130, dy-(j*15))];
}
if (i==4)
{
[img1 setCenter:CGPointMake(dx-(j*20), dy-95)];
}
[self.view addSubview:img1];
[UIView commitAnimations];
[img1 release];
}
k=k+10;
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
}
I still can't identify a specific imageview. Please help me to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以尝试类似的东西
you could try something similar to this
您可以在图像视图上放置一个不可见的 UIbutton 并捕获触摸事件。效果是一样的,而且更干净、更简单。如果您需要更多信息,请告诉我。很乐意在这里编辑我的答案。
You can probably place a invisible UIbutton over your imageviews and capture touch event. The effect is the same and its much more cleaner and simpler. Let me know if you need more info. Would be happy to edit my answer here.
苹果链接 代码:
从多个 imageview 中检测 imageview
堆栈链接
这里是从多个图像中检测图像的讨论
祝你好运
Apple Link For the code:
Detect imageview from multiple imageview
Stack Link As Well
Here is the discussion for to detect the images from the multiple images
Good Luck
不同的 imageView 中会有不同的图像,因此每个 imageView 将位于不同的位置。
在您的触摸方法中,您是否必须检查触摸的边界,然后您必须匹配触摸区域下的 imageView。据此您可以继续您的功能。
There will be different images in different imageView so each imageView will be on different position.
in your touch method do you have to check the bounds of the touch and then you have to match which imageView lies under your touch region.According to that you can proceed your functionality.
将唯一的标签值与每个 UIImageView 实例相关联。
参考代码。
Associate a unique tag values with each UIImageView instances.
Code for reference.
我认为你应该看一下Apple示例项目 GeekGameBoard< /a>.它实现了一个纸牌游戏,您将能够学习如何选择代表卡片的
CALayer
s*,将它们拖动,并使它们彼此交互。*从性能角度来看,这比使用视图更容易,而且可能更好。
I think you should take a look at the Apple sample project GeekGameBoard. It implements a solitaire game and you will be able to learn how to select
CALayer
s* representing cards, drag them around, and have them interact with each other.*Which will be both easier and probably better from a performance perspective than using views.
为单个imageView设置标签是最好的方法。使用标签,您甚至可以检测放置在表格视图单元格内的多个图像,并设置操作以选择单个标签。
Setting tag for individual imageView is the best approach. Using tag you could even detect multiples images placed inside tableview cells and set action for selection of individual tag as well.