iOS view中震动类似实现

#import <Foundation/Foundation.h>

@interface AnimationUtils : NSObject

+ (void)shake:(UIView *)view;

@end
//
//  AnimationUtils.m
//  TPL-AUTO-CLAIM-iPad
//
//  Created by Byron on 12-11-29.
//  Copyright (c) 2012年 cisetech. All rights reserved.
//

#import "AnimationUtils.h"

@implementation AnimationUtils

//震动效果
+ (void)shake:(UIView *)view
{
    CGRect frame = view.frame;
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2);
    int index;
    for (index = 3; index >=0; --index)
    {
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 - frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 + frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
    }
    CGPathCloseSubpath(shakePath);

    shakeAnimation.path = shakePath;
    shakeAnimation.duration = 0.5f;
    shakeAnimation.removedOnCompletion = YES;
    
    [view.layer addAnimation:shakeAnimation forKey:nil];
    CFRelease(shakePath);
}

@end
直接调用就可以了

你可能感兴趣的:(iOS view中震动类似实现)