UIView shake animation

后端 未结 16 1276
梦毁少年i
梦毁少年i 2020-11-28 18:07

i\'m trying to make a UIView shake when a button is pressed.

I am adapting the code I found on http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-e

16条回答
  •  遥遥无期
    2020-11-28 18:52

    You can try the following code:

    + (void)vibrateView:(UIView*)view
    {
        CABasicAnimation *shiverAnimationR;
        shiverAnimationR = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        shiverAnimationR.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(1)];
        //shiverAnimationR.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(-10)];
        shiverAnimationR.duration = 0.1;
        shiverAnimationR.repeatCount = 1000000.0; // Use A high Value
        shiverAnimationR.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
        [view.layer addAnimation: shiverAnimationR forKey:@"shiverAnimationR"];
    
        CABasicAnimation * shiverAnimationL;
        shiverAnimationL = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //shiverAnimationL 2.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(10)];
        shiverAnimationL.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(-1)];
        shiverAnimationL.duration = 0.1;
        shiverAnimationL.repeatCount = 1000000.0;
        shiverAnimationL.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
        [view.layer addAnimation: shiverAnimationL forKey:@"shiverAnimationL"];
    
    }
    

    From the link.

提交回复
热议问题