简体   繁体   English

UISwitch打开/关闭UILongPressGestureRecognizer

[英]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. 我在应用程序中声明了以下方法,并且我想实现一个开关以在mapView中打开和关闭UILongPressGestureRecognizer

- (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. 我知道我可以添加或删除手势识别器或实现.enabled = NO ,但是我不知道如何在switch方法中实现它。

Something like this can help assuming you have a longPressGestureRecognizer property: 假设您具有longPressGestureRecognizer属性,这样的事情可能会有所帮助:

@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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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