2

I have scrollview and added UIPinchGestureRecognizer to scrollview. Now on gesture event I have zoom in or out scrollview and its handle action is as following :

static float lastScale = 1.0;

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
    {
        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

        // Constants to adjust the max/min values of zoom
        const CGFloat kMaxScale = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
        [gestureRecognizer view].transform = transform;

        lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
    }
}

I want to change contentSize in this method. Now, my question is that how to set scrollview content size so that after zoom out I can scroll to see all the content in scrollview.

Please help.

VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • if your intense is zoom In/out then you dont need to apply gesture and set content size manually. Just take one UIView and add all subviews inside of it and implement UIScrollView delegate viewForZoomingInScrollView and return UIView and set minimumZoomScale, maximumZoomScale for UIScrollView because UIScrollView has already buildIN this fuctionality. – Bandish Dave Dec 15 '15 at 05:49
  • Okay... I will try and let you know – VRAwesome Dec 15 '15 at 06:08
  • show my code in answer, make right if it is true so it will helpful for others. – Bandish Dave Dec 15 '15 at 07:12

1 Answers1

1
@interface HorizontalScrollView ()
{
    UIView *container;
}
@end

@implementation HorizontalScrollView
@synthesize scrlView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    container = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.scrlView.frame.size.width, self.scrlView.frame.size.height)];


    UIImage *image = [UIImage imageNamed:@"1.jpeg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CGRect rect = imageView.frame;
    rect.size.height = 215;
    rect.size.width = self.scrlView.frame.size.width;
    rect.origin = CGPointMake(0, 0);
    imageView.frame = rect;
    [container addSubview:imageView];

    [scrlView addSubview:container];

    self.scrlView.minimumZoomScale=1.0;
    self.scrlView.maximumZoomScale=6.0;
    self.scrlView.delegate=self;

    // set the content size so it can be scrollable
    [self.scrlView setContentSize:CGSizeMake(1*self.scrlView.frame.size.width, 0)];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return container;
}
Bandish Dave
  • 791
  • 1
  • 7
  • 27