我想做这样的形状匹配应用程序。
请检查此链接的第二张图片。
也请检查此链接,我可以拖放图像,但我无法匹配确切的位置,也无法用另一个图像填充该图像。
任何想法或建议都将受到高度欢迎。
//UIImageView *imageView1
(blank image with whom the image has to be matched),
//UIImageView *imageView2
(the coloured image that has to be matched)
Take a NSMutableArray
positionArray, now whenever you add any new blank imageview (imageview1 ) to the screen add its additional information to the MutableArray like
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];
Now whenever you add any new blank imageview you should repeat the same for it . Coming back to the UIGestureRecognizer
selector method
-(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;
}
}
}
Take special care at the time of allocating TagValues to the blank imageview and the toBeDragged imageview (both should be saem for same type of image).
这有点棘手。
您是编码员,您知道哪个图像与什么形状匹配,可能是您拍摄两个图像视图,一个带有形状,另一个带有图像..
PS:我只是给出一个想法。
因此,您可能可以跟踪标签。添加手势识别器以拖动图像,因此您将知道正在拖动哪个形状。现在在移动时只需将形状中心与图像当前位置的中心进行比较。确保您比较范围而不是精确的中心。
希望这么多信息对你有帮助:)