문제

I've been trying to implement this but couldn't find any code that could shake it like iPhone app shakes while deleting them!

CABasicAnimation *animation = 
                         [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDuration:0.05];
[animation setRepeatCount:8];
[animation setAutoreverses:YES];
[animation setFromValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x - 20.0f, [lockView center].y)]];
[animation setToValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x + 20.0f, [lockView center].y)]];
[[lockView layer] addAnimation:animation forKey:@"position"];
도움이 되었습니까?

해결책

you can achieve this by the following code, you have define the rotation angle from and to.

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
   [anim setToValue:[NSNumber numberWithFloat:0.0f]];
   [anim setFromValue:[NSNumber numberWithDouble:M_PI/16]]; // rotation angle
   [anim setDuration:0.1];
   [anim setRepeatCount:NSUIntegerMax];
   [anim setAutoreverses:YES];
   [[imageView layer] addAnimation:anim forKey:@"iconShake"];

다른 팁

try this:

- (void)animateImage 
{
    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;  

    [[lockView layer] addAnimation:animation forKey:@"shakeAnimation"];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top