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

时间:2011-04-25 19:15:43

标签: iphone uiimageview touchesbegan touchesmoved

我已经为UIImageView创建了子类,并且已经实现了touchesBegan / 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()永远不会被解雇,我只是看不到发生了什么!

我一直在盯着这一段时间,有人看到我失踪的东西吗?

2 个答案:

答案 0 :(得分:1)

覆盖并实施:- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

看看是否正在调用它。

如果是这样,有些东西会取消您的触摸事件。可能记忆力很低。

答案 1 :(得分:1)

对于将来阅读此内容的人来说,解决方案是将UIImageView包装在容器UIView中。我的绘图应用程序的方式是将单个UIView专用于一个目的:包含我绘制的UIImageView。

创建一个名为“ImageContainer”的类作为“UIView”的子类

ImageContainer *view = [[ImageContainer alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]];

当然,框架可以是你想要的任何东西。

然后,在“ImageContainer”类(头文件)中,添加一个属性,如下所示:

@property UIImageView *imageView;

在实现文件(ImageContainer.m)中合成该属性,并在“ - (id)initWithFrame:(CGRect)frame”方法中将Image添加为ImageContainer的子视图。

然后,在您的绘图代码中,而不是引用:

self.image

参考以下内容:

[[[self imageView] image] drawInRect:self.bounds];
[[self imageView] setImage:...];

希望这有助于那里的人!