0

ボタンをクリックすると(上/下)、一度に1行ずつトラバースするUIImageViewを持つUITableViewがあります。私が今やりたいことは、ユーザーが UIImageView をテーブルの上または下にのみドラッグできるようにすることです (つまり、横方向の動きはありません)。UIImageView の大部分が特定のセル上にある場合、ユーザーが指を離したときに、UIImageView をその行にリンクさせます。UIImageView を使用した UITableView のイメージを次に示します。

ここに画像の説明を入力

スクロールバーは、移動または下に移動する必要がある UIImageView です。次のメソッドを実装することになっていることを認識しています。

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

    // We only support single touches, so anyObject retrieves just that touch from touches.
    UITouch *touch = [touches anyObject];

    if ([touch view] != _imageView) {

    return;
}

}


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

    UITouch *touch = [touches anyObject];


    if ([touch view] == _imageView) {

        return;
    }
}


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

    UITouch *touch = [touches anyObject];

    //here is where I guess I need to determine which row contains majority of the scrollbar.  This would only measure the y coordinate value, and not the x, since it will only be moving up or down.
        return;
    }
}

ただし、この機能を実現する方法がわかりません。同様の例をオンラインで見つけようとしましたが、Apple の MoveMe のサンプル コードも調べましたが、まだ行き詰っています。私のスクロールバーはテーブルの行とまったく同じサイズではなく、少し長くても同じ高さであることにも注意してください。

回答者全員に事前に感謝します

4

1 に答える 1

0

UIImageViewに UIPanGestureRecognizer を追加してみてください。イメージ ビューの現在の位置を取得することから始めて、translationInViewメソッドを使用してイメージ ビューを移動する場所を決定します。

Appleのドキュメントから:

ビューの位置をユーザーの指の下に保つように調整したい場合は、そのビューのスーパービューの座標系で翻訳を要求します...ジェスチャが最初に認識されたときに、翻訳値をビューの状態に適用します—値を連結しないでくださいハンドラーが呼び出されるたびに。

ジェスチャ認識エンジンを追加する基本的なコードは次のとおりです。

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];

[imageView addGestureRecognizer:panGesture];

次に、計算を行って、ビューをどこに移動するかを決定します。

- (void)panView:(UIPanGestureRecognizer*)sender
{
    CGPoint translation = [sender translationInView:self];

    // Your code here - change the frame of the image view, and then animate
    // it to the closest cell when panning finishes
}
于 2013-06-17T21:22:26.383 に答える