Я пытаюсь вибрировать / встряхивать элемент пользовательского интерфейса, а именно элемент управления, как если бы он был поражен, и резонирует. Я могу использовать Core Animation, чтобы встряхнуть его вертикально, горизонтально или обоими на один пиксель:
CABasicAnimation* rotationXAnimation;
rotationXAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
rotationXAnimation.fromValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y-1.0 ];
rotationXAnimation.toValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y+1.0 ];
rotationXAnimation.duration = 0.2;
rotationXAnimation.cumulative = NO;
rotationXAnimation.repeatCount = 10.0;
rotationXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[((UIControl *) sender).layer addAnimation:rotationXAnimation forKey:@"upDownAnimation"];
Или я могу вращаться на небольшую дугу назад и вперед вокруг оси z (-M_PI * 0,02 и M_PI * 0,02):
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat: -M_PI * 0.02 ];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 0.02 ];
rotationAnimation.duration = 0.2;
rotationAnimation.cumulative = NO;
rotationAnimation.repeatCount = 10.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[((UIControl *) sender).layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
Ничто из этого не выглядит так приятно. У кого-нибудь есть хорошие альтернативы? Я был бы особенно признателен за отличные идеи keyPaths.
Спасибо!
Обновление:
Следующее, где я сокращаю и расширяю контроль, лучше, но я все еще ищу другие идеи.
CABasicAnimation* scaleAnimation;
scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat: 0.95 ];
scaleAnimation.toValue = [NSNumber numberWithFloat: +1.05 ];
scaleAnimation.duration = 0.1;
scaleAnimation.cumulative = NO;
scaleAnimation.repeatCount = 10.0;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[((UIControl *) sender).layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];