在Ios Simulator中重新启动应用程序时,swipeGestureRecognizer无法正常工作
问题描述:
Im正在使用SwipeGestureRecognizer。 它按照我的预期工作,但是当我在模拟器中退出应用程序并再次运行它时,SwipeGestureRecognizer不再响应。如果我退出iOS模拟器后再次运行它,它可以工作。在Ios Simulator中重新启动应用程序时,swipeGestureRecognizer无法正常工作
这是我曾尝试:
#import <UIKit/UIKit.h>
@interface swipeTestUI : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)mangeSwipeControl:(UIGestureRecognizer*)sender;
@end
实现文件:
#import "swipeTestUI.h"
@interface swipeTestUI()
@end
@implementation swipeTestUI
@synthesize imageView;
int listOfImages = 0;
- (IBAction)mangeSwipeControl:(UIGestureRecognizer *)sender {
NSLog(@"swipe ok");
NSArray *images=[[NSArray alloc]initWithObjects:
@"1.png",
@"2.png",
@"3.png",
@"4.png",
@"5.png",
@"5.png",nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
listOfImages++;
break;
case UISwipeGestureRecognizerDirectionRight:
listOfImages--;
break;
default:
break;
}
listOfImages = (listOfImages < 0) ? ([images count] -1):listOfImages % [images count];
imageView.image = [UIImage imageNamed:[images objectAtIndex:listOfImages]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
答
我觉得这是一款iOS模拟器问题。我修改了一下代码,我觉得它的效果更好
头文件是一样的,执行文件如下。
-(IBAction)manageSwipeControl:(UISwipeGestureRecognizer *)gesture {
NSArray *images=[[NSArray alloc]initWithObjects:
@"1.png",
@"2.png",
@"3.png",
@"4.png",
@"5.png",
@"6.png",nil];
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
{
listOfImages++;
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
{
listOfImages--;
}
listOfImages = (listOfImages < 0) ? ([images count] -1):listOfImages % [images count];
imageView.image = [UIImage imageNamed:[images objectAtIndex:listOfImages]];
//[self.imageView removeGestureRecognizer:sender];
}
工程就像一个魅力。有趣的事情。当我在iOS模拟器中退出应用程序并重新打开它时。它仍然有效。如果我退出并从iOS模拟器内存中删除它 - 它不会。如果我直接从操作系统启动iOS模拟器,没问题。我可以退出并从内存中删除它,它仍然有效。
不过,这是一个有趣的方式来度过一个下雨的周末。希望这个信息对像我这样的其他新开发者有用。