iOS icon jiggle algorithm

前端 未结 11 854
执笔经年
执笔经年 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条回答
  •  Happy的楠姐
    2020-12-12 17:05

    So I'm sure I'll get yelled at for writing messy code (there are probably simpler ways to do this that I am not aware of because I'm a semi-beginner), but this is a more random version of Vic320's algorithm that varies the amount of rotation and translation. It also decides randomly which direction it will wobble first, which gives a much more random look if you have multiple things wobbling simultaneously. If efficiency is a big problem for you, do not use. This is just what I came up with with the way that I know how to do it.

    For anyone wondering you need to #import and add it to your linked libraries.

    #define degreesToRadians(x) (M_PI * (x) / 180.0)
    
    - (void)startJiggling:(NSInteger)count {
        double kAnimationRotateDeg = (double)(arc4random()%5 + 5) / 10;
        double kAnimationTranslateX = (arc4random()%4);
        double kAnimationTranslateY = (arc4random()%4);
    
        CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) ));
        CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) ));
        int leftOrRight = (arc4random()%2);
        if (leftOrRight == 0){
            CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY);
            CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);
            self.transform = leftWobble;  // starting point
    
            [UIView animateWithDuration:0.1
                                  delay:(count * 0.08)
                                options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                             animations:^{ self.transform = conCatTransform; }
                             completion:nil];
        } else if (leftOrRight == 1) {
            CGAffineTransform moveTransform = CGAffineTransformTranslate(leftWobble, -kAnimationTranslateX, -kAnimationTranslateY);
            CGAffineTransform conCatTransform = CGAffineTransformConcat(leftWobble, moveTransform);
            self.transform = rightWobble;  // starting point
    
            [UIView animateWithDuration:0.1
                                  delay:(count * 0.08)
                                options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                             animations:^{ self.transform = conCatTransform; }
                             completion:nil];
        }
    }
    
    - (void)stopJiggling {
        [self.layer removeAllAnimations];
        self.transform = CGAffineTransformIdentity;  // Set it straight 
    }
    

提交回复
热议问题