#import "YBPhothFlyImageView.h"
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@interface YBPhothFlyImageView()<CAAnimationDelegate>
@end
@implementation YBPhothFlyImageView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)startFlyAnimation
{
UIBezierPath *phothPath = [UIBezierPath bezierPath];
[phothPath moveToPoint:self.center];
CGPoint endPoint = CGPointMake(KScreenWidth/2.0, KScreenHeight/2.0);
// 随机数
NSInteger i = arc4random_uniform(2);
NSInteger rotationDirection = 1 - (2*i);
CGPoint controlPoint = CGPointMake(KScreenWidth/2.0 + (100+arc4random_uniform(KScreenWidth/2.0))*rotationDirection, (KScreenHeight-130-KScreenHeight/2.0)/2.0+ KScreenHeight/2.0 + rotationDirection*(30 + arc4random_uniform(30)));
[phothPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.path = phothPath.CGPath;
keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = @1;
opacityAnimation.toValue = @0;
CABasicAnimation *scallAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scallAnimation.fromValue = @1;
scallAnimation.toValue = @0.5;
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimation.toValue = @(rotationDirection*2*M_PI);
CAAnimationGroup *positionGroup = [CAAnimationGroup animation];
positionGroup.animations = @[keyFrameAnimation,opacityAnimation,scallAnimation,rotateAnimation];
positionGroup.duration = 1;
positionGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
positionGroup.delegate = self;
//如果不加这句[self animationForKey:@"ani1"]是nil
// positionGroup.removedOnCompletion = NO;
// positionGroup.fillMode = kCAFillModeForwards;
//kvc记录layer的值方便在代理方法里移除
// [positionGroup setValue:self forKey:@"likeLayer"];
[self.layer addAnimation:positionGroup forKey:@"positionGroup"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self removeFromSuperview];
}
@end
图片沿轨迹旋转、缩放、透明。
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...