0

我正在尝试创建代码来绘制用户用手指绘制的内容。我为此使用了以下代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
    [myImage setHidden:YES];
}
CGPoint currentTouch = [touch locationInView:self.view];
if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
[self.view addSubview:myImage];
}

但问题是 touchesMoved 并不是每个像素都被调用,所以每个点和下一个点之间有很大的差距。所以我需要以某种方式填补这些空白。有人可以用一些代码帮我做到这一点吗?

提前致谢。

4

1 回答 1

1

不再需要答案,我回答了我自己的问题:p

但是,对于任何想要答案的人,这里是代码:

UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];

if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
    myImage.tag = tag;
[self.view addSubview:myImage];
[myImage release];
    if (!CGPointEqualToPoint(lastTouch, CGPointMake(0, 0))) { 
        CGPoint nextPoint;
            for (double h = 0.0; h<25.0; h++) {
            double blend = h/25;
            nextPoint.x = currentTouch.x + (blend * (lastTouch.x - currentTouch.x));
            nextPoint.y = currentTouch.y + (blend * (lastTouch.y - currentTouch.y));
            myImageRect = CGRectMake(nextPoint.x, nextPoint.y, 5.0f, 5.0f);
            myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"dot.png"]];
                myImage.tag = tag;
            [self.view addSubview:myImage];
            [myImage release];
    }

    }
lastTouch = currentTouch;
}
}

我添加了一个名为 last touch 的点来记录最后一个点,然后添加一个 (for) 循环来填充当前点和最后一个点之间的空间。

于 2011-10-21T14:57:52.307 回答