簡體   English   中英

UISwitch打開/關閉UILongPressGestureRecognizer

[英]UISwitch to turn on/off UILongPressGestureRecognizer

我在應用程序中聲明了以下方法,並且我想實現一個開關以在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!!");


}

我知道我可以添加或刪除手勢識別器或實現.enabled = NO ,但是我不知道如何在switch方法中實現它。

假設您具有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