0

I am trying to set up nested UIScrollViews. What I have is two scroll views with the same content size 640*640 setup like this: canvasScrollView (a UIScrollView) > contentScrollView (a UIScrollView) > content (a UIView). These are all in a UIViewController. What I want to happen is, when the user pinches to zoom in on the content in contentScrollView the canvasScrollView view zooms out at with same factor and speed. I have tried setting up code like below:

#import "ViewController.h"
#import "ContentView.h"

@interface ViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *canvasScrollView;



@property (strong, nonatomic) UIView *viewForZoom;
@property (strong, nonatomic) ContentView *content;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    contentScrollView.contentSize=CGSizeMake(640 ,640);
    contentScrollView.minimumZoomScale=0.25;
    contentScrollView.maximumZoomScale=1;
    self.content = [[[NSBundle mainBundle] loadNibNamed:@"floatingView" owner:self options:nil] objectAtIndex:0];
    self.viewForZoom=self.content;
    [contentScrollView addSubview:self.content];
    contentScrollView.delegate=self;


    //canvasScrollView was setup in the interface builder, same dimensions as above 320*568
    self.canvasScrollView.contentSize=CGSizeMake(640 ,640);
    self.canvasScrollView.minimumZoomScale=0.25;
    self.canvasScrollView.maximumZoomScale=1;
    self.canvasScrollView.userInteractionEnabled=NO;
    [self.canvasScrollView addSubview:contentScrollView];



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return  self.viewForZoom;
}

My first step is to make sure it is the contentScrollView that zooms and not the canvasScrollView. With my code above the views load up fine but I am unable to pan or zoom the content. I'm not sure why this doesn't work. I have disabled the canvasScrollView from user interaction as later on this will be modified programmatically, I have also set the contentScrollView delegate. Not working though. Would really appreciate some help on this one!!!

Thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

1 Answers1

-1

You have added contentScrollView to a view that has userInteractionEnabled=NO. A view that doesn't allow user interaction does not allow user interaction any of its subviews. Try attaching it to the self.view instead.

[self.view addSubview:contentScrollView];
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • This is incorrect, A scrollView contained within a UIView will still respond to user interaction even if the containing UIView is set to userInteractionEnabled = NO. – MDB983 Jan 29 '15 at 23:14
  • You are mistaken. Have you tried it? Setting `userInteractionEnabled=NO` disables all interaction on that view and all of its subviews -- including and `UIScrollView`s that are subviews. – Ian MacDonald Jan 30 '15 at 14:34
  • indeed i have tried it ... You should too before making a comment. Only Events for the given view are ignored. – MDB983 Jan 30 '15 at 15:07
  • I did make sure to double-check it before I responded to your comment. Not only did I test it with code on a device, but this answer may be helpful to you: http://stackoverflow.com/a/10892551/2708650 – Ian MacDonald Jan 30 '15 at 15:14
  • i'd be more than happy to upload my test that relates directly to this question, I.E a scrollview contained within a UIView (acting as container), with the User Interaction Enabled unchecked for the container. consider a lot has changed since IOS 5. – MDB983 Jan 30 '15 at 15:33
  • A lot has changed, but this is not one of the things that has changed. You are simply incorrect; and that's okay. Just admit it and move on. – Ian MacDonald Jan 30 '15 at 15:34
  • Indeed you should face the fact you're wrong. As i say, i would be more than happy to prove it in code. – MDB983 Jan 30 '15 at 15:39
  • Provide away. Include a video of you swiping content within a `UIScrollView` that's within a `userInteractionEnabled=NO` view, too. – Ian MacDonald Jan 30 '15 at 15:43