2

执行触摸移动时,如何设置与新图像(点)具有相同固定距离的图像(点)?

在此处输入图像描述

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Crayon_Black.png"]];
        imageView.center = touchLocation;    

    [drawImage addSubview:imageView];
}

我希望这是有道理的。我只需要完成我的学校项目。提前谢谢大家。

4

1 回答 1

2

只要您不将手指移动得太快,此解决方案就可以工作:

@interface ViewController : UIViewController {
    CGPoint lastLocation_;
    CGFloat accumulatedDistance_;
}

...

-(CGFloat) distanceFromPoint:(CGPoint)p1 ToPoint:(CGPoint)p2 {
    CGFloat xDist = (p2.x - p1.x);
    CGFloat yDist = (p2.y - p1.y);
    return sqrt((xDist * xDist) + (yDist * yDist));
}              

-(void) addImageAtLocation:(CGPoint)location {
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Crayon_Black.png"]];
    imageView.center = location;
    [self.view addSubview:imageView];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    lastLocation_ = [[touches anyObject] locationInView:self.view];
    accumulatedDistance_ = 0;
    [self addImageAtLocation:lastLocation_];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    CGFloat distance = [self distanceFromPoint:touchLocation ToPoint:lastLocation_];
    accumulatedDistance_ += distance;
    CGFloat fixedDistance = 40;
    if (accumulatedDistance_ > fixedDistance) {
        [self addImageAtLocation:touchLocation];
        while (accumulatedDistance_ > fixedDistance) {
            accumulatedDistance_ -= fixedDistance;
        }
    }

    lastLocation_ = touchLocation;
}
于 2012-07-26T14:01:50.840 回答