我已经子类化了 UIImageView,并实现了 TouchBegan/Moved/Finished,如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Began");
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
self.image = nil;
return;
}
lastPoint = [touch locationInView:self];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
NSLog(@"Moved");
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.bounds.size);
[self.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 18.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 1.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Ended");
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
self.image = nil;
return;
}
if(!mouseSwiped) {
NSLog(@"here?");
UIGraphicsBeginImageContext(self.bounds.size);
[self.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 18.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 1.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
它似乎在最初的一段时间内发挥了作用。 TouchesBegan() 每次都会触发,然后当我开始移动时,touchesMoved() 有时会触发 5 次,有时 3 次,有时 7 次,但然后就停止了。 touchesEnded() 永远不会被触发,我只是不知道发生了什么!
我已经盯着这个有一段时间了,有人看到我错过了什么吗?
最佳答案
重写并实现:- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
看看是否正在调用。
如果是这样,则说明某些东西正在取消您的触摸事件。可能是内存不足。
关于iphone - 在子类 UIImageView 中,touchesBegan 触发,touchesMoved 触发几次,但随后无缘无故停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5782165/