使用Pan GestureComponent,我试图获取当前对象的第一个位置。为此,我做了:
Firstable,我在状态开始时存储按钮的位置。
- (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);
}
当用户释放boutton(State Ended)时,我想先将按钮设置回来。
// 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?
答案 0 :(得分:1)
你在谈论两件不同的事情:
recognizer locationInView
目前尚不清楚你要做什么,但你需要决定,你是否通过平移手势翻译移动目标视图:
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;
或者您尝试将目标视图捕捉到平移手势位置
targetView.center = [recognizer locationInView:self.view];
这都是假设目标视图是self.view的直接子视图......