在Ios Simulator中重新启动应用程序时,uiswipeGestureRecognizer无法正常工作

时间:2013-08-16 17:04:55

标签: ios-simulator uiswipegesturerecognizer

我正在玩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);
}

1 个答案:

答案 0 :(得分:0)

我认为这是一个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模拟器,没有问题。我可以退出并从内存中删除它,它仍然有效。

仍然是一个在下雨的周末度过的有趣方式。希望这些信息对像我这样的其他新开发者有用。