Ios UIPAngestureRecognitor停止调用选择器

Ios UIPAngestureRecognitor停止调用选择器,ios,objective-c,uigesturerecognizer,uipangesturerecognizer,Ios,Objective C,Uigesturerecognizer,Uipangesturerecognizer,我在使用UIPanGestureRecognizer时遇到问题,因为当我的手指移动时,它只会调用选择器,我希望它继续调用选择器,即使我的手指站在同一个位置 屏幕上有4个对象,一个在顶部,一个在右侧,一个在左侧,一个在底部。我在屏幕的中心有一个物体(这是我用panGesture移动的物体)。当这个物体接触到其他物体时,我想让它给我一个圆木,当它接触时它会工作,但是如果我把手指放在同一个地方,它就会停下来给我圆木,如果我移动一点,它就会再次给我圆木 不管怎样,即使我的手指在同一个地方,我也能一直呼叫

我在使用UIPanGestureRecognizer时遇到问题,因为当我的手指移动时,它只会调用选择器,我希望它继续调用选择器,即使我的手指站在同一个位置

屏幕上有4个对象,一个在顶部,一个在右侧,一个在左侧,一个在底部。我在屏幕的中心有一个物体(这是我用panGesture移动的物体)。当这个物体接触到其他物体时,我想让它给我一个圆木,当它接触时它会工作,但是如果我把手指放在同一个地方,它就会停下来给我圆木,如果我移动一点,它就会再次给我圆木

不管怎样,即使我的手指在同一个地方,我也能一直呼叫选择器吗

下面是一个代码示例:

- (void)moveObject:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.limiteDirecional];
    [sender setTranslation:CGPointMake(0, 0) inView:self.limiteDirecional];

    CGPoint center = sender.view.center;
    center.y += translation.y;
    int yMin = 0;
    int yMax = self.limiteDirecional.frame.size.height;

    if (center.y < yMin || center.y > yMax )
        return;

    sender.view.center = center;

    center.x += translation.x;
    int xMin = self.limiteDirecional.frame.size.width;
    int xMax = 0;

    if (center.x > xMin || center.x < xMax)
        return;

    sender.view.center = center;

    if (CGRectIntersectsRect(sender.view.frame,self.Top.frame)) {
         NSLog(@"TOP");        
    }

    if (CGRectIntersectsRect(sender.view.frame,self.Botton.frame)) {
        NSLog(@"BOTTON");
    }

    if (CGRectIntersectsRect(sender.view.frame,self.Right.frame)) {
        NSLog(@"RIGHT");
    }

    if (CGRectIntersectsRect(sender.view.frame,self.Left.frame)) {
        NSLog(@" LEFT");
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        sender.view.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
    }
}
-(void)moveObject:(UIPangestureRecognitor*)发送方
{
CGPoint translation=[sender TranslationView:self.limitedReceional];
[sender setTranslation:CGPointMake(0,0)inView:self.limitedReceive];
CGPoint center=sender.view.center;
center.y+=translation.y;
int-yMin=0;
int yMax=自限精度框架尺寸高度;
if(center.yyMax)
返回;
sender.view.center=中心;
center.x+=translation.x;
int xMin=self.limitedrecial.frame.size.width;
int xMax=0;
if(center.x>xMin | | center.x<>代码> 我不完全遵循你的例程的逻辑,所以我会提供一个通用模板,当你想要手势中间的连续事件,用户是否在移动手指时,解决方案会是什么样子。希望您可以根据自己的目的调整此技术

这使用了
CADisplayLink
,这被认为是一种比使用
NSTimer
的旧技术更好的动画技术。但是,要使用
CADisplayLink
,您需要将QuartzCore.framework添加到您的项目中,如果您还没有这样做的话。还要注意的是,在我的手势识别器中,我检查手势的状态,知道我们是在一个中间开始手势,还是结束一个手势:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CGPoint translationInView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(handleGesture:)];
    // I'm adding to the main view, but add it to whatever you want
    [self.view addGestureRecognizer:gesture]; 
}

- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
    NSLog(@"%s translationInView = %@", __FUNCTION__, NSStringFromCGPoint(self.translationInView));

    // Do here whatever you need to happen continuously while the user is in the
    // middle of the gesture, whether their finger is moving or not.
}

- (void)handleGesture:(UIPanGestureRecognizer *)gesture
{
    self.translationInView = [gesture translationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        [self startDisplayLink];

        // Do whatever other initialization stuff as the user starts the gesture
        // (e.g. you might alter the appearance of the joystick to provide some
        // visual feedback that they're controlling the joystick).
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // Do here only that stuff that actually changes as the user is moving their
        // finger in the middle of the gesture, but which you don't need to have
        // repeatedly done while the user's finger is not moving (e.g. maybe the
        // visual movement of the "joystick" control on the screen).
    }
    else if (gesture.state == UIGestureRecognizerStateEnded ||
             gesture.state == UIGestureRecognizerStateCancelled ||
             gesture.state == UIGestureRecognizerStateFailed)
    {
        [self stopDisplayLink];

        // Do whatever other cleanup you want to do when the user stops the gesture
        // (e.g. maybe animating the moving of the joystick back to the center).
    }
}
@end
#导入“ViewController.h”
#进口
@界面视图控制器()
@属性(非原子,强)CADisplayLink*displayLink;
@属性(非原子)转换点视图;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
UIPanGestureRecognizer*手势=[[UIPanGestureRecognizer alloc]initWithTarget:self
操作:@selector(HandleTesture:)];
//我正在添加到主视图中,但是可以添加到任何您想要的内容中
[self.view addgestureRecognitor:signature];
}
-(无效)startDisplayLink
{
self.displayLink=[CADisplayLink displayLinkWithTarget:self-selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
}
-(无效)停止显示链接
{
[self.displayLink失效];
self.displayLink=nil;
}
-(无效)handleDisplayLink:(CADisplayLink*)displayLink
{
NSLog(@“%s translationView=%@”,“函数”,NSStringFromCGPoint(self.translationView));
//在这里,当用户处于
//手势的中间,不管他们的手指是否在移动。
}
-(无效)手势:(UIPangestureRecognitor*)手势
{
self.translationView=[手势翻译视图:手势.view];
if(signature.state==UIGestureRecognitizerStateStart)
{
[自启动显示链接];
//在用户启动手势时,执行任何其他初始化操作
//(例如,您可能会改变操纵杆的外观,以提供一些
//他们正在控制操纵杆的视觉反馈)。
}
else if(signature.state==UIgestureRecognitizerStateChanged)
{
//在这里,只做那些随着用户移动鼠标而改变的事情
/手指在手势的中间,但是你不需要
//在用户手指不移动时重复进行(例如,可能是
//“操纵杆”控制装置在屏幕上的视觉移动)。
}
else if(signature.state==UIgestureRecognitizerStateEnded||
手势.state==UIGestureRecognitzerStateConcelled||
手势.state==UIGestureRecognitzerStateFailed)
{
[自动停止显示链接];
//当用户停止手势时,执行任何其他清理操作
//(例如,可能设置操纵杆移回中心的动画)。
}
}
@结束

如果同时使用
NSTimer
,也可以获得类似的效果。任何对你更有效的方法。

手势识别器本身不会这样做。您可以使用计时器或
CADisplayLink
或其他工具,但要提出解决方案,了解您试图解决的问题很有用。当触摸没有变化时,通过获取触摸变化通知,您试图解决什么问题?对!这就像操纵杆一样,如果你拖动与“传感器”接触的“主对象”,它应该会移动我的精灵,但如果我按住“主对象”,即使它接触“传感器”,它也会停止移动我的精灵!我必须轻推以保持ir移动。。。