How do you create a wiggle animation similar to iphone deletion animation

前端 未结 8 2316
-上瘾入骨i
-上瘾入骨i 2020-12-12 16:57

We are currently developing an application that contains a series of icons. We want the icons to wiggle like the app deletion animations when pressed. What would be the be

8条回答
  •  粉色の甜心
    2020-12-12 17:48

    The answer by Vinzius is very cool. However the wobble only rotates from 0 Radians to 0.08. Thus the wobble can look a little unbalanced. If you get this same issue then you may want to add both a negative and a positive rotation by using a CAKeyframeAnimation rather than a CABasicRotation:

    - (CAAnimation*)getShakeAnimation 
    {
        CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
        CGFloat wobbleAngle = 0.06f;
    
        NSValue* valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
        NSValue* valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
        animation.values = [NSArray arrayWithObjects:valLeft, valRight, nil];
    
        animation.autoreverses = YES;  
        animation.duration = 0.125;
        animation.repeatCount = HUGE_VALF;  
    
        return animation;    
    }
    

    You can use this animation method for your view or button like this.

    [self.yourbutton.layer addAnimation:[self getShakeAnimation] forKey:@""];
    

提交回复
热议问题