我必须在一个屏幕上交换四个图像。图像只能在左/右/上/下方向交换,而不能对角。例如,第一张图像可以与其右下方的图像交换,第二张图像只能与左侧和下方的图像交换,依此类推。任何人都可以请帮我关于如何做到这一点。由于在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