我正在尝试创建用于绘制用户用手指绘制内容的代码。我使用了以下代码:
-(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没有被调用每个像素,所以每个点和下一个点之间存在很大的差距。所以我需要以某种方式填补这些空白。有人可以用一些代码帮助我做到这一点吗?
提前致谢。
答案 0 :(得分: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)循环来填充当前点和最后一个点之间的空格。