2013-08-16 93 views
0

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); 
} 

回答

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模擬器,沒問題。我可以退出並從內存中刪除它,它仍然有效。

不過,這是一個有趣的方式來度過一個下雨的週末。希望這個信息對像我這樣的其他新開發者有用。

相關問題