- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在实现一个 View 时遇到问题。
这是一个在 CATiledLayer 中显示 pdf 页面的 View 。该平铺 View 位于 UISCrollView 内。
我将 View 控制为“ZoomingPDFView”苹果示例。我做了一些修改,以便在未启用滚动时识别滑动手势,并在本网站的各种主题和问题中提供建议。当时,手势被称为一次。但由于我需要解耦 View 并委托(delegate)滑动来缓存页面并实现多功能 View ,我创建了一个 View Controller 来处理滑动手势,页面加载方法提高了 pdf View 的性能。
现在我的 View 在一侧, Controller 在另一侧,滑动手势被检测到两次,我什至无法获得问题的线索。
这是控制台输出
2010-11-19 11:45:08.370 ZoomingPDFViewerForIPad[20327:207] initWithFrame and page
2010-11-19 11:45:08.530 ZoomingPDFViewerForIPad[20327:207] drawPage
2010-11-19 11:45:08.531 ZoomingPDFViewerForIPad[20327:207] scale: 1.000000
2010-11-19 11:45:08.531 ZoomingPDFViewerForIPad[20327:207] pdf scale: 1.290062
2010-11-19 11:45:08.532 ZoomingPDFViewerForIPad[20327:207] pdf initial scale: 1.290062
2010-11-19 11:45:15.488 ZoomingPDFViewerForIPad[20327:207] left
2010-11-19 11:45:15.489 ZoomingPDFViewerForIPad[20327:207] left
2010-11-19 11:45:15.490 ZoomingPDFViewerForIPad[20327:207] initWithFrame and page
2010-11-19 11:45:15.538 ZoomingPDFViewerForIPad[20327:207] drawPage
2010-11-19 11:45:15.538 ZoomingPDFViewerForIPad[20327:207] scale: 1.000000
2010-11-19 11:45:15.539 ZoomingPDFViewerForIPad[20327:207] pdf scale: 1.290062
2010-11-19 11:45:15.539 ZoomingPDFViewerForIPad[20327:207] pdf initial scale: 1.290062
2010-11-19 11:45:15.540 ZoomingPDFViewerForIPad[20327:1a07] initWithFrame and page
2010-11-19 11:45:15.541 ZoomingPDFViewerForIPad[20327:5f07] initWithFrame and page
2010-11-19 11:45:15.593 ZoomingPDFViewerForIPad[20327:1a07] drawPage
2010-11-19 11:45:15.594 ZoomingPDFViewerForIPad[20327:1a07] scale: 1.000000
2010-11-19 11:45:15.594 ZoomingPDFViewerForIPad[20327:1a07] pdf scale: 1.290062
2010-11-19 11:45:15.595 ZoomingPDFViewerForIPad[20327:1a07] pdf initial scale: 1.290062
2010-11-19 11:45:15.695 ZoomingPDFViewerForIPad[20327:5f07] drawPage
2010-11-19 11:45:15.704 ZoomingPDFViewerForIPad[20327:5f07] scale: 1.000000
2010-11-19 11:45:15.707 ZoomingPDFViewerForIPad[20327:5f07] pdf scale: 1.290062
2010-11-19 11:45:15.713 ZoomingPDFViewerForIPad[20327:5f07] pdf initial scale: 1.290062
代码如下:
#import <UIKit/UIKit.h>
@class TiledPDFView;
@protocol PDFScrollViewDelegate;
@interface PDFScrollView : UIScrollView <UIScrollViewDelegate> {
// The TiledPDFView that is currently front most
TiledPDFView *pdfView;
// The old TiledPDFView that we draw on top of when the zooming stops
TiledPDFView *oldPDFView;
// A low res image of the PDF page that is displayed until the TiledPDFView
// renders its content.
UIImageView *backgroundImageView;
id<PDFScrollViewDelegate,NSObject> pdfViewDelegate;
// current pdf zoom scale
CGFloat pdfScale;
CGPDFPageRef page;
CGPDFDocumentRef pdf;
CGFloat initialScale;
TiledPDFView *initialTiledView;
int currentPage;
int pageCount;
UITapGestureRecognizer *doubleTap,*twoFingerDoubleTap;
UISwipeGestureRecognizer *rightSwipe, *leftSwipe;
}
@property (nonatomic,retain) id<PDFScrollViewDelegate> pdfViewDelegate;
-(id)initWithFrame:(CGRect)rect;
-(id)initWithFrame:(CGRect)frame andPDFPage:(CGPDFPageRef)aPage;
-(void)enableGestures;
-(void)drawPage;
@end
@implementation PDFScrollView
@synthesize pdfViewDelegate;
….
-(void)enableGestures{
leftSwipe = [[UISwipeGestureRecognizer alloc ]initWithTarget:self action:@selector(handleRightSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:leftSwipe];
//add right swipe
rightSwipe = [[UISwipeGestureRecognizer alloc ]initWithTarget:self action:@selector(handleLeftSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:rightSwipe];
doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTapsRequired =2;
doubleTap.numberOfTouchesRequired =1;
[self addGestureRecognizer:doubleTap];
twoFingerDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTwoFingerDoubleTap:)];
twoFingerDoubleTap.numberOfTapsRequired =2;
twoFingerDoubleTap.numberOfTouchesRequired =2;
[self addGestureRecognizer:twoFingerDoubleTap];
}
// some more code
@end
#import <UIKit/UIKit.h>
#import "PDFScrollViewDelegate.h"
@class TiledPDFView;
@interface ZoomingPDFViewerForIPadViewController : UIViewController <UIScrollViewDelegate,PDFScrollViewDelegate> {
CGPDFPageRef page;
CGPDFDocumentRef pdf;
NSInteger currentPage;
NSInteger pageCount;
PDFScrollView *myScrollView;
PDFScrollView *previousPage;
PDFScrollView *nextPage;
}
-(id)initWithResourcePath:(NSString*)path ;
-(void)loadNextPage;
-(void)loadPreviousPage;
@end
@implementation ZoomingPDFViewerForIPadViewController
// some more code
#pragma mark -
#pragma mark PDFScrollViewDelegate methods
/*
called when user swipes right on the view
*/
-(void)viewDetectedRightSwipe:(PDFScrollView*)pdfScrollView withGesture:(UISwipeGestureRecognizer*)recognizer {
NSLog(@"right");
if (currentPage>1){
//decreate page counter
currentPage--;
// release old next page
if(nextPage){
[nextPage release];
}
// set the actual page as the next one
nextPage = [myScrollView retain];
// remove the view from the actual view
[myScrollView removeFromSuperview];
// check if the previous page is loaded
if(!previousPage)
[self loadPreviousPage];
// set the previouse page as the actual page
myScrollView = previousPage;
myScrollView.pdfViewDelegate = self;
//[myScrollView drawPage];
// load a new previous page
//[NSThread detachNewThreadSelector:@selector(loadNextPage) toTarget:self withObject:nil];
//[self loadNextPage];
}
}
/*
called when user swipes left on the view
*/
-(void)viewDetectedLeftSwipe:(PDFScrollView*)pdfScrollView withGesture:(UISwipeGestureRecognizer*)recognizer{
NSLog(@"left");
// if the end of the document isn't reached
if (currentPage<pageCount){
//increment current page
currentPage++;
// if a previous page has been loaded release it
if (previousPage) {
[previousPage release];
}
// assing the actual view to as a previous page and retain it before it gets release by superview
previousPage = [myScrollView retain];
// remove the view from the super view
[myScrollView removeFromSuperview];
// if a next page hasn't beeen loaded yet, load it on this thread
if (!nextPage)
[self loadNextPage];
// assign the next page as the current page
myScrollView = nextPage;
// put the current page the delegate
myScrollView.pdfViewDelegate = self;
// add the current page to the super view
[[self view] addSubview:myScrollView];
// load a next page.
[NSThread detachNewThreadSelector:@selector(loadNextPage) toTarget:self withObject:nil];
//[self loadNextPage];
}
}
/*
called when the user taps the screen
*/
-(void)viewDetectedTapping:(PDFScrollView*)pdfScrollView withGesture:(UITapGestureRecognizer*)recognizer {
NSLog(@"tapped");
[myScrollView setZoomScale:1.0f animated:YES];
}
-(void)loadNextPage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGPDFPageRef aPage = CGPDFDocumentGetPage(pdf, currentPage+1);
nextPage = [[PDFScrollView alloc] initWithFrame:myScrollView.frame andPDFPage:aPage ];
[pool release];
}
-(void)loadPreviousPage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGPDFPageRef aPage = CGPDFDocumentGetPage(pdf, currentPage-1);
previousPage = [[PDFScrollView alloc] initWithFrame:myScrollView.frame andPDFPage:aPage];
[pool release];
}
@end
这是手势触发的代码。
-(void)handleRightSwipe:(UIGestureRecognizer*)gesture {
if ([pdfViewDelegate respondsToSelector:@selector(viewDetectedRightSwipe:withGesture:)]) {
UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer*)gesture;
[pdfViewDelegate viewDetectedRightSwipe:self withGesture:swipe];
}
}
-(void)handleLeftSwipe :(UIGestureRecognizer*)gesture{
if ([pdfViewDelegate respondsToSelector:@selector(viewDetectedLeftSwipe:withGesture:)]) {
UISwipeGestureRecognizer *swipe= (UISwipeGestureRecognizer*)gesture;
[pdfViewDelegate viewDetectedLeftSwipe:self withGesture:swipe];
}
}
提前感谢您的宝贵时间
最佳答案
我遇到了完全相同的问题,我的猜测是执行此行时触发了第二个操作:
[myScrollView removeFromSuperview];
出于某种原因,UISwipeGestureRecognizer 在它所附加的 View 被移除时触发。似乎没有其他 UIGestureRecognizer 可以做到这一点。
我的解决方案是在调用 removeFromSuperview 之前禁用所有手势识别器:
for (UIGestureRecognizer *g in myScrollView.gestureRecognizers) {
g.enabled = NO;
g.delegate = nil;
}
[myScrollView removeFromSuperview];
它并不理想,但它确实起到了作用。
关于objective-c - UISwipeGestureRecognizer 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4226239/
我有这个代码: UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
我在调用两次 UISwipeGestureRecognize 时遇到问题,我创建了基于 tabbarcontroller 的应用程序,该应用程序有 4 个选项卡。每个选项卡在该 UIViewContr
是否有一种简单的方法可以延长滑动被识别之前必须移动的距离。看来默认值真的很短。它不仅仅是一个水龙头,但只是勉强。我是否需要创建自定义手势识别器来覆盖此距离? 谢谢。 最佳答案 没有记录的属性可以设置,
我需要根据用户是否滑动屏幕顶部、屏幕中间或屏幕底部来触发不同的事件。我正在尝试找出最好/最简单的方法来做到这一点,因为我很确定没有办法从 UISwipeGestureRecognizer 获取位置。
我正在使用 UISwipeGestureRecogizer刷卡left和 right通过使用 UISwipeGestureRecognizerDirectionRight和 UISwipeGestur
我在 iOS 应用程序中收到无法识别的选择器发送错误。我尝试根据其他类似线程中提到的答案解决该问题,但失败了。请看一下下面的代码并帮助我解决这个问题。 谢谢 class ThirdViewContro
我已经为左/右设置了一个 GestureRecognizer,但我还想为向下(但不是向上)添加一个 GestureRecognizer。当我应用以下内容时,向上滑动在设备屏幕上按预期工作;但是,UIS
我在我的UITabBarController中使用UISwipeGestureRecogniser: class TabBarController: UITabBarController {
我有一个滑动手势识别器,在用户滑动后将其关闭。那时我有一系列动画。第一, View 将沿着用户滑动的方向移动。然后,根据它停止的位置,根据它停止的位置发生另一个动画。然后,我在处理此滑动的函数末尾一直
我有以下 UIGestureRecognizer 可以正确检测向上滑动 - (void) addGestureRecognizer { _swipeRecognizer = [[UISwipe
我在使用 UISwipeGestureRecognizer 时遇到了一些异常行为。可以正确检测到第一次滑动,但无法正确检测到第二次滑动。然后第三次滑动被正确检测到,但第四次没有被正确检测到,等等。我有
我想在滑动 UItextView 时调用一个方法,并确定其标签。我正在使用这段代码: -(IBAction)donecomment:(id)sender{ UISwipeGestureRecogniz
我在 ImageView 中添加了4个UISwipeGestureRecognizer。但是当我向右滑动时,会触发向上UISwipeGestureRecognizer。当我向左滑动时,不会触发UISw
如果我将 UISwipeGestureRecognizer 应用于 View ,则当设备旋转时,滑动方向将按预期工作。但是,如果 UISwipeGestureRecognizer 在 AppDeleg
我如何识别UISwipeGestureRecognizer与 UISwipeGestureRecognizerDirectionUp然后紧接着,无需抬起手指,就识别出 UISwipeGestureRe
在我的 spritekit 游戏中,我试图使 2 个 UISwipeGestureRecognizers 可以同时被识别,但是,我无法这样做。任何帮助将不胜感激,在此先感谢。下面是我的代码... 在
在我的 UiView 中,我有一个名为“container”的 subView,如果用户向上或向下滑动,它应该执行旋转动画。基本上它就像一个轮子,如果用户向下滑动轮子,则向下滑动一定程度,同时向上滑动
我正在尝试创建一个由 UISwipeGestureRecognizer 触发的自定义交互过渡,但状态始终为 UIGestureRecognizerStateEnded,因此我无法使用它来控制动画。 A
在 iOS/Swift 中,UIGestureRecognizer 为 swiping有 location 方法 determine the location where a swipe begin.
我在 SpriteKit、Swift 中有 3 个 SKScenes: 一级 游戏结束 2 级。 在Level 1和Level 2中,我的角色移动是通过以下代码完成的: class Level1: S
我是一名优秀的程序员,十分优秀!