I have come to SO with a question after a long time, I haven’t learned enough yet :(.
I am having a UIScrollView which has UIImageView inside it. The image of the UIImageView is to change with a right and left swipe. I know overriding the touches of UIScrollView will not be a good and efficient solution, so I added a transparent view upon the UIScrollView (not as its subview) covering the bounds of UIScrollView and overridden its touch methods to change the images on swipe.
Now with the help of pinch gesture recognizer added to my transparent view I am able to zoom the UIScrollView and the image programmatically. Till here, I am able to work the things smoothly but I have another typical functionality. I have to move the zoomed image with my finger up and down so that I can see the cropped image. I am doing it with the help of UILongPressGestureRecognizer added to my transparent view. Upon 1 seconds of touch duration I add a UIImageView with a four way arrow image and add it touch location. Now as I move my finger, the fourwayarrow image follows my finger but the image in the scrollview does not follow my finger. Its kind of jerky and often works like I have tied an elastic string. So initially I have to pull the string to some threshold (till that point the scroll view does not move) and when it reaches that level, it suddenly starts moving out of control. I am adding the code here for you people to have a look.
Please feel free to add comments if you people need any other information.
- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer{
CGPoint touchLocation = [longPressGestureRecognizer locationInView:self.transparentLayer];
switch (longPressGestureRecognizer.state) {
case UIGestureRecognizerStatePossible: break;
case UIGestureRecognizerStateBegan:{
if (fourWayArrowImageView == nil) {
fourWayArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4wayarrow.png"]];
[fourWayArrowImageView setBackgroundColor:[UIColor clearColor]];
}
_previousLocation.x = touchLocation.x;
_previousLocation.y = touchLocation.y;
[fourWayArrowImageView setCenter:touchLocation];
[self.transparentLayer addSubview:fourWayArrowImageView];
}
break;
case UIGestureRecognizerStateChanged:{
float dx = (touchLocation.x - _previousLocation.x);
float dy = (touchLocation.y - _previousLocation.y);
CGPoint currentMaxOffset = [self currentMaxOffset];
CGPoint currentOffset = [self.rotatableImageViewContainingScrollView contentOffset];
float theScale = [self.rotatableImageViewContainingScrollView zoomScale];
currentOffset.x -= (dx*theScale);
currentOffset.y -= (dy*theScale);
[fourWayArrowImageView setCenter:touchLocation];
CGSize size = self.rotatableImageViewContainingScrollView.frame.size;
[self.rotatableImageViewContainingScrollView scrollRectToVisible:CGRectMake(currentOffset.x, currentOffset.y, size.width, size.height) animated:YES];
_prevMaxOffset = CGPointMake(currentMaxOffset.x, currentMaxOffset.y);
}
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
case UIGestureRecognizerStateEnded: [fourWayArrowImageView removeFromSuperview]; break;
}
}
Found the solution. Actually I was doing a mistake. I was not updating the previous location after the scrolling and the scrolling should not be animated in this case. So please look at the answer. BTW thanks to all guys who gave time:
So the scrolling in zoom mode now exactly works like photo app.
Hope this helps