在iOS中重叠和交换图像
我想交换两张图片。当我移动拖动图像时,它在视图中的任何其他图像上重叠50%或更大,它必须被交换。问题是如何检查拖动图像是否与其他图像重叠50%或超过50%。请用代码示例帮助并建议逻辑 我正在尝试的代码是:在iOS中重叠和交换图像,ios,objective-c,Ios,Objective C,我想交换两张图片。当我移动拖动图像时,它在视图中的任何其他图像上重叠50%或更大,它必须被交换。问题是如何检查拖动图像是否与其他图像重叠50%或超过50%。请用代码示例帮助并建议逻辑 我正在尝试的代码是: if (imgVw.tag != self.tag) { CGRect imgVwRect = CGRectMake(imgVw.frame.origin.x +(imgVw.frame.size.width/2), imgVw.frame.origin.y+(imgVw.fram
if (imgVw.tag != self.tag) {
CGRect imgVwRect = CGRectMake(imgVw.frame.origin.x +(imgVw.frame.size.width/2), imgVw.frame.origin.y+(imgVw.frame.size.height/2), imgVw.frame.size.width, imgVw.frame.size.height);
CGRect movingImgRect = CGRectMake(newCenter.x+self.frame.size.width, newCenter.y+self.frame.size.height, self.frame.size.width, self.frame.size.height);
if (movingImgRect.origin.x >= imgVwRect.origin.x && movingImgRect.origin.y >= imgVwRect.origin.y)
{
NSLog(@"img view tag %lu",imgVw.tag);
UIImage *tempImg = self.image;
[self setImage:imgVw.image];
[imgVw setImage:tempImg];
}
}
这很简单 使用CGRectIntersection方法计算两个矩形的交点 然后将交点的面积与移动矩形的面积进行比较。类似这样的内容(假设我理解您的代码
if (imgVw.tag != self.tag)
{
CGRect imgVwRect = CGRectMake(imgVw.frame.origin.x +(imgVw.frame.size.width/2),
imgVw.frame.origin.y+(imgVw.frame.size.height/2),
imgVw.frame.size.width,
imgVw.frame.size.height);
CGRect movingImgRect = CGRectMake(newCenter.x+self.frame.size.width,
newCenter.y+self.frame.size.height,
self.frame.size.width,
self.frame.size.height);
//Figure out the intersection rect of the 2 rectangles
CGRect intersectionRect = CGRectIntersection(imgVwRect, movingImgRect);
//Find the area of the intersection
CGFloat xArea = intersectionRect.size.height * intersectionRect.size.width;
//Find the area of the moving image
//(this code could be done once and saved in an iVar)
CGFloat movingImgArea = movingImgRect.size.height * movingImgRect.size.width;
//Is the intersection >= 1/2 the size of the moving image?
if (xArea*2 >= movingImgArea)
{
//Do whatever you need to do when the 2 images overlap by >= 50%
}
}
然后你应该接受答案,并在你有足够的声望点数时立即投票。