gpt4 book ai didi

iphone - 在子类 UIImageView 中,touchesBegan 触发,touchesMoved 触发几次,但随后无缘无故停止

转载 作者:行者123 更新时间:2023-12-03 19:59:32 25 4
gpt4 key购买 nike

我已经子类化了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com