我必須在一個屏幕上交換四個圖像。圖像只能在左/右/上/下方向交換,而不能對角。例如,第一張圖像可以與其右下方的圖像交換,第二張圖像只能與左側和下方的圖像交換,依此類推。任何人都可以請幫我關於如何做到這一點。由於在IOS中交換圖像
2
A
回答
0
要添加拖放功能,假設是你的意思,你要考慮UIPanGestureRecognizer
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];
}];
}
}];
}
相關問題
- 1. 在tableViewRow中交換圖像
- 2. 在ImageView中交換圖像
- 3. 在IBOutletCollection中交換圖像
- 4. 交換圖像
- 5. 在ScrollView中交換圖像在iPhone中
- 6. 在keydown上交換圖像
- 7. 在Mouseover上交換圖像
- 8. jQuery圖像交換
- 9. JavaScript圖像交換
- 10. JQuery圖像交換
- 11. wxPython交換圖像
- 12. 多圖像交換
- 13. iOS地圖中的引腳在玩地圖後互相交換圖像(Xamarin)
- 14. 在Firefox中的圖像交換
- 15. 在啓動畫面中交換圖像
- 16. 懸停在sprite中的交換圖像
- 17. 在Javascript中交換圖像的文本
- 18. 如何在javascript中交換圖像
- 19. 在div中的圖像交換onclick
- 20. 在J2ME中交換圖像區域
- 21. ios在UItableView中交換UILabel
- 22. jQuery像YUI中的圖像交換
- 23. iOS應用程序在兩個設備之間交換圖像
- 24. Xamarin iOS在運行時捆綁交換圖像
- 25. 在iOS + Javascript上以高幀率反覆交換圖像?
- 26. JS圖像交換中的多個圖像,mouseout錯誤圖像
- 27. 在不同圖像的div中交換圖像
- 28. 使用懸停交換圖像,我不想交換圖像,如果我在'.this_page'
- 29. 圖像交換問題
- 30. 純CSS圖像交換
其不符合我的期望,請給我另一個答案。 – Priyanka
我編輯的帖子包括一個工作解決方案,而不是sudo代碼。 – bennythemink