我正在尝试振动/摇动一个UI元素,即一个控件,就像它被击中一样,并且正在产生共鸣。 我可以使用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"];
使用以下错误的用户输入后,我正在摇动我的一个输入视图:
- (void)earthquake:(UIView*)itemView
{
CGFloat t = 2.0;
CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t);
CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t);
itemView.transform = leftQuake; // starting point
[UIView beginAnimations:@"earthquake" context:itemView];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:4];
[UIView setAnimationDuration:0.07];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];
itemView.transform = rightQuake; // end here & auto-reverse
[UIView commitAnimations];
}
- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue])
{
UIView* item = (UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
这看起来很自然,我从一些我不记得的互联网线程得到了这个代码。
您可以在图层位置使用关键帧动画。 这当然是主观的,因为我不确定你认为“讨人喜欢”。 当您输入OS X密码错误时获得的摇动动画我在Cocoa Is My Girlfriend的博客文章中使用Core Animation进行了复制。 不确定这是不是你想要的,但这是另一种选择。
最好的祝福,
更新Bersaelor在这里使用UIView
块动画的漂亮小摇动答案,有些人并不总是知道你可以在这些中使用自动反转和重复计数:
self.transform = CGAffineTransformMakeTranslation(2.0, -2.0);
[UIView animateWithDuration:0.07
delay:0.0
options:UIViewAnimationOptionAutoreverse
animations:^{
UIView.animationRepeatCount = 4;
self.transform = CGAffineTransformMakeTranslation(-2.0, 2.0);
}
completion:^(BOOL finished){
self.transform = CGAffineTransformIdentity;
}];