iOS:“recognizer.view.center = CGPointMake(..."; [英] iOS: Wrong effect with "recognizer.view.center = CGPointMake(..."
问题描述
我正在使用 locationInView 来设置当用户释放它时我的 boton 的位置.释放后,我给出了之前存储的位置,但实际上,我的按钮没有回到正确的位置.
I am using the locationInView to set the position of my bouton when the user release it. After release, I give the location stored before but in fact, my button is not going back to the correct position.
这是我的代码:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
static CGPoint startLocation;
if (recognizer.state == UIGestureRecognizerStateBegan) {
startLocation = [recognizer locationInView:self.view];
NSLog(@"Began: %d" @"-" @"%d", (int)startLocation.x , (int)startLocation.y);
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"Ended Bef: %d" @"-" @"%d", (int)startLocation.x, (int)startLocation.y);
recognizer.view.center = CGPointMake(startLocation.x, startLocation.y);
startLocation = [recognizer locationInView:self.view];
NSLog(@"Ended Aft: %d" @"-" @"%d", (int)startLocation.x, (int)startLocation.y);
}
}
其实就是指令:
recognizer.view.center = CGPointMake(startLocation.x, startLocation.y);
产生错误的效果.有人知道为什么吗?
give a wrong effect. Someone know why?
推荐答案
一些想法:
我建议您在对
center
进行任何更改之前确保捕获startLocation
.
I'd suggest making sure you capture
startLocation
before you do any changing of thecenter
.
您将用户触摸的位置用于 startLocation
.你真的应该用 recognizer.view
的 center
来初始化它.用户极不可能在按钮的中心开始他们的手势.因此,您不太可能回到原来的位置.
You're using the location of the user's touch for startLocation
. You really should initialize this with the center
of the recognizer.view
. It's exceedingly unlikely that the user started their gesture precisely in the center of the button. And as a result, you're unlikely to return back at the original location.
有点不相关,但是:
在
UIGestureRecognizerStateEnded
recognizer.view
的center
重置时,不需要使用CGMakePoint
/code> 子句.如果您真的需要,您可以使用CGPointMake
,但这是不必要的.如果需要,您可以只使用startLocation
.
You don't need to use
CGMakePoint
when resetting thecenter
ofrecognizer.view
in theUIGestureRecognizerStateEnded
clause. You can useCGPointMake
if you really want, but it's unnecessary. You can just usestartLocation
, if you want.
您可能想要动画返回到那个 startLocation
的视图.让它立即去那里很刺耳.
You might want to animate the returning of the view back to that startLocation
. It's jarring to have it immediately go there.
顺便说一句,如果您已保存startLocation
,则无需不断重置translation
.只需使用 startLocation
加上 translation
.对我来说似乎更清楚,但显然这是主观的.
As an aside, if you've saved startLocation
, you don't need to continually reset the translation
. Just use startLocation
plus translation
. Seems more clear to me, but clearly that's subjective.
我个人认为 NSStringFromCGPoint
在记录 CGPoint
结构时非常有用.
I personally think NSStringFromCGPoint
is very useful when logging CGPoint
structures.
所以,我建议:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
static CGPoint startLocation;
if (recognizer.state == UIGestureRecognizerStateBegan)
{
startLocation = recognizer.view.center;
NSLog(@"Began: %@", NSStringFromCGPoint(startLocation));
}
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(startLocation.x + translation.x,
startLocation.y + translation.y);
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"Ended Bef: %@", NSStringFromCGPoint(startLocation));
[UIView animateWithDuration:0.25
animations:^{
recognizer.view.center = startLocation;
}];
CGPoint finalLocation = [recognizer locationInView:self.view];
NSLog(@"Ended Aft: %@", NSStringFromCGPoint(finalLocation));
}
}
这篇关于iOS:“recognizer.view.center = CGPointMake(...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!