我有UIImageView
UILongPressGestureRecognizer
,当检测到长按时发送动作。
我确实在UIImageView
上启用了用户互动。但是,我还有一种手动滚动,可以使用UIImageView
和touchesBegan
方法移动touchesMoved
。
禁用用户交互时,仅滚动有效。启用用户交互后,只有长按手势识别器才能正常工作。
我怎样才能使两者同时发挥作用?
如有必要,可以共享代码,但我不认为这个问题需要它。
答案 0 :(得分:3)
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[gestureRecognizer addTarget:self action:@selector(imgLongPressed:)];
gestureRecognizer.delegate = self;
[imgview addGestureRecognizer: gestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *view_ =(UIImageView*) sender.view;
CGPoint point = [sender locationInView:view_.superview];
if (sender.state == UIGestureRecognizerStateBegan)
{
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
}
}
答案 1 :(得分:0)
手势识别器始终优先并覆盖touchesBegan:
样式事件。您必须使用相同的API /技术实现这两种行为。
答案 2 :(得分:0)
首先,你必须使用手势来移动图像,如下所示。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[vwBigImage addGestureRecognizer:panRecognizer];
将被调用的函数如下所示..
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:yourview];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_firstX = [yourimageview center].x;//declare CGFloat _firstX; in .h file
_firstY = [yourimageview center].y;//declare CGFloat _firstY; in .h file
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
[yourimageview setCenter:translatedPoint];
}
现在你的两个手势都会被召唤。
让我知道它的工作与否!!!
快乐编码!!!!