2012-10-12 57 views
0

我有我的应用程序中声明以下方法,并且我想实现一个开关打开和关闭我的mapView中的UILongPressGestureRecognizerUISwitch打开/关闭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!!"); 


} 

我知道我可以添加或删除gesturerecognizer或实施.enabled = NO,但我不知道如何实现它的开关方法。

+1

这个问题是如何与“的Xcode 4.5”? – 2012-10-12 19:44:21

+0

你有99%,我不确定你不知道该怎么办? –

回答

1

像这样的东西可以帮助假设你有一个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]; 
    } 
} 
+0

谢谢,这工作。为了与我的触摸位置方法一起工作,我只是添加了“_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture :)]; return _longPressGestureRecognizer; ”,以便与ViewDidLoad配合使用。 – EmilDo

+0

很高兴听到它。快乐的编码。 – voromax