Reputation: 706
I currently have a UICollectionView
which consists of grid of images. What I want to do is, when I click on any particular image it should open a image view and I can pan around it.
Issues:
How do I avoid these problems?
This is what I have tried:
- (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:^{
}];
}
Any pointers?
Upvotes: 1
Views: 1861
Reputation: 2024
What you are effectively trying to do is build a viewcontroller hiearchy. You cannot do this as you have tried. You need to present it as a pop over controller at best for the UIImageView to even show. But then you cannot move it. So your approach wont work.
One idea might be to disable userinteraction on the UICollectionView and present your UIImageView as a subview on top of UICollectionView and then controller panning in the same controller that manages the UICollectionView.
Upvotes: 1
Reputation: 1973
A couple of things:
You set the contentSize of your UIScrollView
object imageScroll
to be the size of the UIImageView
object previewImage
. Therefore you should set the frame of previewImage
so imageScroll
knows its contentSize and you should set the frame of imageScroll
to ensure that it will scroll (remember the frame of a UIScrollView
must be smaller than its contentSize). Something like this will work (you should change this to what you want):
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)];
You also did not add previewImage
as a subview to imageScroll
or set its image.
previewImage.image = //set image;
[imageScroll addSubview:previewImage];
With your UIScrollView
set up correctly the user can easily pan around the imageView.
Upvotes: 1