2012-08-24 85 views
2

我必須在一個屏幕上交換四個圖像。圖像只能在左/右/上/下方向交換,而不能對角。例如,第一張圖像可以與其右下方的圖像交換,第二張圖像只能與左側和下方的圖像交換,依此類推。任何人都可以請幫我關於如何做到這一點。由於在IOS中交換圖像

回答

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]; 
     }]; 
    } 
}]; 
} 
+0

其不符合我的期望,請給我另一個答案。 – Priyanka

+0

我編輯的帖子包括一個工作解決方案,而不是sudo代碼。 – bennythemink