大家好,我仍在与UIViewPropertyAnimation
一起制作UIViewController
的动画,以获取可拖动面板(如应用程序地图iOS)的效果。
一切正常,但我需要限制UIGestureRecognizerStateEnded
中 PanGesture 的操作。
简而言之,我希望面板仅在用户滚动到特定高度时才展开,否则,我希望面板返回到初始位置..
这是使您更好地理解的示例:
case UIGestureRecognizerStateEnded:
if (translation.y > 20) // open panel
else // return (With Animation) in default position
到目前为止,我已经以这种方式实现了UIViewPropertyAnimation
函数:
//MARK: - UIViewPropertyAnimation init
-(void)animateTransitionIfNeeded:(InteractiveViewControllerState)state duration:(NSTimeInterval)duration {
BOOL runningAnimationIsEmpty = _runningAnimations.count == 0;
CGSize frameSize = self.view.frame.size;
if (!runningAnimationIsEmpty) return;
else {
UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc] initWithDuration:duration dampingRatio:interactiveAnimationDamping animations:^{
switch (state) {
case InteractiveViewControllerStateExpansed: {
self.interactiveViewController.view.frame = CGRectMake(0, (frameSize.height - self.interactiveControllerHeight) + interactiveViewDampingOffset, frameSize.width, self.interactiveControllerHeight);
}
break;
case InteractiveViewControllerStateCollapsed: {
self.interactiveViewController.view.frame = CGRectMake(0, frameSize.height - interactiveViewVisibleArea, frameSize.width, self.interactiveControllerHeight);
}
break;
default:
break;
}
}];
[animator addCompletion:^(UIViewAnimatingPosition finalPosition) {
self.interactiveStateIsExapanded = !self.interactiveStateIsExapanded;
[self.runningAnimations removeAllObjects];
}];
UIViewPropertyAnimator *cornerRadiusAnimation = [[UIViewPropertyAnimator alloc] initWithDuration:duration curve:UIViewAnimationCurveEaseOut animations:^{
switch (state) {
case InteractiveViewControllerStateExpansed:
self.interactiveViewController.view.layer.cornerRadius = 15;
break;
case InteractiveViewControllerStateCollapsed:
self.interactiveViewController.view.layer.cornerRadius = 0;
break;
default:
break;
}
}];
UIViewPropertyAnimator *blurAnimation = [[UIViewPropertyAnimator alloc] initWithDuration:duration dampingRatio:1 animations:^{
switch (state) {
case InteractiveViewControllerStateExpansed:
self.blurVisualEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
break;
case InteractiveViewControllerStateCollapsed:
self.blurVisualEffectView.effect = nil;
break;
default:
break;
}
}];
[animator startAnimation];
[cornerRadiusAnimation startAnimation];
[blurAnimation startAnimation];
[_runningAnimations addObject:animator];
[_runningAnimations addObject:cornerRadiusAnimation];
[_runningAnimations addObject:blurAnimation];
}
}
#pragma mark - UIPanGesture - Draggable View Controller -
-(void)userDidDragPanel:(UIPanGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
[self startInteractiveTransition:self.interactiveState duration:interactiveAnimationDuration];
break;
case UIGestureRecognizerStateChanged: {
CGPoint translation = [recognizer translationInView:self.interactiveViewController.view];
CGFloat fraction = translation.y / _interactiveControllerHeight;
fraction = self.interactiveStateIsExapanded ? fraction : -fraction;
[self updateInteractiveTransition:fraction];
}
break;
case UIGestureRecognizerStateEnded:
[self continueInteractiveTransition:0];
default:
break;
}
}
-(void)startInteractiveTransition:(InteractiveViewControllerState)state duration:(NSTimeInterval)duration {
if (_runningAnimations.count == 0) [self animateTransitionIfNeeded:state duration:duration];
for (UIViewPropertyAnimator *animator in _runningAnimations) {
[animator pauseAnimation];
_animationProgressWhenInterrupted = animator.fractionComplete;
}
}
-(void)updateInteractiveTransition:(CGFloat)fractionComplete {
for (UIViewPropertyAnimator *animator in _runningAnimations)
animator.fractionComplete = fractionComplete + _animationProgressWhenInterrupted;
}
-(void)continueInteractiveTransition:(CGFloat)fraction{
for (UIViewPropertyAnimator *animator in _runningAnimations)
[animator continueAnimationWithTimingParameters:nil durationFactor:fraction];
}
有人可以帮我解决这个问题吗?希望我能使自己理解...对于我有任何疑问的人