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 に答える