以下代码在ARC中工作正常,但它不能在非弧形中工作,实际上我想在非弧形代码中实现这个自由手绘图。非弧形的问题是指针位于一个地方,而线条正在另一个地方绘制。这是你的光幕 reference
代码: 在.h
UIImageView *drawImage;
CGPoint location;
CGPoint lastPoint;
CGPoint moveBackTo;
CGPoint currentPoint;
NSDate *lastClick;
BOOL mouseSwiped;
NSMutableArray *latLang;
///
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
drawImage.image = [defaults objectForKey:@"drawImageKey"];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:drawImage];
drawImage.backgroundColor = [UIColor blueColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
}
location = [touch locationInView:self.view];
lastClick = [NSDate date];
lastPoint = [touch locationInView:self.view];
NSLog(@"LastPoint:%@",NSStringFromCGPoint(lastPoint));
lastPoint.y -= 0;
mouseSwiped = YES;
[super touchesBegan: touches withEvent: event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height));
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 8.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[drawImage setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (mouseSwiped) {
}
lastPoint = currentPoint;
[self.view addSubview:drawImage];
}
答案 0 :(得分:0)
我想问题是自动释放lastClick。最简单的事情就是取代:
NSDate *lastClick;
与
@property (strong, nonatomic) NSDate *lastClick;
...好吧我混淆了ARC而没有ARC。如果您没有ARC的问题,那么您必须保留日期:
lastClick = [[NSDate date] retain];
然后使用implementation实现touchesEnd的处理程序:
[lastClick release];
lastClick = nil;