我有UIWebView,我使用UILongPressGesture獲取觸摸座標。我將這些座標保存在NSUserDefaults
之後再獲取。我需要在觸摸座標位置顯示一個按鈕。可能嗎?如何將UIButton設置爲觸摸座標
長按:
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];
}
只需添加button1.frame = CGRectMake(x值,y值,60.0,40.0); –