我有 UIWebView 并且我使用 UILongPressGesture 来获取触摸坐标。我保存了这些坐标,NSUserDefaults
然后再获取。我需要在触摸坐标位置显示一个按钮。是否可以?
长按:
UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];
[tap setDelegate:self];
[wbCont addGestureRecognizer:tap];
保存坐标:
- (void)tapTest:(UILongPressGestureRecognizer *)sender {
NSLog(@"coordinate is %f %f", [sender locationInView:wbCont].x, [sender locationInView:wbCont].y);
float xcor = [sender locationInView:wbCont].x;
float ycor = [sender locationInView:wbCont].y;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:xcor forKey:@"xpoint"];
[userDefaults setFloat:ycor forKey:@"ypoint"];
[userDefaults synchronize];
}
从 NSUserDefaults 获取它:
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
float xValue = [data floatForKey:@"xpoint"];
float yValue = [data floatForKey:@"ypoint"];
如果 x & y 位置,我需要显示一个按钮:
if(xValue && yValue){
NSLog(@"xvalue is %f",xValue);
NSLog(@"yval is %f",yValue);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:@selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:@"click" forState:UIControlStateNormal];
// button1.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[wbcont addSubview:button1];
}