Having ScrollView.contentSize issues not able to scroll properly on Top, Left, Right?

317 views Asked by At

Having a Child-View inside Scroll- View and getting issues in Scroll on Top, Left, Right ?

Code for adding Pinch & Zoom in Child Sub-View

 UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(twoFingerPinch:)]
                                                autorelease];

    [_childView addGestureRecognizer:twoFingerPinch];

Method For Pinch & Zoom a Child-View

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    //NSLog(@"Pinch scale: %f", recognizer.scale);
    CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
    // you can implement any int/float value in context of what scale you want to zoom in or out
    _childView.transform = transform;

    _scrollView.contentSize =
    CGSizeMake(_scrollView.frame.size.width,_childView.frame.size.height);

}

Is there any issues in Scroll-View ContentSize setting ?

Here is my Code

Code Link

Thank you

5

There are 5 answers

2
Mavericks On BEST ANSWER

Edit:

Set _scrollview frame size and _childview frame size same:

//Frame for scrollview 56, 92, 196, 408.

_scrollView.delegate = self;

_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 10;

_childView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 196, 408)];

_childView.backgroundColor=[UIColor blackColor];

[_scrollView addSubview:_childView];

 [_scrollView setContentSize:_childView.frame.size];

and implement this delegate

 - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
    return _childView;
  }

Also check your auto resizing flag in XIB. it is set to bottom margin.

1
Apurv On

You are passing scrollView's width to contentsize. You should pass childView's width.

_scrollView.contentSize = CGSizeMake(_chilView.frame.size.width,_childView.frame.size.height);

0
johnMa On

You should not get _childView.frame.size.height after CGAffineTransform.

This question explain it well.

0
Tarun On

I'm not 100% sure of your current scenario.

But after iOS5, UIScrollView has following :-

// `pinchGestureRecognizer` will return nil when zooming is disabled.
@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);

From the description, it appears that if you set these-

@property(nonatomic) float minimumZoomScale;     // default is 1.0
@property(nonatomic) float maximumZoomScale;     // default is 1.0. must be > minimum zoom scale to enable zooming

You can simply enable zooming using these properties. And zoom-in & zoom-out should work for you automatically without having to implement your own UIGestureRecognizer.

0
Nitesh Meshram On

I Have removed all the code and changes with below code I have done very stupid mistake :)

   _scrollView.contentSize = CGSizeMake(_childView.frame.size.width, _childView.frame.size.height);
        _scrollView.maximumZoomScale = 6.0;
        _scrollView.minimumZoomScale = 1.0;
        _scrollView.clipsToBounds = YES;
        _scrollView.delegate = self;


    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    //    NSLog(@"View - %@\n",_childView);
        return _childView;
    }

Thanx @Mavericks

:)