图片沿轨迹旋转、缩放、透明。

#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

demo地址https://github.com/yinbowang/YBPhothFlyImageView.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转眼,岁月的车轮就驶过了2016年。 回顾一年来走过的路程:工作,生活,以及婚姻大事......自己算是度过了普通...
    风行天上阅读 2,647评论 32 54
  • “我哒哒的马蹄是个美丽的错误, 我不是归人, 是个过客……” 我写下曾经最喜欢的字句 不敢再讲多一句
    王非王阅读 203评论 0 0
  • 首先问大家一个问题 秦皇岛到底有多大??? 美术生说:“这么大吧~” 喵星人说:“可能这么大?” 修图师说:“应该...
    车盟车生活阅读 1,064评论 1 2