I am building 2 rects when view controller is loaded, using CGPath
. The rects can be moved with PanGestureRecognizer
. The question is how can I know when 2 rects had met? In order to not let them intersect?
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);
}
In Apple guide here, there is a function that determines if a path contains point
- (BOOL)containsPoint:(CGPoint)point onPath:(UIBezierPath *)path inFillArea:(BOOL)inFil
,
but I have a rectangle which is endless number of points. So what should I do? Breaking my head...