我正在使用下面的代码将右/左滑动事件集成到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
答案 0 :(得分:7)
UIImageView
个实例默认userInteractionEnabled
到NO
。尝试将其设置为YES
。
答案 1 :(得分:2)
确保您还在.h档案中加入了<UIGestureRecognizerDelegate>
。