2012-07-27 65 views
3

我一直在尋找大量的教程和有關這個問題,但似乎無法找到我在找什麼,我有一種感覺,我只是缺少一個簡單的步驟..我仍然在學習繩索,所以忍受着我在這一個..如何實現UIImageView隨機工具的刷卡手勢

我正在Xcode 4.3.3上製作一個圖像隨機數發生器,我已經能夠做到這一點與隨機化圖像的按鈕,但我希望它響應一個輕掃手勢。整個窗口被UIImageView對象覆蓋,所以我只想要一個應用程序通過刷卡來隨機化圖像,所以我想能夠刷新UIImageView。這就是我:

在我的.h文件:

@interface ViewController : UIViewController <UIGestureRecognizerDelegate> { 
IBOutlet UIImageView *quotePage; 

} 

-(IBAction)random; 
-(void)screenWasSwiped; 

@end 

,在我的.m文件:

@interface ViewController() 

@end 

@implementation ViewController 

-(IBAction)random { 
int image = rand() % 4; 
switch (image) { 
    case 0: 
     quotePage.image = [UIImage imageNamed:@"Quote1.png"]; 
     break; 
    case 1: 
     quotePage.image = [UIImage imageNamed:@"Quote2.png"]; 
     break; 
    case 2: 
     quotePage.image = [UIImage imageNamed:@"Quote3.png"]; 
     break; 
    case 3: 
     quotePage.image = [UIImage imageNamed:@"Quote4.png"]; 
     break; 
    default: 
     break; 
} 
} 


-(void)viewDidLoad { 
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]   initWithTarget:self action:@selector(screenWasSwiped)]; 
swipeLeft.numberOfTouchesRequired = 1; 
swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft; 
[self.view addGestureRecognizer:swipeLeft]; 
quotePage.userInteractionEnabled = YES; 

} 

@end 

我認爲這將有可能連接「隨機」行動在故事板中滑動手勢,但它沒有奏效。線程1的信息是:

2012-07-27 15:04:32.012 QuoteRandom [1057:C07] - [視圖控制器screenWasSwiped]:無法識別的選擇發送到實例0x6868210

所以我想我已連接「 screenWasSwiped'到圖像隨機工具,並將滑動手勢應用於UIImageView,但我正在努力弄清楚。我將不勝感激任何指導!提前致謝!

回答

2

你可以叫你-(IBAction)random方法在UISwipeGestureRecognizer改變它init方法:

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(random)]; 

這樣,每次你刷卡,你會得到一個新的隨機圖像的時間。

+0

多數民衆贊成在它做到了!謝謝! – Buzzd 2012-07-27 14:48:17