【问题标题】:UIScrollview, when fully zoomed out is it possible to move the view around the screen?UIScrollview,当完全缩小时是否可以在屏幕上移动视图?
【发布时间】:2015-01-29 10:42:30
【问题描述】:

我试图弄清楚这是否可能。我的UIViewController(与屏幕大小相同)中有一个UIScrollView,设置的内容大小为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;
}

这会按预期进行缩放和平移:

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

【问题讨论】:

    标签: ios xcode uiview uiscrollview


    【解决方案1】:

    这非常有效。该方法来自 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,我认为我没有充分解决这个问题以满足你的要求:

    "但也可以在 640*640 的容器内拖动"

    【讨论】:

    • 感谢您试一试,确实需要能够将其拖到思想中。我想要实现的是图标的 UIView。这个 UView 可以通过捏合来缩小并像一个小虚拟桌面一样在屏幕上移动。
    • @Kex - 是的,我现在很感激。我有一个不同的解决方案,它几乎可以工作,如果它看起来不错,我会在今天晚些时候发布。问题应该需要两个滚动视图。同时你可以看看我前一阵子做的this reply。这不是同一个问题,但它可能会为您提供一些如何操作滚动视图的其他想法。还有这篇 objc.io 文章给出了a very good explanation of scrollViews
    • 如果您的解决方案能够正常工作,我很乐意看到它。我还在研究不同的实现,使用 UIView 并使用 UIGestureRecognizers 平移和调整大小。这可行,但是当您释放 UIScrollView 具有的平移或缩放时,它没有弹性/弹性速度......试图找到解决方法。我会看看那些文章。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    相关资源
    最近更新 更多