UIScrollView没有分页但有touchesmoved

发布于 2024-09-03 06:50:16 字数 1874 浏览 5 评论 0原文

我有一个 UIScrollView,用来显示 PDF 页面。 我还不想使用分页。我想根据 TouchMoved 事件显示内容(因此在水平滑动之后)。

这(有点)有效,但不是捕获单次滑动并显示 1 页,而是滑动似乎被分成了 100 个部分,并且 1 次滑动就像您正在移动滑块一样!

我不知道我做错了什么。

这是实验性的“显示下一页”代码,只需点击一下:

- (void)nacrtajNovuStranicu:(CGContextRef)myContext
{  
 CGContextTranslateCTM(myContext, 0.0, self.bounds.size.height);
 CGContextScaleCTM(myContext, 1.0, -1.0); 
 CGContextSetRGBFillColor(myContext, 255, 255, 255, 1);
 CGContextFillRect(myContext, CGRectMake(0, 0, 320, 412)); 

 size_t brojStranica = CGPDFDocumentGetNumberOfPages(pdfFajl);

 if(pageNumber < brojStranica){
  pageNumber ++;
 }
 else{
  // kraj PDF fajla, ne listaj dalje.
 } 

 CGPDFPageRef page = CGPDFDocumentGetPage(pdfFajl, pageNumber); 
 CGContextSaveGState(myContext);
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
 CGContextConcatCTM(myContext, pdfTransform);

 CGContextDrawPDFPage(myContext, page);
 CGContextRestoreGState(myContext);
 //osvjezi displej
 [self setNeedsDisplay];
}

这是滑动代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self];    
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {  
  [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; 
    }
}

该代码位于显示 PDF 内容的 UIView 中,也许我应该将其放入 UIScrollView 中,或者“显示下一页”代码是否错误?

I have a UIScrollView that I use to display PDF pages in.
I don't want to use paging (yet). I want to display content based on touchesmoved event (so after horizontal swipe).

This works (sort of), but instead of catching a single swipe and showing 1 page, the swipe seems to gets broken into 100s of pieces and 1 swipe acts as if you're moving a slider!

I have no clue what am I doing wrong.

Here's the experimental "display next page" code which works on single taps:

- (void)nacrtajNovuStranicu:(CGContextRef)myContext
{  
 CGContextTranslateCTM(myContext, 0.0, self.bounds.size.height);
 CGContextScaleCTM(myContext, 1.0, -1.0); 
 CGContextSetRGBFillColor(myContext, 255, 255, 255, 1);
 CGContextFillRect(myContext, CGRectMake(0, 0, 320, 412)); 

 size_t brojStranica = CGPDFDocumentGetNumberOfPages(pdfFajl);

 if(pageNumber < brojStranica){
  pageNumber ++;
 }
 else{
  // kraj PDF fajla, ne listaj dalje.
 } 

 CGPDFPageRef page = CGPDFDocumentGetPage(pdfFajl, pageNumber); 
 CGContextSaveGState(myContext);
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
 CGContextConcatCTM(myContext, pdfTransform);

 CGContextDrawPDFPage(myContext, page);
 CGContextRestoreGState(myContext);
 //osvjezi displej
 [self setNeedsDisplay];
}

Here's the swiping code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self];    
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {  
  [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; 
    }
}

The code sits in UIView which displays the PDF content, perhaps I should place it into UIScrollView or is the "display next page" code wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-09-10 06:50:16

我想我明白了。

我添加:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; 
[self setNeedsDisplay];
}

并删除了 [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()];来自touchesmoved方法。

I think I figured it out.

I added:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; 
[self setNeedsDisplay];
}

and removed [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; from touchesmoved method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文