2013-10-25 49 views
0

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

    } 

enter image description here

+0

只需添加button1.frame = CGRectMake(x值,y值,60.0,40.0); –

回答

1

你只需要更新按鈕,根據xValueyValue價值,你得到了它Xÿ位置。按鈕的

設置幀

button1.frame = CGRectMake(xValue, yValue, 60.0, 40.0); 
+0

對不起,我無法得到你..你能否詳細說明你的問題? – iPatel

+0

你可以檢查我編輯的代碼 – user2807197

+0

在webView上添加你的按鈕 – iPatel