0

我玩弄 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);
}
4

1 回答 1

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

不过,在下雨的周末度过一个有趣的方式。希望这些信息对像我这样的其他新开发人员有用。

于 2013-08-18T16:35:14.420 回答