保存接触点位置坐标



我有UIWebView用于显示HTML文章页面。我使用UILongPressGesture来获取触摸位置坐标。我需要将此坐标保存到 NSUserDefaults 或任何地方,稍后再获取它以供其他使用。如何保存这些触摸位置坐标

-(void)viewDidLoad{
UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];
    [tap setDelegate:self];
    [wbCont.scrollView addGestureRecognizer:tap];
}

- (void)tapTest:(UILongPressGestureRecognizer *)sender {
    NSLog(@"coordinate is %f %f", [sender locationInView:wbCont].x,  [sender locationInView:wbCont].y);
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

用于NSUserDefaults的商店位置

  - (void)tapTest:(UILongPressGestureRecognizer *)sender 
    {
        NSLog(@"coordinate is %f %f", [sender locationInView:wbCont].x,  [sender locationInView:wbCont].y);
    CGFloat x = [sender locationInView:wbCont].x;
    CGFloat y = [sender locationInView:wbCont].Y;
      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
      [userDefaults setFloat:x forKey:@"xPoint"];
      [userDefaults setFloat:y forKey:@"yPoint"];
      [userDefaults synchronize];
    }

并从NSUserDefaults中检索点

NSUserDefaults *data = [NSUserDefaults standardUserDefaults];  
float xValue = [data floatForKey:@"xPoint"];
float yValue = [data floatForKey:@"yPoint"];

您也可以从用户默认值中获取它以备将来使用。

 -(void)tapTest:(UILongPressGestureRecognizer *)sender
 {
  NSLog(@"coordinate is %f %f", [sender locationInView:wbCont].x,  [sender locationInView:wbCont].y);
 [[NSUserDefaults standardUserDefaults]setValue:[sender locationInView:wbCont].x forKey:@"xCor"];
   [[NSUserDefaults standardUserDefaults]setValue:[sender locationInView:wbCont].y forKey:@"yCor"];
 [[NSUserDefaults standardUserDefaults]synchronize];
  }

最新更新