问题
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