UIScrollView没有分页但有touchesmoved
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白了。
我添加:
并删除了 [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()];来自touchesmoved方法。
I think I figured it out.
I added:
and removed [self nacrtajNovuStranicu:(CGContextRef)UIGraphicsGetCurrentContext()]; from touchesmoved method.