gpt4 book ai didi

ios - UIPanGestureRecognizer 停止调用选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:32 30 4
gpt4 key购买 nike

我在使用 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);
}
}

最佳答案

我并没有完全遵循您的例程逻辑,所以我将提供一个通用模板,说明当您希望在手势中间发生连续事件时,无论用户是在移动手指还是不是。希望您可以根据自己的目的调整此技术。

这使用了 CADisplayLink,它被认为是比使用 NSTimer 的旧技术更好的动画技术。不过,要使用 CADisplayLink,您需要 add the needed framework , 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

如果您也使用 NSTimer 也可以达到类似的效果。任何更适合您的东西。

关于ios - UIPanGestureRecognizer 停止调用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860816/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com