晃动提醒

来源:互联网 发布:哪些软件看电影免费 编辑:程序博客网 时间:2024/04/28 02:16

#import <QuartzCore/QuartzCore.h>


@interface CAKeyframeAnimation (shakeAnimation)


+ (CAKeyframeAnimation *)shakeAnimation:(CGRect)frame;


@end






#import "CAKeyframeAnimation+shakeAnimation.h"


@implementation CAKeyframeAnimation (shakeAnimation)

static int numberOfShakes =8;//震动次数

static float durationOfShake =0.5f;//震动时间

static float vigourOfShake =0.02f;//震动幅度


+ (CAKeyframeAnimation *)shakeAnimation:(CGRect)frame

{

    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

    CGMutablePathRef shakePath =CGPathCreateMutable();

    CGPathMoveToPoint(shakePath,NULL,CGRectGetMidX(frame),CGRectGetMidY(frame) );

for (int index =0; index <numberOfShakes; ++index)

{

CGPathAddLineToPoint(shakePath,NULL,CGRectGetMidX(frame) - frame.size.width *vigourOfShake,CGRectGetMidY(frame));

CGPathAddLineToPoint(shakePath,NULLCGRectGetMidX(frame) + frame.size.width *vigourOfShake,CGRectGetMidY(frame));

}

    CGPathCloseSubpath(shakePath);

    shakeAnimation.path = shakePath;

    shakeAnimation.duration = durationOfShake;

    CFRelease(shakePath);

    

    return shakeAnimation;

}

@end