gpt4 book ai didi

iOS:获取当前对象的真实位置

转载 作者:行者123 更新时间:2023-11-29 03:59:43 26 4
gpt4 key购买 nike

使用 Pan GestureComponent,我试图获取当前拖动对象的第一个位置。为此,我做了:

首先,我存储状态开始时按钮的位置。

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{

CGPoint startlocation;

// Get Position X and Y
if (recognizer.state == UIGestureRecognizerStateBegan) {
startlocation = [recognizer locationInView:self.view];
NSLog(@"%f" @"-" @"%f", startlocation.x , startlocation.y);
}

当用户释放按钮(状态结束)时,我想将按钮设置回最初的位置。

// Quand on lache le composant : Action de fin
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"%f" @"-" @"%f", startlocation.x , startlocation.y);
recognizer.view.center = startLocation;
}

NSLog:

状态开始:

76.000000-158.000000

状态结束:

0.000000--1.998666

我有一个问题。我的按钮位于屏幕之外。不知道为什么startLocation的X和Y要修改?

最佳答案

你在谈论两件不同的事情:

  1. 触发平移手势的触摸位置识别器locationInView
  2. View 的位置(您没有说明您使用的是框架原点还是中心)

尚不清楚您要做什么,但您需要决定,是否通过平移手势翻译来移动目标 View :

if (recognizer.state == UIGestureRecognizerStateBegan) {
startLocation = targetView.center;
}

CGPoint translation = [recognizer translationInView:self.view];

CGPoint newCenter = startLocation;
newCenter.x += translation.x;
newCenter.y += translation.y;

targetView.center = newCenter;

或者您尝试将目标 View 捕捉到平移手势位置

targetView.center = [recognizer locationInView:self.view];

这一切都假设目标 View 是 self.view 的直接 subview ...

关于iOS:获取当前对象的真实位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com