다음은 계획입니다. UIView를 하위 클래스로 분류합니다. 몇 가지 속성을 추가하고 몇 가지 메서드를 재정의 할 것입니다. 아이디어는 당신이 터치로 터치를 지시하도록합니다 .Began과 touchesMoved. 손가락이 손을 떼면 Ended는보기를 적절한 휴식 위치로 움직입니다. 내가 제공하는 코드 샘플에서는 완전히 확장되거나 완전히 철회되지만 원하는 경우 언제든지 철회 할 수 있습니다.
약간의 관성이 적용되는 것처럼 들리는군요. 조금 더 복잡합니다. 추가 애니메이션을 직접 추가해야하기 때문입니다. 그러나 올바른 애니메이션을 연결하기 만하면됩니다 (예 : 타이밍 기능을 사용하여 종료 지점을지나 애니메이션으로 다시 가져와야 함). 그것은 사용자가 이동을 마친 후에 시작됩니다. 당신이 공상을하고 싶다면, 당신은 스 와이프의 속도를 추적하고이 속도/운동량을 기반으로 애니메이션을 발사합니다.
다음은 서랍에 애니메이션 효과를 적용하는 데 도움이되는 코드 샘플입니다. 그것은 당신이 묘사 한 공상을 가지고 있지는 않지만, 그러한 행동을 어떻게 그리고 어디에서 도입 할 것인지를 결정할 근거를 제공해야합니다.
"현재 위치", 원점, 천장 (뷰가 위로 드래그를 중단해야하는 지점 또는 "밖으로"있는 지점) 및 바닥 최대 값은 Y 오프셋, 또는은) 아래에 가지 않을 좌표입니다.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint startPt = [touch locationInView:self];
self.currentPoint = startPt;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint activePoint = [[touches anyObject] locationInView:self];
//new position
CGPoint newPoint = CGPointMake(self.center.x,
self.center.y + (activePoint.y - currentPoint.y));
if (newPoint.y > self.draggingFloor) {
//too low
newPoint.y = self.draggingFloor;
} else if (newPoint.y < self.draggingCeiling) {
//too high
newPoint.y = self.draggingCeiling;
}
self.center = newPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//The user isn't touching. Now we can adjust to drawer to a "better" position.
//The 150f here is going to depend on how far the view should be dragged before it opens.
CGFloat median = pullDrawerYOrigin + 150.0f;
CGFloat position = self.center.y;
if (position <= median) {
//We're closer to open than closed. So open all the way.
[self animateDrawerToPositionYesForOpen:YES];
}
else if (position >= median) {
//close
[self animateDrawerToPositionYesForOpen:NO];
}
}
- (CGFloat) draggingFloor {
//Don't drag any lower than this.
if (!__draggingFloor) {
__draggingFloor = pullDrawerYOrigin + (self.bounds.size.height *.5);
}
return __draggingFloor;
}
- (CGFloat) draggingCeiling {
//Don't drag any higher than this.
if (!__draggingCeiling) {
__draggingCeiling = self.superview.bounds.size.height + (self.bounds.size.height * .05);
}
return __draggingCeiling;
}
- (void) animateDrawerToPositionYesForOpen:(BOOL)position {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.2];
CGPoint myCenter = self.center;
if (position == YES) {
myCenter.y = self.draggingCeiling;
}
else if (position == NO) {
myCenter.y = self.draggingFloor;
}
self.center = myCenter;
[UIView commitAnimations];
}
안녕하세요! 이것은 내가 찾고 정확히입니다. 는이 동작을 구현하는 방법을 발견 했습니까? –
을 @DmitryZhukov 전혀 미안 . 아무것도 못 찾았 어. 뭔가 찾으면 알려줘! – choise