0

我正在工作一个应用程序。在这个随机数的时间图像是滚动的..然后停止图像..在我的代码中,第一个随机数是生成的,它存储在 n..例如。随机否生成 12 次,然后滚动 12 次图像,然后停止..

- (void)viewDidLoad
{
int n = 1 + arc4random() % 10 + 10;

    NSString *string = [NSString stringWithFormat:@"%i",n];

    //lbl.text = string;

    NSUInteger i;
    for (i = 1; i <= kNumImages; i++)
    {


       // [kNumImages addObject:[NSNumber numberWithInt:i]];
        NSString *imageName = [NSString stringWithFormat:@"%d.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  
        lbl.text = string;
        [FirstScrollView addSubview:imageView];
        [imageView release];

        if(n == i){
          break;       

    }

}

我想在随机不匹配时停止滚动意味着n == i我该怎么做?

4

2 回答 2

0

[firstScrollView setContentOffset:CGRectMake(n*firstScrollView.frame.size.width,firstScrollView.frame.size.height)];

您只需要设置滚动视图的内容偏移量。

于 2012-10-29T06:18:36.720 回答
0

尝试这个,

- (void)viewDidLoad
{
int n = 1 + arc4random() % 10 + 10;

    NSString *string = [NSString stringWithFormat:@"%i",n];

    //lbl.text = string;

    NSUInteger i;
    for (i = 1; i <= kNumImages; i++)
    {
        if(n == i)
          break;
        else {

       // [kNumImages addObject:[NSNumber numberWithInt:i]];
        NSString *imageName = [NSString stringWithFormat:@"%d.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  
        lbl.text = string;
        [FirstScrollView addSubview:imageView];
        [imageView release];

          }    

    }

}
于 2012-10-29T06:06:17.053 回答