gpt4 book ai didi

iphone - 在 iOS 中按点偏移从中心移动图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:26 26 4
gpt4 key购买 nike

我使用 UIPanGesture 在 iPhone 屏幕上移动图像。有些图像很小,当您用手指移动它们时,会遮挡住您对图像本身的看法。我想在图像移动时设置图像的中心,使图像中心实际上在触摸位置前面 10 个点,而不是将其设置为触摸位置。

我测试了下面的内容,但很快意识到它反复从 Y 中减去 10,使图像离触摸位置越来越远,最终离开屏幕,而不是保持恒定的 10 点偏移。

我应该怎么做?

- (void) TestGestureMethod:(UIPanGestureRecognizer *) panGesture {
CGPoint translation = [panGesture translationInView:self.view];
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:
[self.view bringSubviewToFront:testObject];
break;
case UIGestureRecognizerStateChanged:
testObject.center = CGPointMake(testObject.center.x + translation.x,
testObject.center.y + translation.y);
testObject.center = CGPointMake(testObject.center.x, testObject.center.y - 10);
break;
case UIGestureRecognizerStateEnded:
break;
}
[panGesture setTranslation:CGPointZero inView:self.view];
}

最佳答案

由于您不是翻译图像而是手动设置其中心,您是否考虑过使用 UIGestureRecognizer locationInView: 而不是 translationInView:?

你可以做这样的事情......

- (void)TestGestureMethod:(UIPanGestureRecognizer *)panGesture
{
CGPoint location = [panGesture locationInView:self.view];

switch (panGesture.state) {
...
case UIGestureRecognizerStateChanged:
testObject.center = CGPointMake(location.x, location.y - 10);
break;
...
}
}

这应该会导致图像中心始终位于触摸点下方 10 点处。

关于iphone - 在 iOS 中按点偏移从中心移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411890/

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