粒子动画

2016-11-23  本文已影响18人  seventhboy

@property(nonatomic, strong) CAEmitterLayer *emitterLayer;

static CGFloat PI = M_PI;

-(void)click {
[self animateInView:self.view];
}

-(void)animateInView:(UIView *)view {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor yellowColor];
imageView.frame = CGRectMake(100, 400, 30, 30);
int nameI = arc4random() % 8 + 1;
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_fanpian_0%d.png",nameI]];
imageView.image = image;

[view addSubview:imageView];

// Pre-Animation setup
imageView.transform = CGAffineTransformMakeScale(0, 0);
imageView.alpha = 0;

//在底部 由无到有 又小变大的弹性动画
[UIView animateWithDuration:0.5
                      delay:0.0
     usingSpringWithDamping:0.6
      initialSpringVelocity:0.8
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     imageView.transform = CGAffineTransformIdentity;
//                       imageView.transform = CGAffineTransformScale(imageView.transform, 4, 4);
                     imageView.alpha = 0.9;
                 }
                 completion:NULL];
NSInteger i = arc4random_uniform(2);
NSInteger rotationDirection = 1 - (2 * i); // -1 OR 1
NSInteger rotationFraction = arc4random_uniform(10);

NSTimeInterval totalAnimationDuration = 3;
//放大后向左或向右旋转一定角度动画
[UIView
 animateWithDuration:totalAnimationDuration
 animations:^{
     imageView.transform = CGAffineTransformMakeRotation(  rotationDirection * M_PI / (16 + rotationFraction * 0.2));
 }
 completion:NULL];

// S型动画
CGFloat heartSize = CGRectGetWidth(imageView.bounds);
CGFloat heartCenterX = imageView.center.x;
CGFloat viewHeight = CGRectGetHeight(view.bounds);

UIBezierPath *heartTravelPath = [UIBezierPath bezierPath];
[heartTravelPath moveToPoint:imageView.center];

// 随机结束点
CGPoint endPoint = CGPointMake( heartCenterX + (rotationDirection)*arc4random_uniform(2 * heartSize),
                               viewHeight / 6.0 + arc4random_uniform(viewHeight / 4.0));
NSInteger j = arc4random_uniform(2);
NSInteger travelDirection = 1 - (2 * j); // -1 OR 1

//绘制S型曲线 随机点
CGFloat xDelta = (heartSize / 2.0 + arc4random_uniform(2 * heartSize)) * travelDirection;
CGFloat yDelta =  MAX(endPoint.y, MAX(arc4random_uniform(8 * heartSize), heartSize));

//控制点1 控制点2
CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, yDelta);
CGPoint controlPoint2 = CGPointMake(heartCenterX - xDelta, yDelta);

// CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, viewHeight - yDelta);
// CGPoint controlPoint2 = CGPointMake(heartCenterX - 2 * xDelta, yDelta);

[heartTravelPath addCurveToPoint:endPoint
                   controlPoint1:controlPoint1
                   controlPoint2:controlPoint2];
CAKeyframeAnimation *keyFrameAnimation =
[CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.path = heartTravelPath.CGPath;
keyFrameAnimation.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
keyFrameAnimation.duration = totalAnimationDuration + endPoint.y / viewHeight;
[imageView.layer addAnimation:keyFrameAnimation forKey:@"positionOnPath"];

// 动画消失
[UIView animateWithDuration:totalAnimationDuration
                 animations:^{
                     imageView.alpha = 0.0;
                 }
                 completion:^(BOOL finished) {
                     [imageView removeFromSuperview];
                 }];
dispatch_after(
               dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)),
               dispatch_get_main_queue(), ^{
               });

}

上一篇下一篇

猜你喜欢

热点阅读