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

标签 iphone uiimageview touchesbegan touchesmoved

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

相关文章:

iphone - Xcode 4 上的权限被拒绝

iphone - 我想在 CFSTR 中传递一个变量?

iphone - iPad 模拟器不接收 iPhone 的 320x480 帧之外的触摸事件

Swift Sprite Kit 开启触控功能

iphone - .Net Apple 推送通知服务提供商?

iphone - cocoa touch 编程。内循环中的KVO/KVC super 慢。我怎样才能加快速度?

ios - 在 UIScrollView 内的可缩放 UIIMageView 周围绘制一个 1 像素宽的框

ios - 如何使 UIImageView 出现在 MapView 上方

swift - subview 中两个对象之间的碰撞检测

swift - 为什么我的 .delaysTouchesBegan 不能始终如一地工作?