如何添加UIGestureRecognizer来识别UIImageView顶部的左右滑动?我只需要在离开滑动时调用一个动作,而在滑动时则需要另一个动作。
答案 0 :(得分:6)
创建2个滑动手势并将其添加到图像视图中。
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myRightAction)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[myImageView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer * recognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myLeftAction)];
[recognizer2 setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[myImageView addGestureRecognizer:recognizer2];
[recognizer2 release];
答案 1 :(得分:2)
你有没有设置
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
在附加UIGestureRecognizer
之前为true?
如果没有尝试
如果这不起作用,您可以添加临时视图并将手势识别器添加到临时视图,然后将imageview添加到临时视图