简体   繁体   中英

UISwitch to turn on/off UILongPressGestureRecognizer

I have the following methods declared in my app, and I want to implement a switch to turn on and off UILongPressGestureRecognizer in my mapView.

- (IBAction)addNewPin:(UISwitch *)sender {

if (sender.on) {

NSLog(@"ON!!");
}

else {

NSLog(@"OFF!!");
}

}


- (IBAction)didPressForPin:(UILongPressGestureRecognizer *)sender {

CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

MKPointAnnotation *pa = [[MKPointAnnotation alloc]init];
pa.coordinate = locCoord;
pa.title = @"Test Title!";
[mapView addAnnotation:pa];


NSLog(@"Pressed!!");


}

I know that I can add or remove the gesturerecognizer or implement .enabled = NO , but I do not know how to implement it in the switch method.

Something like this can help assuming you have a longPressGestureRecognizer property:

@synthesize longPressGestureRecognizer = _longPressGestureRecognizer;

- (UILongPressGestureRecognizer *)longPressGestureRecognizer
{
    if (_longPressGestureRecognizer) {
        return _longPressGestureRecognizer;
    }

    _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    return _longPressGestureRecognizer;
}


- (IBAction)toggleAddPinSwitch:(UISwitch *)sender
{
    if ([sender isOn]) {
        [self.mapView addGestureRecognizer:self.longPressGestureRecognizer];
    } else {
        [self.mapView removeGestureRecognizer:self.longPressGestureRecognizer];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM