1

を使用して、View Controller がロードされたときに 2 つの四角形を構築してCGPathいます。四角形は で移動できますPanGestureRecognizer。問題は、2 つの四角形がいつ交わったかをどのように知ることができるかということです。交差させないためには?

MainViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i=0; i< 2; i++) {
        //create rects of CGPath
        CGRectCustomView* randomColorRect = 
                                [[CGRectCustomView alloc]initWithFrame: 
                                CGRectMake(<random place on screen>)];
        //random angle
        randomColorRect.transform = 
                                CGAffineTransformMakeRotation
                                (DegreesToRadians([Shared randomIntBetween:0 and:360]));

        [self.view addSubview:randomColorRect];

    }

}

- (BOOL)areRectsCollide {

      ???How to find this???

}

CGRectCustomView.m:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 8.0); 
    CGContextStrokePath(context); // do actual stroking
    CGContextSetRGBFillColor(context, <green color>, 1); 
    CGContextFillRect(context, CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)); 
    path = CGContextCopyPath(context);
}

ここのAppleガイドには、パスにポイントが含まれているかどうかを判断する関数があります

- (BOOL)containsPoint:(CGPoint)point onPath:(UIBezierPath *)path inFillArea:(BOOL)inFil

しかし、無限の数のポイントである長方形があります。それで、私は何をすべきですか?頭が壊れる…

4

2 に答える 2

0

私があなたの質問を理解していれば、2 つの CGRects がより適切に使用できるかどうかを確認するには:

 /* Return the intersection of `r1' and `r2'. This may return a null rect. */
 CG_EXTERN CGRect CGRectIntersection(CGRect r1, CGRect r2)

For example:
 CGRect rect1 = CGRectMake(0, 0, 10, 10);
 CGRect rect2 = CGRectMake(5, 5, 10, 10);
 CGRect rect3 = CGRectMake(20, 20, 10, 10);


 CGRect r1 = CGRectIntersection(rect1, rect2);   // Returns a CGRect with (0,0,5,5)

 CGRect r2 = CGRectIntersection(rect1, rect3);   // Returns a CGRect with (Inf,Inf,0,0)
 if (CGSizeEqualToSize(r2.size,CGSizeZero)) {
     //   rect1 rect3 Intersects at CGSizeZero == Do not Intersect
 }
于 2013-09-29T10:56:44.537 に答える