如何确定两个uiviewimage是否相交。我想做一个自动捕捉功能。当小图像与大图像相交或接近时(让distance<=x说),我希望小图像自动捕捉(连接)到大图像的交叉点处。
发布于 2012-06-02 12:49:49
前两张海报已经在CGRectIntersectsRect的正确轨道上。
BOOL isIntersecting = CGRectIntersectsRect(smallImage.frame, largeImage.frame);
if(isIntersecting){
//Animate the Auto-Snap
[UIView beginAnimation:@"animation" context:nil];
[UIView setAnimationDuration:0.5];
smallImage.frame = largeImage.frame;
[UIView commitAnimations];
}基本上,这意味着如果两个图像相交,小图像帧将在0.5秒内捕捉到较大的图像。不过,您不必对其进行动画处理;您只需删除除smallImage.frame = largeImage.frame;之外的所有代码,即可使其瞬间生效。但是,我推荐使用动画的方式。希望这能有所帮助。
-编辑
您可以使用以下代码:
BOOL isIntersecting = CGRectIntersectsRect(smallImage.frame, largeImage.frame);
if(isIntersecting){
//Animation
[UIView beginAnimation:@"animation" context:nil];
[UIView setAnimationDuration:0.5];
smallImage.center = CGPointMake(largeImage.center.x, largeImage.center.y);
//If you want to make it like a branch, you'll have to rotate the small image
smallImage.transform = CGAffineTransformMakeRotation(30);
//The 30 is the number of degrees to rotate. You can change that.
[UIView commitAnimations];
}希望这解决了你的问题。记住,如果这有帮助,请投票并选择答案。
-EDIT-最后一件事。我说CGAffineTransformMakeRotation(30)中的" 30“代表30度,但这是不正确的。CGAffineTransformMakeRotation函数以弧度为单位接受其参数,因此,如果您想要30度,可以这样做:
#define PI 3.14159265
CGAffineTransformMakeRotation(PI/6);发布于 2012-06-01 21:13:04
CGRect bigframe = CGRectInset(bigView.frame, -padding.x, -padding.y);
BOOL isIntersecting = CGRectIntersectsRect(smallView.frame, bigFrame);发布于 2012-06-01 21:13:55
您可以使用CGRectIntersectsRect方法检查帧:
if (CGRectIntersectsRect(myImageView1.frame, myImageView2.frame))
{
NSLog(@"intersected")
}https://stackoverflow.com/questions/10850895
复制相似问题