我正在使用SwipeGestureRecognizer玩弄。它按预期运行,但是当我在模拟器中退出应用程序并再次运行时,SwipeGestureRecognizer不再响应。如果我在退出iOS模拟器后再次运行它,它将起作用。
这是我尝试过的:
#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模拟器内存中删除-不会。如果我直接从OS启动iOS模拟器,那么就没问题了。我既可以退出,也可以将其从内存中删除,但它仍然有效。
不过,在下雨的周末度过的一种有趣的方式。希望此信息对像我这样的其他新开发人员有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句