UIScrollview,完全缩小后可以在屏幕上移动视图吗?

时间:2015-01-29 10:42:31

标签: ios xcode uiview uiscrollview

我试图弄清楚这是否可行。我的UIScrollView(与屏幕尺寸相同)的UIViewController设置内容大小为640 * 640。滚动视图中的UIView,大小为640 * 640。代码:

#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;
}

按预期缩放和平移:

zoomed in

zoomed out fully

完全缩小后,视图会粘在屏幕左上方。我想要发生的是因为视图缩小是粘在屏幕的中心,但它也可以在640 * 640容器内拖动。这是可能的吗?任何帮助,将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

这非常有效。该方法来自Apple的photoscroller 示例,其中它用于子类滚动视图(在layoutSubviews中)。我发现它在scrollViewDidZoom scrollView委托方法中同样有效。

- (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;
}

编辑

查看自此questions posted以来,我认为我已根据您的要求充分解决了这个问题:

  

&#34;但它也可以在640 * 640容器中拖拽&#34;