随机生成器实现滑动手势

随机生成器实现滑动手势

本文介绍了如何为 UIImageView 随机生成器实现滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览大量关于此的教程和问题,但似乎找不到我要找的东西,我觉得我只是错过了一个简单的步骤..我仍在学习绳索让我忍受这个..

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..

我正在 xcode 4.3.3 上制作图像随机化器,我已经能够使用按钮来随机化图像,但我希望它能够响应滑动手势.整个窗口都被 UIImageView 对象覆盖,所以我只想要一个通过滑动来随机化图像的应用程序,所以我希望能够滑动 UIImageview.这就是我所拥有的:

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:

在我的 .h 文件中:

in my .h file:

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

}

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

@end

在我的 .m 文件中:

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

我认为可以将随机"动作与故事板中的滑动手势联系起来,但没有奏效.线程 1 消息是:

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]:无法识别的选择器发送到实例 0x6868210

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

所以我想我必须将screenWasSwiped"连接到图像随机化器,并将滑动手势应用于 UIImageView,但我正在努力弄清楚.我将不胜感激任何指导!提前致谢!

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!

推荐答案

你可以在你的 UISwipeGestureRecognizer 中调用你的 -(IBAction)random 方法来改变它 init 方法:

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.

这篇关于如何为 UIImageView 随机生成器实现滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 03:09