2

1 つの画面で 4 つの画像を交換する必要があります。画像の入れ替えは上下左右のみ可能で、斜め方向の入れ替えはできません。たとえば、1 番目の画像はその右側とその下の画像と入れ替えることができ、2 番目の画像は左側とその下の画像とのみ入れ替えることができます。誰でもそれを行う方法について私を助けてください。ありがとう

4

2 に答える 2

0

スワイプ ジェスチャ レコグナイザーを追加します。ユーザーがスワイプするときに、どちらの方向を決定し、画像のスワップを処理します。

[編集] - 正方形を 4 つの等しいセクションに分割したと想像してください。左上のセクションのインデックスは 0、右上のインデックスは 1、左下のインデックスは 2、最後に右下のインデックスは 3 です。以下のコードは現在のインデックスをチェックし、そこから画像がスワップを行うことができますが、そうでない場合は何もしません。

このコードは頭のてっぺんからのものなので、構文エラーがあるかもしれませんが、ロジックは健全です (私は :D を願っています)。

- (void) viewDidLoad {
// turn on user interaction on the image view as its off by default.
[self.imageView setUserInteractionEnabled:TRUE];

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[self.imageView addGestureRecognizer:recognizer];

self.currentImageIndex = 0;
self.images = [NSArray arrayWithObjects:[UIImage imageNamed:@"top-left"],[UIImage imageNamed:@"top-right"],[UIImage imageNamed:@"bottom-left"],[UIImage imageNamed:@"top-right"],nil];

}


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {

if (self.currentImageIndex == 0 || self.currentImageIndex == 2) self.currentImageIndex++; // change to image to the right
else return; // do nothing

}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {

if (self.currentImageIndex == 1 || self.currentImageIndex == 3) self.currentImageIndex--; // change to the image to the left
else return; // do nothing

}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {

if (self.currentImageIndex == 2 || self.currentImageIndex == 3) self.currentImageIndex -= 2; // change to the above image 
else return; // do nothing

}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) {

if (self.currentImageIndex == 0 || self.currentImageIndex == 1) self.currentImageIndex += 2; // change to the above image 
else return; // do nothing

}
[UIView animationWithDuration:0.5 animations:^{
    [self.imageView setAlpha:0];
} completion^(BOOL finished){
    if (finished) {
         [UIView animationWithDuration:0.5 animations:^{
             [self.imageView setImage[self.images objectAtIndex:self.currentImageIndex]];
             [self.imageView setAlpha:1];
         }];
    }
}];
}
于 2012-08-24T11:32:48.543 に答える
0

ドラッグアンドドロップ機能を追加するには、それがあなたの意図であると仮定して、 UIPanGestureRecognizerを調べる必要があります。

于 2012-08-24T10:54:15.993 に答える