我有一个小(30X30 大小)UIView 的网格,我通过使用以下代码在屏幕上点击两个点作为起点和终点在它们上面画一条线:
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {244.0f/255.0f, 226.0f/255.0f, 119.0f/255.0f, 0.8};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 20.0);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
点击屏幕上的两个点并画一条线可以正常工作,但是如何让所有视图与线相交?我想在 touches end 方法中获得这些视图。
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:alphabetView];
UIView *touched = [alphabetView hitTest:pt withEvent:event];
CGPoint p = touched.center;
// code here to get view list.
}
任何帮助,将不胜感激。