【问题标题】:How to bring an image to front in ScrollView如何在 ScrollView 中将图像置于最前面
【发布时间】:2015-06-28 13:44:18
【问题描述】:

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

我的代码:

-(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)];
}

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview


    【解决方案1】:

    您可以使用将子视图置于前面。比如

    [scrollView bringSubviewToFront:_img1];

    【讨论】:

    • 我知道这段代码,但是当中间的那个在前面时,我怎么能把所有的东西都拿回来呢?虽然它是水平的uiscrollview
    • 如果你要添加一个视图或图像,它会按顺序一个一个排列。你不能让所有的都显示在前面。在这种情况下,您可以与设计师一起将三个图像更改为一个并添加它。如果一个层意味着你可以使用 transform 属性。那以同样的情况结束。
    【解决方案2】:

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

    您也可以尝试使用 UICollectionView

    【讨论】:

    • 它看起来是一个非常复杂的项目......我如何用UICollectionView做同样的事情?
    【解决方案3】:
    - (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];
    
    
    }
    

    试试这个

    【讨论】:

    • 很抱歉,这不是我想要的。我想要与上图相同的外观。它应该彼此相邻。
    • 是的,我想要相同的类型。因此,除了中间的图像之外,每个图像都将被带到前面
    • 我已经更新了我的代码。一旦我滚动并且其中一个图像出现在中间,我想将它带到前面,而其他图像则留在后面。
    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2010-09-14
    • 2010-10-02
    • 2014-02-27
    • 1970-01-01
    相关资源
    最近更新 更多