未调用UISwipeGestureRecognizer选择器。知道为什么吗?

时间:2011-11-04 15:28:28

标签: iphone ios ios4 uiimageview uiswipegesturerecognizer

我正在使用下面的代码将右/左滑动事件集成到UIImageView对象(称为 photo_view ),但在iphone模拟器和设备中进行测试后它无效。下面的方法 handleLeftSwipe handleRightSwipe 甚至都没有被调用,因为日志记录没有在调试器日志中打印任何内容。这是代码:

- (void)viewDidLoad
{
     [super viewDidLoad];

     UISwipeGestureRecognizer *leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(handleLeftSwipe:)];
     leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
     leftSwipeRecognizer.numberOfTouchesRequired = 1;
     [photo_view addGestureRecognizer:leftSwipeRecognizer];
     leftSwipeRecognizer.delegate = self;
     [leftSwipeRecognizer release];

     UISwipeGestureRecognizer *rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
     rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
     rightSwipeRecognizer.numberOfTouchesRequired = 1;
     [photo_view addGestureRecognizer:rightSwipeRecognizer];
     rightSwipeRecognizer.delegate = self;
     [rightSwipeRecognizer release];

}


- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)recognizer
{
     NSLog(@"handleLeftSwipe called");
}

- (void)handleRightSwipe:(UISwipeGestureRecognizer *)recognizer
{
     NSLog(@"handleRightSwipe called");
}

任何想法是什么原因?

提前帮忙,

的Stephane

2 个答案:

答案 0 :(得分:7)

UIImageView个实例默认userInteractionEnabledNO。尝试将其设置为YES

答案 1 :(得分:2)

确保您还在.h档案中加入了<UIGestureRecognizerDelegate>