如何确定两个uiviewimages是否相交。我想做一个自动快照功能。当小图像与大图像相交或靠近它时(让我们说距离<= x),我希望小图像在它们相交的点处自动捕捉(连接)到大图像。
答案 0 :(得分:2)
CGRect bigframe = CGRectInset(bigView.frame, -padding.x, -padding.y);
BOOL isIntersecting = CGRectIntersectsRect(smallView.frame, bigFrame);
答案 1 :(得分:1)
您可以使用CGRectIntersectsRect方法检查帧:
if (CGRectIntersectsRect(myImageView1.frame, myImageView2.frame))
{
NSLog(@"intersected")
}
答案 2 :(得分:1)
前两张海报与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;
以外的所有代码来实现。但是,我推荐动画方式。
希望这会有所帮助。
------- -------- EDIT
您可以使用以下代码:
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);