提要
给UIView通过类目扩展动画的方法,UIView对象直接传参调用,实现相应的动画效果。
实现
UIView+Animation.h
#import <UIKit/UIKit.h>
@interface UIView (Animation)
#pragma mark - position
/**
震动动画
@param times 震动次数 8
@param duration 震动时间 0.5
@param amplitude 震动幅度 0.03
@param key key
*/
- (void)shakeAnimationWithTimes:(NSUInteger)times
duration:(float)duration
amplitude:(float)amplitude
forKey:(nonnull NSString *)key;
#pragma mark - transform
/**
伸缩变化
@param repeatCount 动画重复次数 HUGE表示无限循环
@param duration 动画时长 1.5
@param scaleValue 放大(缩小)倍数 1.5
@param key key
*/
- (void)scallEnlargeAndReductionWithRepeateCount:(NSUInteger)repeatCount
duration:(float)duration
enlargeScale:(float)scaleValue
forKey:(nonnull NSString *)key;
/**
移除
@param key 动画对应的key
*/
- (void)removeAnimationForKey:(nonnull NSString *)key;
@end
UIView+Animation.m
#import "UIView+Animation.h"
@implementation UIView (Animation)
/**
震动动画
@param times 震动次数 8
@param duration 震动时间 0.5
@param amplitude 震动幅度 0.03
*/
- (void)shakeAnimationWithTimes:(NSUInteger)times
duration:(float)duration
amplitude:(float)amplitude
forKey:(nonnull NSString *)key
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGFloat width = CGRectGetWidth(self.frame);
CGFloat midX = CGRectGetMidX(self.frame);
CGFloat midY = CGRectGetMidY(self.frame);
CGMutablePathRef shakePath = CGPathCreateMutable();
CGPathMoveToPoint(shakePath, NULL, midX, midY);
for (int i = 0; i < times; i++)
{
CGPathAddLineToPoint(shakePath, NULL, width - width * amplitude, midY);
CGPathAddLineToPoint(shakePath, NULL, midX + width * amplitude, midY);
}
CGPathCloseSubpath(shakePath);
animation.path = shakePath;
animation.duration = duration;
CFRelease(shakePath);
[self.layer addAnimation:animation forKey:key];
}
/**
伸缩变化
@param repeatCount 动画重复次数 HUGE表示无限循环
@param duration 动画时长 1.5
@param scaleValue 放大(缩小)倍数 1.5
@param key key
*/
- (void)scallEnlargeAndReductionWithRepeateCount:(NSUInteger)repeatCount
duration:(float)duration
enlargeScale:(float)scaleValue
forKey:(nonnull NSString *)key {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
NSMutableArray *values = [[NSMutableArray alloc] init];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1.0)]];
animation.values = values;
animation.duration = duration;
animation.repeatCount = repeatCount;
[self.layer addAnimation:animation forKey:key];
}
/**
移除
@param key 动画对应的key
*/
- (void)removeAnimationForKey:(nonnull NSString *)key {
[self.layer removeAnimationForKey:key];
}
@end
说明
个人笔记,更多方法将持续更新。gitHub地址:https://github.com/Mexiang/UIView-Animation有兴趣的可以自己添加自定义方法实现更多动画效果,方便我们在项目中使用。
网友评论