How to do a curve/arc animation with CAAnimation?

前端 未结 5 673
离开以前
离开以前 2021-01-30 09:35

I have an user interface where an item get deleted, I would like to mimic the \"move to folder\" effect in iOS mail. The effect where the little letter icon is \"thrown\" into t

5条回答
  •  太阳男子
    2021-01-30 10:10

    Try this it will solve your problem definitely, I have used this in my project:

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 126, 320, 24)] autorelease];
    label.text = @"Animate image into trash button";
    label.textAlignment = UITextAlignmentCenter;
    [label sizeToFit];
    [scrollView addSubview:label];
    
    UIImageView *icon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"carmodel.png"]] autorelease];
    icon.center = CGPointMake(290, 150);
    icon.tag = ButtonActionsBehaviorTypeAnimateTrash;
    [scrollView addSubview:icon];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.center = CGPointMake(40, 200);
    button.tag = ButtonActionsBehaviorTypeAnimateTrash;
    [button setTitle:@"Delete Icon" forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];
    [scrollView bringSubviewToFront:icon];
    
    - (void)buttonClicked:(id)sender {
        UIView *senderView = (UIView*)sender;
        if (![senderView isKindOfClass:[UIView class]])
            return;
    
        switch (senderView.tag) {
            case ButtonActionsBehaviorTypeExpand: {
                CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
                anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                anim.duration = 0.125;
                anim.repeatCount = 1;
                anim.autoreverses = YES;
                anim.removedOnCompletion = YES;
                anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)];
                [senderView.layer addAnimation:anim forKey:nil];
    
                break;
            }
    
            case ButtonActionsBehaviorTypeAnimateTrash: {
                UIView *icon = nil;
                for (UIView *theview in senderView.superview.subviews) {
                    if (theview.tag != ButtonActionsBehaviorTypeAnimateTrash)
                        continue;
                    if ([theview isKindOfClass:[UIImageView class]]) {
                        icon = theview;
                        break;
                    }
                }
    
                if (!icon)
                    return;
    
                UIBezierPath *movePath = [UIBezierPath bezierPath];
                [movePath moveToPoint:icon.center];
                [movePath addQuadCurveToPoint:senderView.center
                                 controlPoint:CGPointMake(senderView.center.x, icon.center.y)];
    
                CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
                moveAnim.path = movePath.CGPath;
                moveAnim.removedOnCompletion = YES;
    
                CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
                scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
                scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
                scaleAnim.removedOnCompletion = YES;
    
                CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
                opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
                opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
                opacityAnim.removedOnCompletion = YES;
    
                CAAnimationGroup *animGroup = [CAAnimationGroup animation];
                animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, opacityAnim, nil];
                animGroup.duration = 0.5;
                [icon.layer addAnimation:animGroup forKey:nil];
    
                break;
            }
        }
    }
    

提交回复
热议问题