3

그래서 fallingBall이라는 UIView가 현재 theBlockView이라는 UIView와 잘 충돌합니다. 이 충돌을 감지하기 위해 CGRectIntersectsRect(theBlockView.frame, fallingBall.frame)을 사용하고 있습니다.Cocoa iOS 충돌이있는 서클로 사각형 만들기

그게 전부예요. 그래서 지금은 내 fallingBall을 실제로 둥글게하고 싶습니다. theBlockView의 꼭지점을 반올림하고 싶습니다. 이렇게하려면 다음 코드를 사용했습니다.

//round top right-hand corner of theBlockView 
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:theBlockView.bounds 
              byRoundingCorners:UIRectCornerTopRight 
              cornerRadii:CGSizeMake(10.0, 10.0)]; 
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.frame = theBlockView.bounds; 
maskLayer.path = maskPath.CGPath; 
theBlockView.layer.mask = maskLayer; 

//round the fallingBall view 
[[fallingBall layer] setCornerRadius:30]; 

그러나 멋지고 둥근 것처럼 보이지만 뷰는 여전히 사각형입니다. 제 질문은 : 어떻게하면 CGRectIntersectsRect을 모양처럼 보이게 할 수 있습니까? 동일하게 작동하지만 충돌을 감지하기 위해 뷰의 알파를 사용하는 함수가 있습니까?

시간 내 주셔서 감사합니다.

답변

3

사실 내 질문에 답변 해 드리겠습니다.

좋아요, 그래서 지난 10 시간 동안의 많은 부분을 둘러 보았습니다.이 게시물을 보았습니다 : Circle-Rectangle collision detection (intersection) - e.James가 무엇을 말하고 있는지 확인하십시오!

나는이에 도움이 함수를 썼다 : 첫번째, 다음 struct의 선언

typedef struct 
{ 
    CGFloat x; //center.x 
    CGFloat y; //center.y 
    CGFloat r; //radius 
} Circle; 
typedef struct 
{ 
    CGFloat x; //center.x 
    CGFloat y; //center.y 
    CGFloat width; 
    CGFloat height; 
} MCRect; 

그런 다음 함수를 추가합니다

-(BOOL)circle:(Circle)circle intersectsRect:(MCRect)rect 
{ 

    CGPoint circleDistance = CGPointMake(abs(circle.x - rect.x), abs(circle.y - rect.y)); 

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; } 
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; } 

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; } 

    CGFloat cornerDistance_sq = pow((circleDistance.x - rect.width/2), 2) + pow((circleDistance.y - rect.height/2), 2); 

    return (cornerDistance_sq <= (pow(circle.r, 2))); 
} 

을 나는 희망이 누군가를하는 데 도움이!

2

CGRectIntersectsRect는 항상 직사각형을 사용하며, 뷰의 프레임도 항상 사각형입니다. 자신 만의 함수를 작성해야합니다. 코너 반경을 사용하여 원의 중심을 계산하고 직사각형과 원이 어떻게 든 교차하는지 테스트 할 수 있습니다.

+0

알았어, 팁 주셔서 감사합니다! –

관련 문제