5

我有一个应用程序,我需要将 a 初始化contentOffsetUIScrollView某个值。我注意到当我设置 时contentOffset,它会将点向上舍入到最接近的整数。我已经尝试使用两者setContentOffsetsetContentOffset:animated但仍然得到相同的结果。当我尝试手动添加contentOffset到框架原点时,我没有得到我想要的结果。有人知道吗?

UIScrollView *myScrollViewTemp = [[UIScrollView alloc] initWithFrame: CGRectMake(CropThePhotoViewControllerScrollViewBorderWidth, CropThePhotoViewControllerScrollViewBorderWidth, self.myScrollViewBorderView.frame.size.width - (CropThePhotoViewControllerScrollViewBorderWidth * 2), self.myScrollViewBorderView.frame.size.height - (CropThePhotoViewControllerScrollViewBorderWidth * 2))];
[self setMyScrollView: myScrollViewTemp];
[[self myScrollView] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[self myScrollView] setShowsVerticalScrollIndicator: NO];
[[self myScrollView] setShowsHorizontalScrollIndicator: NO];
[[self myScrollView] setBouncesZoom: YES];
[[self myScrollView] setDecelerationRate: UIScrollViewDecelerationRateFast];
[[self myScrollView] setDelegate:self];
[[self myScrollViewBorderView] addSubview: [self myScrollView]];
[myScrollViewTemp release];

UIImageView *myImageViewTemp = [[UIImageView alloc] initWithImage: self.myImage];
[self setMyImageView: myImageViewTemp];
[[self myScrollView] addSubview:[self myImageView]];
[[self myScrollView] setContentSize: [self.myImage size]];
[[self myScrollView] setMinimumZoomScale: self.previousZoomScale];
[[self myScrollView] setMaximumZoomScale: 10];
[[self myScrollView] setZoomScale: self.previousZoomScale animated:NO];
[[self myScrollView] setContentOffset: self.contentOffset animated:NO];
[myImageViewTemp release];

更新:更新代码。

4

4 回答 4

2

看来这不是我的错误,而是Apple有意或无意的预期行为。为了解决这个问题,您需要在 UIViewController 的 viewWillLayoutSubviews 方法中设置 contentOffset。显然,您需要在设置整个视图框架后设置 contentOffset。

于 2012-05-14T20:57:34.023 回答
1

我也在这个问题上苦苦挣扎,但事实证明你可以在滚动视图上设置边界,它做同样的事情并且实际上可以处理 CGFloat 值。

CGPoint newOffset = CGPointMake(3.8, 0);

我们在这里得到了新的偏移量。

[scrollView setContentOffset:newOffset];

在此行之后,当前偏移量为 (4,0)。

scrollView.bounds = CGRectMake(newOffset.x, newOffset.y, scrollView.bounds.size.width, scrollView.bounds.size.height);

这给了你 (3.8, 0)

于 2013-09-10T14:46:00.160 回答
1

斯威夫特 4:

我进行了扩展以提高精度:

extension UIScrollView {
    func setPreciseContentOffset( x:CGFloat? = nil, y:CGFloat? = nil, animated:Bool,completion:@escaping ((Bool) -> Void)) {
        var myX:CGFloat = self.contentOffset.x
        var myY:CGFloat = self.contentOffset.y
        if let x = x {
            myX = x
        }
        if let y = y {
            myY = y
        }
        let point = CGPoint(x:myX,y:myY)
        if animated {
            // calculate duration
            var duration = 0.25 // min duration
            let destCoord =  myX != self.contentOffset.x ? myX : myY
            let diff = fabs ( self.contentOffset.x - destCoord)
            duration += TimeInterval(diff) / 1000
            UIView.animate(withDuration:duration, delay: 0.0, options: [.curveEaseInOut], animations: {
                self.bounds = CGRect(x:point.x,y:point.y,width: self.bounds.size.width,height: self.bounds.size.height)
            }, completion: { (finished: Bool) in
                completion(finished)
            })
        } else {
            self.bounds = CGRect(x:point.x,y:point.y,width: self.bounds.size.width,height: self.bounds.size.height)
            completion(true)
        }
    }
}

用法:

// supposing you have to scroll manually in horizontal

self.myScrollview.setPreciseContentOffset(x:contentX, animated: true, completion: {[weak self] _ in
        guard let `self` = self else { return }
        // HERE the scroll is ended, pay attention the end scrolling delegates aren't be called  because you set manually self.bounds so HERE implement your scrolling end code
    })
于 2018-02-11T15:19:48.873 回答
0

我认为这是因为如果 contentOffset 没有四舍五入,那么 UIScrollView 的任何子视图的内容都会变得模糊,尤其是文本。

于 2012-05-04T15:08:02.767 回答