3

我对 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() 从未被解雇,我只是不明白发生了什么!

我已经盯着这个有一段时间了,有没有人看到我缺少的东西?

4

2 回答 2

1

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

看看是否被调用。

如果是这样,则某些事情正在取消您的触摸事件。可能是内存不足。

于 2011-04-25T19:21:50.503 回答
1

对于将来阅读本文的任何人,解决方案是将 UIImageView 包装在容器 UIView 中。Ido it 为我的绘图应用程序的方式是将单个 UIView 专用于一个目的:包含我在其上绘制的 UIImageView。

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

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

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

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

@property UIImageView *imageView;

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

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

self.image

参考以下内容:

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

希望这可以帮助那里的人!

于 2012-07-03T18:07:43.043 回答