Iphone 在IOS中交换图像

Iphone 在IOS中交换图像,iphone,ios,Iphone,Ios,我必须在一个屏幕上交换四个图像。只能在左/右/上/下方向交换图像,不能对角交换。例如,第一个图像可以与其右侧和下方的图像交换,第二个图像只能与其左侧和下方的图像交换,依此类推。有谁能帮我做这件事吗。感谢您添加拖放功能,假设这就是您的意思,您将希望查看添加滑动手势识别器。当用户滑动确定哪个方向并处理图像交换时 [编辑]-想象一个正方形被分成四个相等的部分。左上部分的索引为0,右上部分的索引为1,左下部分的索引为2,最后右下部分的索引为3。下面的代码检查当前索引,并从中确定是否可以进行图像交换,如果

我必须在一个屏幕上交换四个图像。只能在左/右/上/下方向交换图像,不能对角交换。例如,第一个图像可以与其右侧和下方的图像交换,第二个图像只能与其左侧和下方的图像交换,依此类推。有谁能帮我做这件事吗。感谢您添加拖放功能,假设这就是您的意思,您将希望查看添加滑动手势识别器。当用户滑动确定哪个方向并处理图像交换时

[编辑]-想象一个正方形被分成四个相等的部分。左上部分的索引为0,右上部分的索引为1,左下部分的索引为2,最后右下部分的索引为3。下面的代码检查当前索引,并从中确定是否可以进行图像交换,如果不能,则什么也不做

这段代码来自我的头脑,所以可能有语法错误,但逻辑是合理的(我希望:D)


这与我的期望不符,请给我另一个答案。我编辑了这篇文章以包含一个有效的解决方案,而不是sudo代码。
- (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];
         }];
    }
}];
}