UIScrollView contentOffset固定在{0,0}

时间:2014-05-14 16:13:42

标签: ios objective-c uiscrollview uiscrollviewdelegate

我正在使用project,并且在尝试修复其他内容时设法破坏了UIScrollView的功能。我恢复了代码,但滚动视图仍然被打破。要找到错误,请运行项目,然后在表格视图中选择“基本查看器”。现在尝试缩放和平移可见页面......它会缩放,但不会滚动。

所以问题是滚动视图的内容偏移固定为{0,0}。 (即使在缩放之后。)并且导致它被固定的事情不是调用setContentOffset:。我已经将UIScrollView子类化为调试正在进行的操作,并且已经发现UIPanGestureRecognizer没有问题,即将正确的值发送到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:来打印调试信息,一切看起来都很正常。< / p>

我很难过,多谢那些可以帮我解决这个问题的人。

0 个答案:

没有答案