Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改UIScrollView中内容的中心_Ios_Xcode_Uiscrollview_Uiimageview - Fatal编程技术网

Ios 更改UIScrollView中内容的中心

Ios 更改UIScrollView中内容的中心,ios,xcode,uiscrollview,uiimageview,Ios,Xcode,Uiscrollview,Uiimageview,我有一个UIScrollView,内容是UIImageView。它显示精细,滚动精细。没问题。图像比屏幕宽得多,因此我希望能够在scrollview中“自动居中”图像的特定部分(坐标) 不过,这就是我被困的地方。所需的居中位置将根据应用程序的使用动态更改,因此我需要能够对其进行配置,以便加载时图像视图(x,y)的所需坐标自动居中于scrollview内。它不需要自动滚动/设置动画,只需要在那里 在使用手势/缩放时,我看到过类似的问题/答案,但在这里两者都没有使用。只是滚动而已。我只需要能够有一个

我有一个UIScrollView,内容是UIImageView。它显示精细,滚动精细。没问题。图像比屏幕宽得多,因此我希望能够在scrollview中“自动居中”图像的特定部分(坐标)

不过,这就是我被困的地方。所需的居中位置将根据应用程序的使用动态更改,因此我需要能够对其进行配置,以便加载时图像视图(x,y)的所需坐标自动居中于scrollview内。它不需要自动滚动/设置动画,只需要在那里

在使用手势/缩放时,我看到过类似的问题/答案,但在这里两者都没有使用。只是滚动而已。我只需要能够有一个设置的图像部分是集中在滚动视图时,它第一次加载


我希望我说得有道理。谢谢您的时间。

我现在正在使用iPhone,因此如果此代码有点古怪,请原谅,因为如果不先调用size.center,我无法准确地记得是否可以使用center属性

CGPoint centerOffset = CGPointMake(0, [scrollView contentSize].(size).center);
[scrollView setContentOffset: centerOffset animated: YES]; // (or No, depends on you)

不确定这是否是你想要的,但我只是想把它放在那里,这个方法有用吗

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [scroller frame].size.height / scale;
    zoomRect.size.width  = [scroller frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}
还是这个

- (CGRect)zoomRectForScale:(float)scale withOrigin:(CGPoint)origin {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [scroller frame].size.height / scale;
    zoomRect.size.width  = [scroller frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = origin.x;
    zoomRect.origin.y    = origin.y;

    return zoomRect;
}

注意:这些不是我写的,它们来自苹果提供的示例代码,但我偶尔会用到它们,当我看到你的问题时,我想到了它们。

在研究了几天这个问题后,我使用ScrollView属性-contentInset想出了一个简单的解决方案。 这将始终使图像居中,即使在缩放时也是如此(如果遵循所有说明)

布局子视图时:

- (void)layoutScrollView
{
    if (!self.image) return;

    CGFloat heightScale = self.frame.size.height / self.image.size.height;
    CGFloat widthScale = self.frame.size.width / self.image.size.width;
    CGFloat scale = MIN(widthScale, heightScale);

    self.scrollView.minimumZoomScale = scale;
    self.scrollView.maximumZoomScale = MAX(1.0f ,scale * 2.0f);
    [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:NO];

    [self centerImage];
}

只需在scrollview中设置其位置即可。例如:

- (void)centreImageView
{
    CGRect bounds = self.bounds;
    CGSize contentSize = self.contentSize;
    CGFloat offsetX = MAX(0, (bounds.size.width - contentSize.width) * 0.5f);
    CGFloat offsetY = MAX(0, (bounds.size.height - contentSize.height) * 0.5f);

    _myContentView.center = CGPointMake(contentSize.width * 0.5 + offsetX, contentSize.height * 0.5 + offsetY);
}

如果您正在缩放,请在您的初始化中以及从
-(void)scrollViewDidZoom:(UIScrollView*)scrollView
中调用它。

重新阅读您的问题,我认为这不是您想要的,但我会将它们留在这里以防万一。谢谢您的时间。。。这一个答案比另一个答案更难实现,但这可能是因为我缺乏经验。再次感谢!工作得很有魅力!谢谢你的时间;我以前试过抵消,但显然不正确:)不客气!记得投票支持工作答案。我已经试过了,但不允许,因为我的代表还没有到15岁。一旦我有能力,我一定会回来的。谢谢你的回答。我已经在使用ContentInsets了,但问题是即使在放大时它们也在那里。使用您的方法,放大时不再插入内容。我将它移植到C#进行MonoTouch,效果非常好。您还向我介绍了scrollViewDidZoom方法,它为在缩放时更改内容提供了各种可能性。这实际上解决了我的问题!谢谢
- (void)layoutScrollView
{
    if (!self.image) return;

    CGFloat heightScale = self.frame.size.height / self.image.size.height;
    CGFloat widthScale = self.frame.size.width / self.image.size.width;
    CGFloat scale = MIN(widthScale, heightScale);

    self.scrollView.minimumZoomScale = scale;
    self.scrollView.maximumZoomScale = MAX(1.0f ,scale * 2.0f);
    [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:NO];

    [self centerImage];
}
- (void)centreImageView
{
    CGRect bounds = self.bounds;
    CGSize contentSize = self.contentSize;
    CGFloat offsetX = MAX(0, (bounds.size.width - contentSize.width) * 0.5f);
    CGFloat offsetY = MAX(0, (bounds.size.height - contentSize.height) * 0.5f);

    _myContentView.center = CGPointMake(contentSize.width * 0.5 + offsetX, contentSize.height * 0.5 + offsetY);
}