Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在目标C中保存UIView的起始位置_Objective C_Ios_Uigesturerecognizer_Uipangesturerecognizer - Fatal编程技术网

Objective c 在目标C中保存UIView的起始位置

Objective c 在目标C中保存UIView的起始位置,objective-c,ios,uigesturerecognizer,uipangesturerecognizer,Objective C,Ios,Uigesturerecognizer,Uipangesturerecognizer,因此,我试图保存UIView的起始位置,以便我创建的块可以/将在未放置在正确位置时捕捉回该位置。我已经创建了一个名为“startPoint”的CGPoint,我想将它的坐标设置为块的起点。现在发生的是,我得到了块,当我释放它时,它会转到屏幕上的0,0坐标(左上角)。不太清楚我错过了什么。任何帮助都将不胜感激。下面是手势识别器的代码 - (void)pan:(UIPanGestureRecognizer *)gestureRecognizer { UIView *view = [gestu

因此,我试图保存UIView的起始位置,以便我创建的块可以/将在未放置在正确位置时捕捉回该位置。我已经创建了一个名为“startPoint”的CGPoint,我想将它的坐标设置为块的起点。现在发生的是,我得到了块,当我释放它时,它会转到屏幕上的0,0坐标(左上角)。不太清楚我错过了什么。任何帮助都将不胜感激。下面是手势识别器的代码

- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer {
    UIView *view = [gestureRecognizer view];
    CGPoint startPoint;


    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        view.backgroundColor = [UIColor redColor];
        startPoint = [gestureRecognizer translationInView:self.view];
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        CGPoint translation = [gestureRecognizer translationInView:view.superview];

        view.center = CGPointMake(view.center.x+translation.x, view.center.y+translation.y);

        [gestureRecognizer setTranslation:CGPointZero inView:view.superview];

    } else if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {

//        double snapX = round(view.center.x / 110) * 110;
//        double snapY = round(view.center.y / 110) * 110;

        double snapX = round(startPoint.x);
        double snapY = round(startPoint.y);

        view.backgroundColor = [UIColor blueColor];

        [UIView animateWithDuration:0.3 animations:^{
            view.center = CGPointMake(snapX, snapY);
        }];
    }
}

尝试更改此行:

startPoint = [gestureRecognizer translationInView:self.view];
为此:

startPoint = [gestureRecognizer translationInView:view.superview];

另外,我不确定我是否真的能从你的描述中理解发生了什么,但听起来你应该在这个方法的范围之外声明
startPoint

你有一个名为
startPoint
的局部变量。这意味着每次调用变量(
pan:
)时,
pan:
)都会重新创建该变量。您需要将
startPoint
设置为一个实例变量或属性,以便创建一次并在调用
pan:

时保持它。您有解决方案吗?