Ios UIScrollView contentOffset固定在{0,0}

Ios UIScrollView contentOffset固定在{0,0},ios,objective-c,uiscrollview,uiscrollviewdelegate,Ios,Objective C,Uiscrollview,Uiscrollviewdelegate,我有一个正在开发的应用程序,在尝试修复其他东西的同时,设法破坏了UIScrollView的功能。我恢复了代码,但滚动视图仍然损坏。要找到bug,请运行项目,然后在表视图中选择Basic Viewer。现在尝试缩放和平移可见页面。。。它将缩放,但不会滚动 所以问题是滚动视图的内容偏移量被固定到{0,0}。即使在变焦之后。导致它被固定的原因不是调用setContentOffset:。我已经对UIScrollView进行了子类化,以调试正在发生的事情,并发现UIPangestureRecognitor

我有一个正在开发的应用程序,在尝试修复其他东西的同时,设法破坏了UIScrollView的功能。我恢复了代码,但滚动视图仍然损坏。要找到bug,请运行项目,然后在表视图中选择Basic Viewer。现在尝试缩放和平移可见页面。。。它将缩放,但不会滚动

所以问题是滚动视图的内容偏移量被固定到{0,0}。即使在变焦之后。导致它被固定的原因不是调用setContentOffset:。我已经对UIScrollView进行了子类化,以调试正在发生的事情,并发现UIPangestureRecognitor没有问题,它正在向setContentOffset:发送正确的值

滚动视图是UICollectionViewCell的子视图。以下是将其添加到单元格的方式:

- (void)setPageContentView:(PDFKPageContentView *)pageContentView
{
    if (_pageContentView) {
        [self removeConstraints:[self constraints]];
        [_pageContentView removeFromSuperview];
    }

    if (pageContentView == nil) {
        return;
    }

    _pageContentView = pageContentView;
    _pageContentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:_pageContentView];

    NSMutableArray *constraints = [[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[content]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"superview": self.contentView, @"content": _pageContentView}] mutableCopy];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[content]|" options:NSLayoutFormatAlignAllLeft metrics:nil views:@{@"superview": self.contentView, @"content": _pageContentView}]];
    [self.contentView addConstraints:constraints];
}
以及UIScrollView子类:

@implementation PDFKPageContentView
{
    PDFKPageContent *theContentView;
    PDFKPageContentThumb *theThumbView;
    UIView *theContainerView;
}

static void *PDFKPageContentViewContext = &PDFKPageContentViewContext;

static inline CGFloat ZoomScaleThatFits(CGSize target, CGSize source)
{
    CGFloat w_scale = (target.width / source.width);
    CGFloat h_scale = (target.height / source.height);
    return ((w_scale < h_scale) ? w_scale : h_scale);
}

- (void)updateMinimumMaximumZoom
{
    CGRect targetRect = CGRectInset(self.bounds, 0, 0);
    CGFloat zoomScale = ZoomScaleThatFits(targetRect.size, theContentView.bounds.size);
    //Set the minimum and maximum zoom scales
    self.minimumZoomScale = zoomScale;
    self.maximumZoomScale = (zoomScale * ZOOM_MAXIMUM);
}

- (id)initWithFrame:(CGRect)frame fileURL:(NSURL *)fileURL page:(NSUInteger)page password:(NSString *)phrase
{
    if ((self = [super initWithFrame:frame]))
    {
            self.scrollsToTop = NO;
            self.delaysContentTouches = NO;
            self.showsVerticalScrollIndicator = NO;
            self.showsHorizontalScrollIndicator = NO;
            self.contentMode = UIViewContentModeRedraw;
            self.backgroundColor = [UIColor clearColor];
            self.userInteractionEnabled = YES;
            self.autoresizesSubviews = NO;
            self.pagingEnabled = NO;
            self.bouncesZoom = YES;
            self.delegate = self;
            self.scrollEnabled = YES;
            self.clipsToBounds = YES;

            theContentView = [[PDFKPageContent alloc] initWithURL:fileURL page:page password:phrase];

            if (theContentView != nil)
            {
                    theContainerView = [[UIView alloc] initWithFrame:theContentView.bounds];
                    theContainerView.backgroundColor = [UIColor blueColor];
                    theContainerView.autoresizesSubviews = NO;
                    theContainerView.userInteractionEnabled = NO;
                    theContainerView.contentMode = UIViewContentModeRedraw;
                    theContainerView.autoresizingMask = UIViewAutoresizingNone;
                    theContainerView.backgroundColor = [UIColor whiteColor];

                    //Content size same as view size
                    self.contentSize = theContentView.bounds.size;

                    //Add the thumb view to the container view
                    theThumbView = [[PDFKPageContentThumb alloc] initWithFrame:theContentView.bounds]; // Page thumb view
                    [theContainerView addSubview:theThumbView];

                    //Add the content view to the container view
                    [theContainerView addSubview:theContentView];

                    //Add the container view to the scroll view
                    [self addSubview:theContainerView];

                    //Update the minimum and maximum zoom scales
                    [self updateMinimumMaximumZoom];

                    //Set zoom to fit page content
                    self.zoomScale = self.minimumZoomScale;
            }

            [self addObserver:self forKeyPath:@"frame" options:0 context:PDFKPageContentViewContext];

            //Tag the view with the page number
            self.tag = page;
        }

    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"frame" context:PDFKPageContentViewContext];
}

- (void)showPageThumb:(NSURL *)fileURL page:(NSInteger)page password:(NSString *)phrase guid:(NSString *)guid
{
    //Page thumb size
    BOOL large = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
    CGSize size = (large ? CGSizeMake(PAGE_THUMB_LARGE, PAGE_THUMB_LARGE) : CGSizeMake(PAGE_THUMB_SMALL, PAGE_THUMB_SMALL));

    PDFKThumbRequest *request = [PDFKThumbRequest newForView:theThumbView fileURL:fileURL password:phrase guid:guid page:page size:size];

    //Request the page thumb
    UIImage *image = [[PDFKThumbCache sharedCache] thumbRequest:request priority:YES];

    // Show image from cache
    if ([image isKindOfClass:[UIImage class]]) [theThumbView showImage:image];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //Reset the zoom on frame change.
    //Our context
    if (context == PDFKPageContentViewContext) {

        if ((object == self) && [keyPath isEqualToString:@"frame"]) {
            CGFloat oldMinimumZoomScale = self.minimumZoomScale;
            //Update zoom scale limits
            [self updateMinimumMaximumZoom];

            //Old minimum
            if (self.zoomScale == oldMinimumZoomScale) {
                self.zoomScale = self.minimumZoomScale;
            } else {
                // Check against minimum zoom scale
                if (self.zoomScale < self.minimumZoomScale) {
                    self.zoomScale = self.minimumZoomScale;
                } else {
                    // Check against maximum zoom scale
                    if (self.zoomScale > self.maximumZoomScale) {
                        self.zoomScale = self.maximumZoomScale;
                    }
                }
            }
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    //Center the content when zoomed out
    CGSize boundsSize = self.bounds.size;
    CGRect viewFrame = theContainerView.frame;

    if (viewFrame.size.width < boundsSize.width)
        viewFrame.origin.x = (((boundsSize.width - viewFrame.size.width) / 2.0f) + self.contentOffset.x);
    else
        viewFrame.origin.x = 0.0f;

    if (viewFrame.size.height < boundsSize.height)
        viewFrame.origin.y = (((boundsSize.height - viewFrame.size.height) / 2.0f) + self.contentOffset.y);
    else
        viewFrame.origin.y = 0.0f;

    theContainerView.frame = viewFrame;
    theThumbView.frame = theContainerView.bounds;
    theContentView.frame = theContainerView.bounds;
}

- (id)processSingleTap:(UITapGestureRecognizer *)recognizer
{
    return [theContentView processSingleTap:recognizer];
}

- (void)zoomIncrement
{    
    CGFloat zoomScale = self.zoomScale;

    if (zoomScale < self.maximumZoomScale) {
        zoomScale *= ZOOM_FACTOR; // Zoom in

        if (zoomScale > self.maximumZoomScale) {
            zoomScale = self.maximumZoomScale;
        }

        [self setZoomScale:zoomScale animated:YES];
    }
}

- (void)zoomDecrement
{
    CGFloat zoomScale = self.zoomScale;

    if (zoomScale > self.minimumZoomScale) {
        zoomScale /= ZOOM_FACTOR; // Zoom out

        if (zoomScale < self.minimumZoomScale) {
            zoomScale = self.minimumZoomScale;
        }

        [self setZoomScale:zoomScale animated:YES];
    }
}

- (void)zoomReset
{
    if (self.zoomScale > self.minimumZoomScale) {
        self.zoomScale = self.minimumZoomScale;
    }
}

#pragma mark UIScrollViewDelegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return theContainerView;
}

#pragma mark UIResponder instance methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event]; // Message superclass
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event]; // Message superclass
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event]; // Message superclass
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event]; // Message superclass
}

- (void)setBounds:(CGRect)bounds
{
    if (bounds.size.width != 0 && bounds.size.height == 0) {
        [super setBounds: bounds];
    }
}

@end
在该项目中,我已经覆盖了UIScrollView的setContentOffset:、setContentSize:、setFrame:、和setBounds:,以打印调试信息,这一切似乎都很正常

我被难住了,提前感谢任何能帮我解决这个问题的人