如何在ScrollView中将图像置于最前面

时间:2015-04-21 11:53:22

标签: ios objective-c uiscrollview uiimageview

我在ScrollView中显示图像,但我想知道如何在滚动的同时将每张图像放在中间的正面。它也在背面,如下图所示。

enter image description here

我的代码:

-(void)scrollView2{

    _scrlView.backgroundColor = [UIColor blackColor];
    _scrlView.pagingEnabled = NO;
    _scrlView.bounces = false;
    _scrlView.delegate = self;
    _scrlView.showsHorizontalScrollIndicator = YES;
    _scrlView.layer.cornerRadius = 2;
    _scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width, _scrlView.frame.size.height);

    _scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 2, _scrlView.frame.size.height);

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    int segment = screenWidth / 5;

    int screenX2 = screenHeight/2 - segment/2;

    __block float x = (screenWidth-segment)/2;

    NSArray *arrImages=[NSArray arrayWithObjects:@"Smiley-1.png",@"Smiley-2.png",@"Smiley-3.png",@"Smiley-4.png",@"Smiley-5.png", nil];

    for(int i=0;i<arrImages.count;i++)
    {
        UIImageView *imgVw=[[UIImageView alloc]init];
        [imgVw setFrame:CGRectMake(x, screenX2, segment, segment)];
        [imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrImages objectAtIndex:i]]]];
        [imgVw setBackgroundColor:[UIColor redColor]];
        [imgVw setContentMode:UIViewContentModeScaleToFill];
        [_scrlView addSubview:imgVw];

        x=x+segment;
    }

    [self.view addSubview:_scrlView];
}

float oldY;

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

  int scrollX = scrollView.contentOffset.x;
  int position = scrollX/segment;

  NSLog(@"X: %f , position: %d", scrollView.contentOffset.x , position);

  [_scrlView setContentOffset: CGPointMake(_scrlView.contentOffset.x, oldY)];
}

3 个答案:

答案 0 :(得分:0)

您可以使用前面的子视图。例如

[scrollView bringSubviewToFront:_img1];

答案 1 :(得分:0)

我理解你的意思,有一个有趣的project。它应该对你有帮助。

您也可以尝试使用 UICollectionView

答案 2 :(得分:0)

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSArray *arrImages=[NSArray arrayWithObjects:@"smiley1",@"smiley2",@"smiley3",@"smiley4",@"smiley5",@"smiley6", @"smiley7",@"smiley8", nil];

   screenRect = [[UIScreen mainScreen] bounds];
   screenWidth = screenRect.size.width;
   screenHeight = screenRect.size.height;

   UIScrollView *scrlView=[[UIScrollView alloc]init];
   [scrlView setFrame:CGRectMake(0, 0, 320, 568)];
   scrlView.backgroundColor = [UIColor blackColor];
   scrlView.pagingEnabled = YES;
   scrlView.bounces = false;
   scrlView.delegate = self;
   scrlView.showsHorizontalScrollIndicator = YES;
   scrlView.layer.cornerRadius = 2;

  segmentWidth = screenWidth / 5;

   long ScrollContentSize = ((arrImages.count -1)*segmentWidth)+screenWidth;

   scrlView.contentSize = CGSizeMake(ScrollContentSize, scrlView.frame.size.height);

   float screenX2 = screenHeight/2 - segmentWidth/2;

   __block float screenX = (screenWidth-segmentWidth)/2;

   for(int i=0;i<arrImages.count;i++)
   {
      UIImageView *imgVw=[[UIImageView alloc]init];
      [imgVw setFrame:CGRectMake(screenX, screenX2, segmentWidth+30, segmentWidth+30)];
      [imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrImages objectAtIndex:i]]]];
      [imgVw setTag:5000+i];
      [imgVw setContentMode:UIViewContentModeScaleAspectFit];
       screenX = screenX+segmentWidth;
      [scrlView addSubview:imgVw];

      if (i%2==0)
      {
         [imgVw setBackgroundColor:[UIColor clearColor]];
         [scrlView sendSubviewToBack:imgVw ];

       }
      else
       {
         [imgVw setBackgroundColor:[UIColor clearColor]];
         [scrlView bringSubviewToFront:imgVw];

       }

   }

   [self.view addSubview:scrlView];


}

试试这个