考虑编写自己的对话框弹出窗口,而不是与 UIAlertView 混为一谈。如果您想要可以使用变换动画轻松实现的“反弹”动画。这是我刚刚制作的一个简单的弹出对话框。
为了测试它,创建一个新的基于视图的应用程序项目并添加这个类。然后您可以通过将下面的“使用”代码添加到您的 *ViewController.m
来测试它
这是一个演示理论的非常简单的例子。在 SimpleDialog 中有一个显示弹出窗口的静态方法会更有意义,但我不想让示例过于复杂。
SimpleDialog.h
#import <UIKit/UIKit.h>
@interface SimpleDialog : UIView
{
}
- (void) show;
@end
SimpleDialog.m
#import "SimpleDialog.h"
#define BOUNCE_SPEED 1
@implementation SimpleDialog
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.backgroundColor = [UIColor greenColor];
UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(0, 0, 200, 20);
closeButton.center = self.center;
[closeButton setTitle:@"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
}
return self;
}
- (void) show
{
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:self];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounceInStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounceInStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounceOutStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounceOutStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
- (void) hide
{
[self removeFromSuperview];
}
@end
用法
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* popButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
popButton.frame = CGRectMake(0, 0, 200, 20);
popButton.center = self.view.center;
[popButton setTitle:@"Pop" forState:UIControlStateNormal];
[popButton addTarget:self action:@selector(showIt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popButton];
}
- (void) showIt
{
SimpleDialog* simple = [[SimpleDialog alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
simple.center = self.view.center;
[self.view addSubview:simple];
[simple show];
[simple release];
}