2013-04-30 18 views
0

我想做形状匹配应用程序like this如何匹配iOS中UIGestureRecognizer中的图像形状?

请检查这个链接的第二个图像。

请检查this link也, 我可以拖放图像,但我无法匹配的确切位置,无法填写与另一个图像。

任何想法或建议将非常受欢迎。

+0

如何给这样的链接? – 2013-04-30 07:07:56

+0

你是如何尝试过匹配位置的?你用'用另一个填充图像'是什么意思?你是不是简单地叠加或替换视图? – Wain 2013-04-30 07:09:43

+0

先生,我是新的iphone和这个项目分配给我,所以我正在寻找并从iPhone专家询问这个。真的不知道这个项目的先生。 – 2013-04-30 07:15:39

回答

0

这有点棘手。

你是程序员,你知道哪个图像匹配什么形状,可能是你需要两个imageviews一个与形状等与图像..

PS:我在这里只给一个想法。

所以可能你可以跟踪标签。添加手势识别器来拖动图像,以便您了解哪个形状正在被拖动。移动时只需将形状中心与图像当前位置的中心进行比较。确保你比较范围而不是确切的中心。

希望这么多的信息可以帮助您:)

+0

谢谢aloot先生。是的,它会帮助我。现在我有想法从哪里开始。先生,你有没有关于这个项目的任何教程链接? – 2013-04-30 07:18:49

+0

我不这么认为,你会得到任何示例代码。技巧将工作 – DivineDesert 2013-04-30 08:07:05

+0

先生我已根据你创建了一个演示。现在我无法理解这一行“现在移动时只需比较形状中心与图像当前位置的中心。”?先生,请您再提一些建议。 – 2013-04-30 09:47:57

1

//UIImageView *imageView1(与之图像必须被匹配的空白图像),

//UIImageView *imageView2(彩色图像必须被匹配)

取一个NSMutableArray positionArray,现在每当你添加任何新的空白imageview(imageview1)到屏幕添加其额外的信息到MutableArray像

UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,50)]; 
[imageview setTag:TagValue]; 
[imageview setImage:[UIImage imageNamed:@"blankMonkeyShapedImage.png"]]; 

//Incrementing the TagValue by one each time any blank imageview is added 
TagValue++; 

NSMutableDictionary *locationDic=[[NSMutableDictionary alloc] init]; 
[locationDic setValue:[NSNumber numberWithInt:imageview.tag] forKey:@"TagNumber"]; 
[locationDic setValue:[NSString stringWithFormat:@"Monkey~example"] forKey:@"ImageName"]; 
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.origin.x] forKey:@"PosX"]; 
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.origin.x] forKey:@"PosY"]; 
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.size.width] forKey:@"SizeWidth"]; 
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.size.height] forKey:@"SizeHeight"]; 

[self.view addSubview:imageview]; 

[positionArray addObject:locationDic]; 

现在,无论何时添加任何新的空白图像视图,都应该重复相同的操作。回来的UIGestureRecognizer选择方法

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture 
{ 
     CGPoint translation = [gesture translationInView:self.view]; 

     for (int i=0 ; i<[positionArray count]; i++) 
     { 
      NSMutableDictionary *lctionDic=[positionArray objectAtIndexPath:i]; 

      float posX=[[lctionDic valueFor:@"PosX"] floatValue]; 
      float posY=[[lctionDic valueFor:@"PosY"] floatValue]; 
      float SizeWidth = [[lctionDic valueFor:@"SizeWidth"] floatValue]; 
      float SizeHeight = [[lctionDic valueFor:@"SizeHeight"] floatValue]; 


      if (translation.x >= posX && translation.x <= posX+SizeWidth && 
       translation.y >= posY && translation.y <= posX+SizeHeight) 
      { 
       //Condition where the dragged objects position matches any previousluy placed balnk imageview. 

       if(gesture.view.tag==[[lctionDic valueFor:@"TagNumber"] intValue]) 
       { 
        NSLog(@"Matched Image's Name is : %@",[lctionDic valueForKey:@"ImageName"]); 
        break; 
       } 
       else 
        continue; 
      } 
     } 
} 

在分配TagValues空白的ImageView和toBeDragged ImageView的时候要特别小心(两者都应该是为SAEM相同类型的图像)。