问题
I am writing an iPhone application somewhat like pick up sticks app on appstore. I have already done with drawing multiple lines and displaying it using CoreGraphics. But now I am stuck with how to delete the line when the user taps on it. I have searched on Google a lot but didn't find anything related.
I have seen this post of yours at some other place and this has really helped me out. but the problem is that if i have a stick and then another stick on top of it. when i click on the stick which is below, it should not get deleted and if i tap on the above on, it should get deleted if there is no stick above it. How to achieve this thing.. A lso i need to store the paths i.e. my lines which is UIBeizerpath in a list, array, dictionary whatever, please help me with this thing also. I'm not finding anything for it.
回答1:
I see 2 issues raised: (1) how to detect a touch on a line drawn in a view, and (2) how to redraw the view without the touched line. A combined approach is to define a line object to be a rectangle converted to a string: NSStringFromCGRect(CGRectMake(x, y, width, height)
. Add one or more line objects to NSSet *setOfLines
and add Quartz 2D / Core Graphics drawing code in the drawRect:
method of the UIView
class that shows the lines.
- (void)drawRect:(CGRect)rect
{
gc = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(gc, lineThickness]);
CGContextSetStrokeColorWithColor(gc, ([UIColor redColor]).CGColor);
for (NSString *lineString in setOfLines) {
CGRect line = CGRectFromString(lineString);
CGContextMoveToPoint(gc, line.origin.x, line.origin.y);
CGContextAddLineToPoint(gc, (line.origin.x + line.size.width), (line.origin.y + line.size.height));
}
CGContextStrokePath(gc);
}
In that same UIView class, add a tap gesture recognizer (note the coexistence with double tapping):
UITapGestureRecognizer *singleTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lineSingleTapped:)];
singleTapper.numberOfTapsRequired = 1;
[singleTapper requireGestureRecognizerToFail:doubleTapper];
[cell addGestureRecognizer:singleTapper];
- (IBAction)lineSingleTapped:(UITapGestureRecognizer *)sender
{
CGPoint tapPoint = [sender locationInView:self];
// Get the line that was tapped
for (NSString *lineString in setOfLines) {
CGRect line = CGRectFromString(lineString);
// Make a rectangle that defines the tappable area of a line: disclaimer, might be very thin or short!
// A minimum size of 44.0 x 44.0 is advisable
CGRect lineTapArea = CGRectMake(line.origin.x, line.origin.y, (line.origin.x + line.size.width), line.origin.y + line.size.height);
if (CGRectContainsPoint(lineTapArea, tapPoint)) {
[setOfLines removeObject:lineObject];
[self setNeedsDisplayInRect:lineTapArea];
break;
}
}
}
The design pattern here is:
- Implement the drawing view class both for user interaction and to do drawing
- Define data objects that represent the visual elements to be drawn
- Alter the data objects based on user interaction and then force part or all of the view to be redrawn
回答2:
If you are using Core Graphics in a UIView, you won't have to delete anything. You will instead have be able to completely redraw the view, but perhaps with one less stick.
来源:https://stackoverflow.com/questions/8702898/core-graphics-line-drawing-and-deleting-on-touch