1

I am trying to figure out if this is possible or not. I have a UIScrollView in my UIViewController (the same size as the screen) the set content size is 640*640. The UIView in the scroll view, size 640 *640. Code :

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UIView *viewForZoom;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2;
    //self.profileImageView.clipsToBounds = YES;


    UIView *content = [[[NSBundle mainBundle] loadNibNamed:@"floatingView" owner:self options:nil] objectAtIndex:0];
    self.viewForZoom=content; //need this pointer for the zoom
    self.scrollView.contentSize=CGSizeMake(640 ,640);

    self.scrollView.minimumZoomScale=0.25;
    self.scrollView.maximumZoomScale=1;

    [self.scrollView addSubview:content];
}

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

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

    return  self.viewForZoom;
}

This zooms and pans as expected :

zoomed in

zoomed out fully

When fully zoomed out the view is stuck to the top left on the screen. What I want to happen is as the view zooms out is sticks to the centre of the screen but it can also be dragged around within the 640*640 container. Is this something that is possible? Any help would be appreciated. Thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

1 Answers1

0

This works quite effectively. The approach comes from Apple's photoscroller example, where it is used in a subclassed scrollview (in layoutSubviews). I find it works just as effectively in scrollViewDidZoom scrollView delegate method.

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect imageViewFrame = self.imageView.frame ;

    // centre horizontally
    if (imageViewFrame.size.width < boundsSize.width) {
        imageViewFrame.origin.x = (boundsSize.width - imageViewFrame.size.width) / 2;
    } else {
        imageViewFrame.origin.x = 0;
    }

    // centre vertically
    if (imageViewFrame.size.height < boundsSize.height && self.imageView.image) {
        imageViewFrame.origin.y = (boundsSize.height - imageViewFrame.size.height) / 2;
    } else {
        imageViewFrame.origin.y = 0;
    }
    self.imageView.frame = imageViewFrame;
}

edit

Looking at the questions you have posted since this one, I don't think I have addressed this issue adequately for your requirements:

"but it can also be dragged around within the 640*640 container"

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • Thanks for giving it a shot, do need to be able to drag it around thought. What I'm trying to achieve is a UIView of icons. This UView can be pinched to zoom out and moved around the screen like a little virtual desktop. – Kex Jan 30 '15 at 07:28
  • @Kex - yes I appreciate that now. I have a different solution which is _almost_ working, if it looks good I will post it up later today. The problem should _not_ require two scrollViews. Meanwhile you might take a look at [this reply](http://stackoverflow.com/a/16368015/1375695) I made a while ago. It's not the same problem, but it might give you some other ideas how to manipulate scrollViews. Also this _objc.io_ article gives [a very good explanation of scrollViews](http://www.objc.io/issue-3/scroll-view.html) – foundry Jan 30 '15 at 08:39
  • I would love to see your solution if you get it working. I am also working on a different implementation, using a UIView and panning and resizing with UIGestureRecognizers. This works but it doesn't have the bouncy/springy speed when you release a pan or zoom that the UIScrollView has....trying to figure a way around it. I'll take a look at those articles. Thanks – Kex Jan 30 '15 at 08:45