Reputation: 835
I guess its duplicated question, but I have checked all the info about that on stackoverflow and I'm not able to figure it out.
After zooming
in the Image of a UICollectionViewCell
, I want to pan the Image, but only until the borders of the UIScreen
/parent view.
This is what's happening:
This is the code I use for Scaling
& Pan
:
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
if (self.pinchGesture.scale<1.0){
return;
}
if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self.superview];
CGPoint newcenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
[self setCenter:newcenter];
[recognizer setTranslation:CGPointZero inView:self];
}
}
- (void)doPinch:(UIPinchGestureRecognizer *)gesture
{
if([gesture state] == UIGestureRecognizerStateBegan) {
previousScale = 1.0;
}
CGFloat currentScale = [[self.layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 4.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (previousScale - [gesture scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
self.transform = CGAffineTransformScale(self.transform, newScale, newScale);
}
Thanks in advance ;)
Upvotes: 1
Views: 261
Reputation: 835
Finally I use the zoom that implements UIScrollView
. I added a UIScrollView
to my UICollectionViewCell
.
And I've set it up the minimumZoomScale
, maximumZoomScale
, zoomScale
of the UIScrollView
, and implemented the viewForZoomingInScrollView
UIScrollViewDelegate
method on my Custom UICollectionViewCell
, to get running the zoom
functionality in the UIScrollView
.
That's how looks the cellForItemAtIndexPath
: method of my UICollectionView
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
SBQPhotosDetailCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.imageDetail=[[UIImageView alloc] initWithFrame:cell.bounds];
cell.imageDetail.contentMode=UIViewContentModeScaleAspectFit;
cell.scrollView.contentSize = CGSizeMake(cell.imageDetail.frame.size.width, cell.imageDetail.frame.size.height);
;
cell.imageDetail.transform = CGAffineTransformIdentity;
cell.scrollView.delegate=cell;
cell.scrollView.maximumZoomScale=4.0;
cell.scrollView.minimumZoomScale=1.0;
cell.scrollView.zoomScale=1.0;
[cell.scrollView addSubview:cell.imageDetail];
[cell.imageDetail setImageWithURL:[NSURL URLWithString:[self.arrayURLS objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:@"thunder"]];
cell.delegate = self;
return cell;
}
Upvotes: 1