我必须生成介于11到20之间的随机数并将其存储在按钮中。每次生成随机数并在iPhone中随机抽取两个图像的时间 例如,如果随机数为12,则图像滑动12次,然后滑动将停止。
我已经生成了随机数,但无法使用随机数滑动图像。
这是我的代码
-(IBAction)btngeneratePressed:(id)sender{
number = 1 + arc4random() % 10 + 10;
for (int j=1;j<number-1;j++) // generate unique random number only once
{
NSArray *myImageNames = [NSArray arrayWithObjects:@"1.png", @"2.png", nil];
//int index = arc4random() % [myImageNames count];
int index = arc4random() % 2;
UIImage *myImage = [UIImage imageNamed:[myImageNames objectAtIndex:index]];
img.image = myImage;
if(number == j){
NSLog(@"Number %d ",j);
UIAlertView *someError = [[UIAlertView alloc] initWithTitle: @"Rendom number" message: @"Image swip" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[someError show];
[someError release];
break;
}
}
}
- (void)viewDidLoad
{
number = 1 + arc4random() % 10 + 10;
NSString *string=[NSString stringWithFormat:@"%i",number];
int p=string;
label.text = string;
for(int j=1;j<number-1;j++)
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(btngeneratePressed:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
recognizer.numberOfTouchesRequired = 1;
recognizer.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:recognizer];
[recognizer release];
//Add a right swipe gesture recognizer
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(btngeneratePressed:)];
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
if(number == j){
NSLog(@"Number %d ",j);
UIAlertView *someError = [[UIAlertView alloc] initWithTitle: @"Rendom number" message: @"Image swip" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[someError show];
[someError release];
break;
}
}
}
答案 0 :(得分:1)
我并不完全明白你想要做什么,但是我会把它分成几块来解决问题,而不是试图一次性完成这一切,而且所有这些都混乱了。
将psedo-random唯一编号生成器排序为一次任务,并将其封装在类/方法中。 弄清楚如何滑动图像并捕获/计数单个,然后多次滑动 整理图像显示 将它们连接在一起
现在你正在生成数字1滑动手势识别器,这绝对不对。你想要其中一个。
每次调用btngeneratePressed时,你都会重新生成所有图像:这绝对是低效的,而且很慢。
将问题分解为不连续的部分并解决每个部分,以构建您需要的最终解决方案。