How to implement Swipe Gesture for UIImageView randomizer

不羁的心 提交于 2020-01-13 14:13:12

问题


i've been looking through loads of tutorials and questions about this but can't seem to find what I'm looking for and I have a feeling I'm just missing a simple step.. I'm still learning the ropes so bear with me on this one..

I'm making an image randomizer on xcode 4.3.3 and i have been able to do it with a button to randomize the images, but i want it to respond to a swipe gesture. the whole window is covered by a UIImageView object so i just want an app that randomizes images by swiping so i want to be able to swipe the UIImageview. this is what I have:

in my .h file:

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

}

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

@end

and in my .m file:

@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

I thought it would be possible to connect the 'random' action to a swipe gesture in storyboard, but it hasn't worked. the thread 1 message is:

2012-07-27 15:04:32.012 QuoteRandom[1057:c07] -[ViewController screenWasSwiped]: unrecognized selector sent to instance 0x6868210

so i think i have to connect 'screenWasSwiped' to the image randomizer and also apply the swipe gesture to the UIImageView but I am struggling to figure it out. I would appreciate any guidance! thanks in advance!


回答1:


You can call your -(IBAction)random method in your UISwipeGestureRecognizer changing it init method to:

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

This way, every time you swipe you will get a new random image.



来源:https://stackoverflow.com/questions/11689519/how-to-implement-swipe-gesture-for-uiimageview-randomizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!