计算从一些UIViews中随机抽取的UIViews的数量,即:从9个UIViews中抽取的随机5个UIViews,然后执行操作

时间:2014-10-18 12:14:55

标签: ios objective-c random uiview uigesturerecognizer

我是Objective c的新手并且已经环顾了几天但找不到答案。我在容器视图中有9个imageViews,每个只能被点击一次,我想在随机抽取5个UIImageViews后做一些事情。这是我用于每个人体验一次点击的代码。如果必须更改我的代码,请提供任何帮助。

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveToNextMovingRectangle:)];

    tapGestureRecognizer.numberOfTouchesRequired = 1;
    tapGestureRecognizer.numberOfTapsRequired = 1;

    [self.imageView1 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView2 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView3 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView4 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView5 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView6 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView7 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView8 addGestureRecognizer:tapGestureRecognizer];
    [self.imageView9 addGestureRecognizer:tapGestureRecognizer];

//    if (5 random imageShapes have been tapped){
//        
//        do something
//    }

2 个答案:

答案 0 :(得分:0)

创建一个新的计数器变量并在每次用户点击图像时增加它的值,当它达到5时你可以做任何你想做的事。就像这样:

首先在ViewController类声明globel计数器变量,如:

var counter : Int = Int()


UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveToNextMovingRectangle:)];

tapGestureRecognizer.numberOfTouchesRequired = 1;
tapGestureRecognizer.numberOfTapsRequired = 1;

[self.imageView1 addGestureRecognizer:tapGestureRecognizer];
[self.imageView2 addGestureRecognizer:tapGestureRecognizer];
[self.imageView3 addGestureRecognizer:tapGestureRecognizer];
[self.imageView4 addGestureRecognizer:tapGestureRecognizer];
[self.imageView5 addGestureRecognizer:tapGestureRecognizer];
[self.imageView6 addGestureRecognizer:tapGestureRecognizer];
[self.imageView7 addGestureRecognizer:tapGestureRecognizer];
[self.imageView8 addGestureRecognizer:tapGestureRecognizer];
[self.imageView9 addGestureRecognizer:tapGestureRecognizer];
self.moveToNextMovingRectangle()

}


   func moveToNextMovingRectangle(){ 

 // Increment in counter value 
    counter += 1;
    if (counter>=5)
    {
        //do what ever you want
    }
    else {
        //get a random number using  arc4random() method and show your next imageview
    }
}

我认为这样可以尝试。

答案 1 :(得分:0)

假设我们已经在nsarray和计数器中保存了我们的imagesviews以跟踪水龙头

NSArray *imgViews = @[self.imageView1,self.imageView2.....,self.imageView9];  
int counter;

之后在你的选择器中执行此操作

-(void) moveToNextMovingRectangle{  
counter +- 1;
    if (counter>=5)
    {
        //do what ever you want
    }
    else {
        //get a random number using  arc4random() method and show your next imageview
    }
}