0

これは私が今まで持っているものです

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    for (UIImageView *imageView in _imageViewArray) {
            CGPoint Location = [touch locationInView:touch.view];
            imageView.center = Location;
    }   
}

私が直面している問題は、1つの画像を移動すると、すべて同じ場所にジャンプすることです。

サイバーポーンのおかげで、これは私がやったことで動作するようになりました

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint oldPoint = [touch previousLocationInView:touch.view];
    CGPoint newPoint = [touch locationInView:touch.view];

    CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);

    for (UIImageView *imageView in _imageViewArray) {
        if (CGRectContainsPoint(imageView.frame, newPoint)) {
            CGPoint cntr = [imageView center];
            [imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];

        }
}
}
4

2 に答える 2

5

それらをすべて同じ場所に移動しているため、タッチ位置の違いを計算し、その変位をすべてのビューに追加する必要があります。以下のコードはあなたの問題を解決するはずです! touchesBegan を忘れて、そのような touchesMoved メソッドをオーバーライドするだけです。

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

    CGPoint oldPoint = [touch previousLocationInView:touch.view];
    CGPoint newPoint = [touch locationInView:touch.view];

    CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);

    for (UIImageView *imageView in _imageViewArray) {
        CGPoint cntr = [imageView center];
        [imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];
    }
}

それらのいずれかがクリックされたときにそれらを個別に移動したい場合は、代わりに以下のコードを使用してください!

float oldX, oldY;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:touch.view];
    for (UIImageView *imageView in _imageViewArray) {
        if(CGRectContainsPoint(imageView.frame, pt)) {
            oldX = imageView.center.x - imageView.frame.origin.x - pt.x;
            oldY = imageView.center.y - imageView.frame.origin.y - pt.y;
            break;
        }
    }
}

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

    for (UIImageView *imageView in _imageViewArray) {
        if (CGRectContainsPoint(imageView.frame, pt)) {
            [self setCenter:CGPointMake(pt.x+oldX, pt.y+oldY)];
        }
    }

プログラミングを楽しもう!

于 2012-11-16T04:43:13.003 に答える
0

ここでは、そのようにコーディングしました。単一の画像を移動する場合は、その画像を見つける必要があり、その画像だけを移動する必要があります。

于 2012-11-16T04:37:58.663 に答える