ios - 像通知中心弹跳一样的可拉式 View

标签 ios iphone ipad

我已经使用 Pan Gesture 创建了 Pullable View ,一切正常,但存在弹跳问题。当我们向上滑动 View 时,在动画结束时出现弹跳,就像通知中心在 View 动画结束时弹跳一样。我已经为 Bounces 创建了一个 bounces Category,但我没有得到与 Notification Center 相同的 bounces。必须对弹跳进行一些调整。所以请帮助我。以下是我的代码。

#import "UIView+Bounce.h"
@implementation UIView (Bounce)
+ (CAKeyframeAnimation *)dockBounceAnimationWithViewHeight:(CGFloat)viewHeight {
    NSUInteger const kNumFactors    = 23;
    CGFloat const kFactorsPerSec    = 120.0f;
    CGFloat const kFactorsMaxValue  = 128.0f;

    CGFloat factors[kNumFactors]    = { 0,  83, 100, 114, 124, 138, 156, 184, 156, 138, 124, 114, 100, 83, 32, 0, 0, 18, 28, 32, 28, 18, 0 };

    NSMutableArray *transforms = [NSMutableArray array];

    for (NSUInteger i = 0; i < kNumFactors; i++) {
        CGFloat positionOffset  = factors[i] / kFactorsMaxValue * viewHeight;
        CATransform3D transform = CATransform3DMakeTranslation(0.0f, -positionOffset, 0.0f);

        [transforms addObject:[NSValue valueWithCATransform3D:transform]];
    }

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.repeatCount           = 1;
    animation.duration              = kNumFactors * 1.0f / kFactorsPerSec;
    animation.fillMode              = kCAFillModeForwards;
    animation.values                = transforms;
    animation.removedOnCompletion   = YES; // final stage is equal to starting stage
    animation.autoreverses          = NO;

    return animation;
}
- (void)bounce:(float)bounceFactor {
    CGFloat midHeight = self.frame.size.height * bounceFactor;
    CAKeyframeAnimation *animation = [[self class] dockBounceAnimationWithViewHeight:midHeight];
    [self.layer addAnimation:animation forKey:@"bouncing"];
}
@end

最佳答案

您的目标是 iOS7+ 吗?在这种情况下,功能是内置的。您可以使用:

+ (void)animateWithDuration:(NSTimeInterval)duration 
                      delay:(NSTimeInterval)delay 
     usingSpringWithDamping:(CGFloat)dampingRatio 
      initialSpringVelocity:(CGFloat)velocity 
                    options:(UIViewAnimationOptions)options 
                 animations:(void (^)(void))animations 
                 completion:(void (^)(BOOL finished))completion

该方法的文档是 here ,基本上这是一种制作弹跳动画的简单方法。

我们的做法是在 iOS7+ 上使用反弹,在 iOS6 上我们使用另一种动画。

关于ios - 像通知中心弹跳一样的可拉式 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904620/

相关文章:

ios - 如何从现有的 UIScrollView 中获取重复的 UIScrollView?

iphone - 帮我解决错误 _deflateInit2_

ios - 如何在其他 ViewController 中继续下载数据

ios - 在多个文件中重复使用相同的 UIColor

ios - 在 Foursquare api 中获取附近 field 在 iOS 9 测试版中不起作用

ios - Whatsapp 消息布局 - 时间和消息在同一行

ios - 在左侧对齐 UIButton 的图像和 UILabel

iphone - iOS 通话中状态栏覆盖顶部导航栏

iphone - 如何在 iOS 中缓存带有图像的整个网页

ios - iPad 的通用应用程序无法加载 iPad .xib 文件?