gpt4 book ai didi

ios - 如何在 UICollectionView 中以模态方式呈现 UIImageview

转载 作者:行者123 更新时间:2023-11-29 10:56:17 24 4
gpt4 key购买 nike

我目前有一个 UICollectionView,它由图像网格组成。我想要做的是,当我点击任何特定图像时,它应该打开一个 ImageView ,我可以在它周围平移。


问题:

  1. ImageView 不会显示图像。
  2. 如果我为 ImageView 启用平移,整个 GridView 将移动。

如何避免这些问题?

这是我试过的:

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
[self.collectionView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellWithReuseIdentifier:@"CellID"];


// Do any additional setup after loading the view, typically from a nib.
}


-(int)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

return 30;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
cell.backgroundColor=[UIColor whiteColor];
UIImageView *imgView=(UIImageView *)[cell viewWithTag:1];
imgView.image=[UIImage imageNamed:@"57.png"];
return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImageView *previewImage=[[UIImageView alloc]init];
UIScrollView *imageScroll=[[UIScrollView alloc]init];
imageScroll.clipsToBounds=YES;
imageScroll.contentSize=previewImage.bounds.size;
UIViewController *imageController=[[UIViewController alloc]init];
imageController.view.backgroundColor=[UIColor whiteColor];
[imageController.view addSubview:imageScroll];
imageController.modalPresentationStyle=UIModalTransitionStyleCrossDissolve;
imageController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:imageController animated:YES completion:^{

}];

}

有什么建议吗?

最佳答案

一些事情:

您将 UIScrollView 对象 imageScroll 的 contentSize 设置为 UIImageView 对象 previewImage 的大小。因此,您应该设置 previewImage 的框架,以便 imageScroll 知道它的 contentSize 并且您应该设置 imageScroll 的框架以确保它会滚动(记住UIScrollView 的框架必须小于它的 contentSize)。这样的东西会起作用(你应该把它改成你想要的):

UIImageView *previewImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
UIScrollView *imageScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

您也没有将 previewImage 添加为 imageScroll 的 subview 或设置其图像。

previewImage.image = //set image;
[imageScroll addSubview:previewImage];

通过正确设置 UIScrollView,用户可以轻松地在 imageView 周围平移。

关于ios - 如何在 UICollectionView 中以模态方式呈现 UIImageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com