[英]How to bring an image to front in ScrollView
我正在ScrollView
显示图像,但是我想知道如何在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)];
}
您可以使用将子视图置于最前面。 例如
[scrollView bringSubviewToFront:_img1];
我明白你的意思了,有一个有趣的项目 。 它应该可以帮助您。
您还可以尝试使用UICollectionView 。
- (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];
}
尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.