UISwitch打开/关闭UILongPressGestureRecognizer

时间:2012-10-12 19:42:14

标签: iphone ios6 uigesturerecognizer uiswitch

我在我的应用中声明了以下方法,我想在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,但我不知道如何在切换方法中实现它。

1 个答案:

答案 0 :(得分: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];
    }
}