为 UIAlertView 创建摇动动画
Create a shake animation for a UIAlertView
我正在尝试摇动 UIAlertView
。我已经让 alertView
停留在按钮点击时,但摇动动画不起作用。这是我拥有的:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self.direction = 1;
self.shakes = 0;
[self shake:aUIView];
}
}
-(void)shake:(UIAlertView *)theOneYouWannaShake
{
[UIView animateWithDuration:0.03 animations:^ {
theOneYouWannaShake.transform = CGAffineTransformMakeTranslation(5 * self.direction, 0);
}
completion:^(BOOL finished) {
if(self.shakes >= 10) {
theOneYouWannaShake.transform = CGAffineTransformIdentity;
return;
}
self.shakes++;
self.direction = self.direction * -1;
[self shake:theOneYouWannaShake];
}];
}
我尝试将 NSLog
放入 - (void)shake...
中,我得到了一个输出。所以我的代码一定有问题,为什么 alertView
没有摇晃。
您可以使用 UIKit Dynamics 实现 iOS 中的动画效果。这是 tutorial which using UIKit Dynamics for UIAlertView
shake animation. For more you can refer this tutorial 也位于 swift.
我创造了一种错觉,让 alertview 看起来像是在摇晃。前提是你必须设置 alertview(包括那个深色背景屏幕)应该摇晃到什么程度的参数。
因为你不能改变 UIAlertView 的框架,所以继续使用 CGTranformations。
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message:"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Done", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
UIView *dillusionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[dillusionView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:dillusionView];
[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x - 10, dialog.frame.origin.y);
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x + 10, dialog.frame.origin.y);
}];
} completion:nil];
我建议改用自定义创建的 UIAlertView,而且可用的开源库很少。
我正在尝试摇动 UIAlertView
。我已经让 alertView
停留在按钮点击时,但摇动动画不起作用。这是我拥有的:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self.direction = 1;
self.shakes = 0;
[self shake:aUIView];
}
}
-(void)shake:(UIAlertView *)theOneYouWannaShake
{
[UIView animateWithDuration:0.03 animations:^ {
theOneYouWannaShake.transform = CGAffineTransformMakeTranslation(5 * self.direction, 0);
}
completion:^(BOOL finished) {
if(self.shakes >= 10) {
theOneYouWannaShake.transform = CGAffineTransformIdentity;
return;
}
self.shakes++;
self.direction = self.direction * -1;
[self shake:theOneYouWannaShake];
}];
}
我尝试将 NSLog
放入 - (void)shake...
中,我得到了一个输出。所以我的代码一定有问题,为什么 alertView
没有摇晃。
您可以使用 UIKit Dynamics 实现 iOS 中的动画效果。这是 tutorial which using UIKit Dynamics for UIAlertView
shake animation. For more you can refer this tutorial 也位于 swift.
我创造了一种错觉,让 alertview 看起来像是在摇晃。前提是你必须设置 alertview(包括那个深色背景屏幕)应该摇晃到什么程度的参数。
因为你不能改变 UIAlertView 的框架,所以继续使用 CGTranformations。
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message:"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Done", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
UIView *dillusionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[dillusionView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:dillusionView];
[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x - 10, dialog.frame.origin.y);
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x + 10, dialog.frame.origin.y);
}];
} completion:nil];
我建议改用自定义创建的 UIAlertView,而且可用的开源库很少。