iOS icon jiggle algorithm

前端 未结 11 893
执笔经年
执笔经年 2020-12-12 16:31

I am writing an iPad app that presents user documents similar to the way Pages presents them (as large icons of the actual document). I also want to mimic the jiggling beha

11条回答
  •  孤街浪徒
    2020-12-12 16:55

    For completeness, here is how I animated my CALayer subclass — inspired by the other answers — using an explicit animation.

    -(void)stopJiggle 
    {
        [self removeAnimationForKey:@"jiggle"];
    }
    
    -(void)startJiggle 
    {
        const float amplitude = 1.0f; // degrees
        float r = ( rand() / (float)RAND_MAX ) - 0.5f;
        float angleInDegrees = amplitude * (1.0f + r * 0.1f);
        float animationRotate = angleInDegrees / 180. * M_PI; // Convert to radians
    
        NSTimeInterval duration = 0.1;
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        animation.duration = duration;
        animation.additive = YES;
        animation.autoreverses = YES;
        animation.repeatCount = FLT_MAX;
        animation.fromValue = @(-animationRotate);
        animation.toValue = @(animationRotate);
        animation.timeOffset = ( rand() / (float)RAND_MAX ) * duration;
        [self addAnimation:animation forKey:@"jiggle"];
    }
    

提交回复
热议问题