0

我有 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];

    }

在此处输入图像描述

4

1 回答 1

1

你只需要根据你得到的 值来更新按钮XY的位置。xValueyValue

设置按钮框架

button1.frame = CGRectMake(xValue, yValue, 60.0, 40.0);
于 2013-10-25T06:03:06.300 回答