2012-06-01 5 views
3

두 개의 uiviewimages가 교차하는지 어떻게 결정할 수 있습니까? 자동 스냅 기능을 원합니다. 작은 이미지가 큰 이미지와 교차하거나 가까이있을 때 (거리는 < = x라고 말합니다) 작은 이미지가 교차하는 지점의 큰 이미지에 자동으로 스냅 (연결)되기를 원합니다.두 UIViewImages의 교차점

답변

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;을 제외한 모든 코드를 제거하여 즉시 만들 수 있습니다. 그러나 나는 애니메이션 방식을 추천한다. 희망이 도움이됩니다.

------- 편집 --------

당신은 코드를 사용할 수 :이 문제를 해결

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); 
+0

답장을 보내 주셔서 감사합니다. 아주 잘 작동합니다. 문제는 작은 이미지가 큰 이미지 안에 들어가는 것입니다. 나는 이런 것을 원한다. 작은 이미지가 나무의 가지라고 가정합니다. 큰 이미지는 큰 지점입니다. 작은 가지가 큰 가지와 교차 할 때, 작은 가지의 원점은 http://postimage.org/image/u5790gt05/과 같이 큰 가지의 표면에 연결됩니다. CGRectIntersection 메서드를 사용해야합니까? –

+0

CGRectIntersect 메서드는 교차 할 경우에는'YES'를 반환하고 그렇지 않은 경우에는'NO'를 반환합니다. 위의 게시물에 더 추가 할 것입니다 ... – pasawaya

2
CGRect bigframe = CGRectInset(bigView.frame, -padding.x, -padding.y); 

BOOL isIntersecting = CGRectIntersectsRect(smallView.frame, bigFrame); 
+0

padding.x 및 padding.y 무엇인가? 죄송합니다 나는 초보자입니다. –

+0

두 번째 비트 만 필요합니다. 뷰 자체가 교차하지 않을 때 더 작은 뷰를 더 큰 뷰로 스냅하려면 더 큰 사각형으로 패싯을 삽입 할 수 있습니다. 원하는 경우 또는 cgpoint를 선언하는 경우 x 및 y에 대해 동일한 값을 사용할 수 있습니다. –

1

당신은 프레임을 확인하기 위해 CGRectIntersectsRect 방법을 사용할 수 있습니다

if (CGRectIntersectsRect(myImageView1.frame, myImageView2.frame)) 
    { 
     NSLog(@"intersected") 
    }