我正在尝试创建一个应用程序,该应用程序由用户每3次点击绘制一次角度。每个抽头创建角度点并将其连接。
这很简单。但是,现在我试图使这些角度可编辑,并且变得越来越困难。当我完成3次敲击和touchesEnded调用后,我将这三个点添加到名为segment的NSObject子类中,该子类具有三个属性firstPoint,secondPoint和thirdPoint,并精确地存储该属性。
然后,我将“段”对象添加到数组中。每当touchesBegan被调用时,我就会进入数组的for循环。如果抽头位于“段”对象的3个CG点中的任意25个像素之内,则我不会绘制新角度,而是编辑该角度。
据我所知,到目前为止,还没有任何问题或错误,感谢你们!我只是在检查常规改进/简化之处,以保持当前使用的方法。我希望我能给所有人以赏金,因为你们所有人都非常有帮助!
这是我迄今为止最大的项目,我将不胜感激。如果您愿意并且可以应付的话,这是一个非常好的挑战。 IMMENSELY 非常感谢您的帮助。如果您能想到一种更好的方式来处理我要完成的任务,请让我知道!
注意:要绘制,只需在屏幕上点击/单击即可。
这是最相关的代码:
drawingsubclass.m
#import "SegmentDraw.h"
#import <QuartzCore/QuartzCore.h>
BOOL editing1 = FALSE;
BOOL editing2 = FALSE;
BOOL editing3 = FALSE;
BOOL erased = FALSE;
int radius = 35;
int handleSize = 20;
@implementation SegmentDraw {
UIImage *incrementalImage;
}
@synthesize first;
@synthesize second;
@synthesize third;
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor clearColor]];
first = CGPointZero;
second = CGPointZero;
third = CGPointZero;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
for (Segment *currentSegment in self.tapArray) {
int xDistance = abs(point.x - currentSegment.firstPoint.x);
int yDistance = abs(point.y - currentSegment.firstPoint.y);
int firstDistance = sqrtf(xDistance * xDistance + yDistance * yDistance);
int xDistance2 = abs(point.x - currentSegment.secondPoint.x);
int yDistance2 = abs(point.y - currentSegment.secondPoint.y);
int secondDistance = sqrtf(xDistance2 * xDistance2 + yDistance2 * yDistance2);
int xDistance3 = abs(point.x - currentSegment.thirdPoint.x);
int yDistance3 = abs(point.y - currentSegment.thirdPoint.y);
int thirdDistance = sqrtf(xDistance3 * xDistance3 + yDistance3 * yDistance3);
if (firstDistance <= radius) {
NSLog(@"First point matches");
editing1 = TRUE;
editing2 = FALSE;
editing3 = FALSE;
first = point;
second = currentSegment.secondPoint;
third = currentSegment.thirdPoint;
self.segmentBeingEdited = currentSegment;
[self setNeedsDisplay];
[self drawBitmap];
erased = FALSE;
return;
}
else if (secondDistance <= radius) {
NSLog(@"Second point matches");
editing2 = TRUE;
editing1 = FALSE;
editing3 = FALSE;
first = currentSegment.firstPoint;
second = point;
third = currentSegment.thirdPoint;
self.segmentBeingEdited = currentSegment;
[self setNeedsDisplay];
[self drawBitmap];
erased = FALSE;
return;
}
else if (thirdDistance <= radius) {
NSLog(@"Third point matches");
editing3 = TRUE;
editing1 = FALSE;
editing2 = FALSE;
first = currentSegment.firstPoint;
second = currentSegment.secondPoint;
third = point;
self.segmentBeingEdited = currentSegment;
[self setNeedsDisplay];
[self drawBitmap];
erased = FALSE;
return;
}
else {
editing1 = FALSE;
editing2 = FALSE;
editing3 = FALSE;
}
}
if (CGPointEqualToPoint(first, CGPointZero)) {
first = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
second = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
third = [touch locationInView:self];
}
else {
//[self drawBitmap];
first = [touch locationInView:self];
second = CGPointZero;
third = CGPointZero;
}
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
for (Segment *currentSegment in self.tapArray) {
if (editing1) {
editing2 = FALSE;
editing3 = FALSE;
first = point;
second = self.segmentBeingEdited.secondPoint;
third = self.segmentBeingEdited.thirdPoint;
//self.segmentBeingEdited = currentSegment;
if (erased == FALSE) {
[self drawBitmap];
// NSLog(@"YOU NEED TO ERASE.");
}
[self setNeedsDisplay];
return;
}
else if (editing2) {
editing1 = FALSE;
editing3 = FALSE;
first = self.segmentBeingEdited.firstPoint;
second = point;
third = self.segmentBeingEdited.thirdPoint;
//self.segmentBeingEdited = currentSegment;
if (erased == FALSE) {
[self drawBitmap];
// NSLog(@"YOU NEED TO ERASE.");
}
[self setNeedsDisplay];
return;
}
else if (editing3) {
// NSLog(@"It's editing time, yo");
editing1 = FALSE;
editing2 = FALSE;
first = self.segmentBeingEdited.firstPoint;
second = self.segmentBeingEdited.secondPoint;
third = point;
//self.segmentBeingEdited = currentSegment;
if (erased == FALSE) {
[self drawBitmap];
// NSLog(@"YOU NEED TO ERASE.");
}
[self setNeedsDisplay];
return;
}
else {
editing1 = FALSE;
editing2 = FALSE;
editing3 = FALSE;
}
}
if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
first = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
second = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && !(CGPointEqualToPoint(third, CGPointZero))) {
third = [touch locationInView:self];
}
else {
first = [touch locationInView:self];
second = CGPointZero;
third = CGPointZero;
}
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSUInteger index;
if (editing1 || editing2 || editing3) {
index = [self.tapArray indexOfObject:self.segmentBeingEdited];
Segment *datSegment = [self.tapArray objectAtIndex:index];
datSegment.firstPoint = first;
datSegment.secondPoint = second;
datSegment.thirdPoint = third;
}
if (!CGPointEqualToPoint(third, CGPointZero) && editing1 == FALSE && editing2 == FALSE && editing3 == FALSE) {
Segment *segment = [[Segment alloc] init];
segment.firstPoint = first;
segment.secondPoint = second;
segment.thirdPoint = third;
if (self.tapArray == nil) {
self.tapArray = [[NSMutableArray alloc] init];
}
[self.tapArray addObject:segment];
}
editing1 = FALSE;
editing2 = FALSE;
editing3 = FALSE;
erased = FALSE;
[self drawBitmap];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)drawRect:(CGRect)rect {
[incrementalImage drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 5.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGRect rectangle = CGRectMake(first.x - 10, first.y - 10, handleSize, handleSize);
CGContextAddEllipseInRect(context, rectangle);
CGContextMoveToPoint(context, first.x, first.y);
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGContextAddLineToPoint(context, second.x, second.y);
CGRect rectangle2 = CGRectMake(second.x - 10, second.y - 10, handleSize, handleSize);
CGContextAddEllipseInRect(context, rectangle2);
CGContextMoveToPoint(context, second.x, second.y);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGContextAddLineToPoint(context, third.x, third.y);
CGRect rectangle3 = CGRectMake(third.x - 10, third.y - 10, handleSize, handleSize);
CGContextAddEllipseInRect(context, rectangle3);
}
CGContextStrokePath(context);
}
}
- (void)drawBitmap {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
if (!incrementalImage) { // first draw;
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor clearColor] setFill];
[rectpath fill]; // fill it
}
[incrementalImage drawAtPoint:CGPointZero];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 5.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
if (editing1 || editing2 || editing3) {
// CGContextBeginTransparencyLayer(context, NULL);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetLineWidth(context, 6.0);
//CGContextSetBlendMode(context, kCGBlendModeColor);
if (!CGPointEqualToPoint(self.segmentBeingEdited.firstPoint, CGPointZero)) {
CGContextMoveToPoint(context, self.segmentBeingEdited.firstPoint.x, self.segmentBeingEdited.firstPoint.y);
if (!CGPointEqualToPoint(self.segmentBeingEdited.secondPoint, CGPointZero)) {
CGContextAddLineToPoint(context, self.segmentBeingEdited.secondPoint.x, self.segmentBeingEdited.secondPoint.y);
}
if (!CGPointEqualToPoint(self.segmentBeingEdited.thirdPoint, CGPointZero)) {
CGContextAddLineToPoint(context, self.segmentBeingEdited.thirdPoint.x, self.segmentBeingEdited.thirdPoint.y);
}
CGContextStrokePath(context);
// CGContextEndTransparencyLayer(context);
}
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
erased = TRUE;
return;
}
CGContextSetBlendMode(context, kCGBlendModeNormal);
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGContextMoveToPoint(context, first.x, first.y);
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGContextAddLineToPoint(context, second.x, second.y);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGContextAddLineToPoint(context, third.x, third.y);
}
CGContextStrokePath(context);
}
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
@end
回顾一下,我想绘制一些角度,然后能够通过点击我要编辑的角度的3个点中的任意一个来编辑它们。然后通过拖动,将角度编辑为延伸到分接头当前所在的位置。只是想使其更好/更简单。任何意见/见解欢迎!
最佳答案
您没有正确处理编辑标志。您拨打touchesBegan:
if(yDistance2 <= 25 || editing){
NSLog(@"It's editing time, yo");
editing = TRUE; // Here's your problem
这意味着在
touchesMoved
中,当您点击时:if(xDistance <= 25 || editing) // Note the "Or editing"
editing
等于true,这意味着它将移动点1,因为它首先命中了if语句,无论设置哪一点。结论
您需要为每个点使用特定的编辑标记。不是共享的。至少没有
or
检查。或者更好的是,更好地处理touchesMoved
。您几乎不需要完全复制touchesBegan
中的代码边注
尽管与您的问题不完全相关,但我相信您还在if语句中交换point2和point3。在检查xDistance2时:
else if(xDistance2 <= 25 || editing){
NSLog(@"X is pretty close");
if(yDistance2 <= 25 || editing){
NSLog(@"It's editing time, yo");
editing = TRUE;
first = currentSegment.firstPoint;
second = currentSegment.secondPoint;
third = point; // <- Why are you setting third to point and not second to point.
self.segmentBeingEdited = currentSegment;
[self setNeedsDisplay];
return;
}
}
当您检查XDistance3时也是如此。您将第二点设置为点,而不是第三点。
关于ios - 使用drawRect绘制可编辑的角度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20772052/