1

我在滚动视图下有 imageView。当我从滚动视图中选择特定图像时,它会显示屏幕中心。当我选择另一个图像时,前一个图像将消失。我不能在屏幕上显示多个图像。

代码:

- (void)viewDidAppear:(BOOL)animated {
  myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 10, 200, 200)];
    [self.view addSubview:myImageView];
}

- (void)loadNextPage:(int)index {  
    int countFlag = 0;
    for(int i=index*4;i<(index+1)*4;i++)
    {
        UIButton *imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)];
        imageView.tag=i+1;
        [imageView addTarget:self action:@selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
        //[imageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        // [imageView.layer setBorderWidth:1.0f];
        switch ((i+1)%15) {
            case 0:
                [imageView setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
                break;                    
            case 1:
                [imageView setImage:[UIImage imageNamed:@"img1.png"]  forState:UIControlStateNormal];
                break;
            case 2:
                [imageView setImage:[UIImage imageNamed:@"img2.png"]  forState:UIControlStateNormal];
                break;
            case 3:
                [imageView setImage:[UIImage imageNamed:@"img3.png"]  forState:UIControlStateNormal];
                break;
            case 4:
                [imageView setImage:[UIImage imageNamed:@"img4.png"]  forState:UIControlStateNormal];
                break;
            case 5:
                [imageView setImage:[UIImage imageNamed:@"img5.png"]  forState:UIControlStateNormal];
                break;
        break;
        }

        [myScrollView addSubview:imageView];

        countFlag++;
   }
 }

- (void)imageViewClicked:(id)sender{
    switch ([sender tag]%15) {
        case 0:
            [myImageView setImage:[UIImage imageNamed:@"img.png"]];
            // myImageView.tag=1;
            [myImageView setTag:0];
            break;
        case 1:
            [myImageView setImage:[UIImage imageNamed:@"img1.png"]];
            //  myImageView.tag=2;
            [myImageView setTag:1];
            break;
        case 2:
            [myImageView setImage:[UIImage imageNamed:@"img2.png"]];
            [myImageView setTag:2];
            break;
        case 3:
            [myImageView setImage:[UIImage imageNamed:@"img3.png"]];
            [myImageView setTag:3];
            break;
        case 4:
            [myImageView setImage:[UIImage imageNamed:@"img4.png"]];
            [myImageView setTag:4];
            break;
        case 5:
            [myImageView setImage:[UIImage imageNamed:@"img5.png"]];
            [myImageView setTag:5];
            break;
    }

    [self.view addSubview:myImageView];
}
4

0 回答 0