translationInView
이 연속 제스처의 시작부터 있기 때문에 번역이 누적되는 문제가 있지만 원래 프레임이 아닌 현재 프레임에 번역을 추가하는 것은 문제입니다. 이것은 동작 상태를 확인하여 해결하고이 제스처의 시작 인 경우 다음 원래 프레임을 저장 한 다음 제스처 진행 미래 번역의 기초로 그것을 사용한다. 당신이 그것을 사용하는 것 같지 않았기 때문에
-(void) panningMyView:(UIPanGestureRecognizer*) panGesture
{
static CGRect originalFrame; // or you could make this a non-static class ivar
if (panGesture.state == UIGestureRecognizerStateBegan)
{
originalFrame = self.myPanningView.frame;
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [panGesture translationInView:self.view];
if (translation.x > 0) {
CGRect newFrame = originalFrame;
newFrame.size.width += translation.x;
newFrame.size.height += translation.y;
self.myPanningView.frame = newFrame;
}
}
}
참고, 나는 oldPoint
제거되었다. 또한 newPoint
의 이름을 translation
으로 바꿨습니다. 화면상의 점이 아니기 때문에 손가락이 화면에서 얼마나 움직 였는지 (또는 번역 된 정도)를 측정했기 때문입니다. oldRect
의 이름을 으로 변경했는데, 정확하게 그것이 무엇인지 파악하기 때문입니다.
본질적으로, 나는 일상의 논리를 보존하려고 노력했지만 논리와 변수 이름을 간단하게 명확히했습니다. 추가로 else if
원인을 찾고, 종료 또는 취소 제스처를 확인하고, 애니메이션을 사용하여 제스처를 완료하거나 취소 할 수 있다고 생각 했겠지만, 원래 질문에서이 참조를 참조하지 않았기 때문에 태클하지 않았습니다. .
관계없이, 우리가 여기서하고있는 일에 대한 아이디어를 얻길 바랍니다. 우리는 원본 프레임을 저장하고 현재 프레임에 적용하는 대신 번역을 적용합니다.
는 업데이트 : 후속 질문에
, 당신은 당신이 애니메이션을 명확히하는 방법을 물었다. 당신은 다음과 같이 할 수 있습니다 :
-(void) panningMyView:(UIPanGestureRecognizer*) panGesture
{
static CGRect originalFrame; // or you could make this a non-static class ivar
CGPoint translation = [panGesture translationInView:self.view];
if (panGesture.state == UIGestureRecognizerStateBegan)
{
originalFrame = self.myPanningView.frame;
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
if (translation.x > 0) {
CGRect newFrame = originalFrame;
newFrame.size.width += translation.x;
newFrame.size.height += translation.y;
self.myPanningView.frame = newFrame;
}
}
else if (panGesture.state == UIGestureRecognizerStateEnded ||
panGesture.state == UIGestureRecognizerStateCancelled ||
panGesture.state == UIGestureRecognizerStateFailed)
{
CGRect finalFrame = originalFrame;
// if we've gone more than half way, move it all the way,
// otherwise return it to the original frame
if (translation.x > (self.view.frame.size.width/2.0))
{
finalFrame.size.width += self.view.frame.size.width;
finalFrame.size.height += self.view.frame.size.height;
}
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.myPanningView.frame = finalFrame;
}
completion:nil];
}
}
출처
2012-12-25 17:54:29
Rob
좋은 설명, 나는 용어를 망 쳤어. 그러나, 당신은 그것들을 아주 분명하게 만들었습니다. 마지막 프레임 치수가 stateChanged 여기에 설정되어있는 애니메이션의 어떤 종류 표시 할 수 있습니다 끝난 상태에서, 그래서 – andyPaul
모든 것은, stateChanged 여기에 처리됩니다. – andyPaul
@andyPaul 나는 제스처 종료 애니메이션을 추가 또 다른 예를 들어 내 대답을 업데이 트했습니다. 제가 말하고있는 논리는 "어이, 반쯤 이상이라면 finalFrame을 최종 프레임 크기로 설정 한 다음 애니메이션을 적용합니다. 절반 정도만 건너면 반환 값을 애니메이션으로 만듭니다. 이것을 원래 크기로 되돌려 놓습니다. " 분명히, 나는 적절한 'finalFrame.size' 값을 알지 못한다. 그래서 나는 추측을했다. 그러나 당신은 그것을 변경해야만 할 것이다. ('if' 문은'translation'이 얼마나 " 절반 이상의 방법 "),하지만 당신은 아이디어를 얻을. – Rob