Objective C: UILongPressGestureRecognizer Error

2019-08-27 06:22发布

问题:

I am trying too put a UILongPressGestureRecognizer on my scrollView but it is not working.

It has an error after long touch saying: "Thread 1: Program Received Signal SIGABRT"

here my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    longPressToDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:scrollView action:@selector(forLongPress:)];
    longPressToDrag.minimumPressDuration = 3.0;
    [scrollView addGestureRecognizer:longPressToDrag];

    [self pages];
}

- (void)forLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    NSLog(@"Long Touch");
}

i have the error on my here:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //<--- Thread 1: Program received signal "SIGABRT"
    }
}

回答1:

I think you're using the wrong target. Try changing

longPressToDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:scrollView action:@selector(forLongPress:)];

to

longPressToDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(forLongPress:)];