如何为iPhone创建此类警报?
答案 0 :(得分:4)
答案 1 :(得分:2)
为此,您只需创建视图 - 其中包含uiview,tableview和按钮。 CustomAlertView的代码。
UIView-AlertAnimations.h // user defined class
@interface UIView(AlertAnimations)
- (void)doPopInAnimation;
- (void)doPopInAnimationWithDelegate:(id)animationDelegate;
- (void)doFadeInAnimation;
- (void)doFadeInAnimationWithDelegate:(id)animationDelegate;
@end
UIView-AlertAnimations.m
#import "UIView-AlertAnimations.h"
#import <"QuartzCore/QuartzCore.h">
#define kAnimationDuration 0.2555
@implementation UIView(AlertAnimations)
- (void)doPopInAnimation
{
[self doPopInAnimationWithDelegate:nil];
}
- (void)doPopInAnimationWithDelegate:(id)animationDelegate
{
CALayer *viewLayer = self.layer;
CAKeyframeAnimation* popInAnimation =[CAKeyframeAnimation
animationWithKeyPath:@"transform.scale"];
popInAnimation.duration = kAnimationDuration;
popInAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:1.1],
[NSNumber numberWithFloat:.9],
[NSNumber numberWithFloat:1],
nil];
popInAnimation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.8],
[NSNumber numberWithFloat:1.0],
nil];
popInAnimation.delegate = animationDelegate;
[viewLayer addAnimation:popInAnimation forKey:@"transform.scale"];
}
- (void)doFadeInAnimation
{
[self doFadeInAnimationWithDelegate:nil];
}
- (void)doFadeInAnimationWithDelegate:(id)animationDelegate
{
CALayer *viewLayer = self.layer;
CABasicAnimation *fadeInAnimation = [CABasicAnimation
animationWithKeyPath:@"opacity"];
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeInAnimation.duration = kAnimationDuration;
fadeInAnimation.delegate = animationDelegate;
[viewLayer addAnimation:fadeInAnimation forKey:@"opacity"];
}
@end
CustomAlertView.m
@implementation CustomAlertView
- (IBAction)show
{
// Retaining self is odd, but we do it to make this "fire and forget"
[self retain];
// We need to add it to the window, which we can get from the delegate
id appDelegate = [[UIApplication sharedApplication] delegate];
UIWindow *window = [appDelegate window];
[window addSubview:self.view];
// Make sure the alert covers the whole window
self.view.frame = window.frame;
self.view.center = window.center;
// "Pop in" animation for alert
[alertView doPopInAnimationWithDelegate:self];
// "Fade in" animation for background
[backgroundView doFadeInAnimation];
}
- (IBAction)dismiss:(id)sender
{
[inputField resignFirstResponder];
[UIView beginAnimations:nil context:nil];
self.view.alpha = 0.0;
[UIView commitAnimations];
[self performSelector:@selector(alertDidFadeOut) withObject:nil afterDelay:0.5];
if (sender == self || [sender tag] == CustomAlertViewButtonTagOk)
[delegate CustomAlertView:self wasDismissedWithValue:inputField.text];
else
{
if ([delegate respondsToSelector:@selector(customAlertViewWasCancelled:)])
[delegate customAlertViewWasCancelled:self];
}
}
- (void)alertDidFadeOut
{
[self.view removeFromSuperview];
[self autorelease];
}
CAAnimation代表方法
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.inputField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self dismiss:self];
return YES;
}
@end
- (IBAction)show
{
// Retaining self is odd, but we do it to make this "fire and forget"
[self retain];
// We need to add it to the window, which we can get from the delegate
id appDelegate = [[UIApplication sharedApplication] delegate];
UIWindow *window = [appDelegate window];
[window addSubview:self.view];
// Make sure the alert covers the whole window
self.view.frame = window.frame;
self.view.center = window.center;
// "Pop in" animation for alert
[alertView doPopInAnimationWithDelegate:self];
// "Fade in" animation for background
[backgroundView doFadeInAnimation];
}
- (IBAction)dismiss:(id)sender
{
[inputField resignFirstResponder];
[UIView beginAnimations:nil context:nil];
self.view.alpha = 0.0;
[UIView commitAnimations];
[self performSelector:@selector(alertDidFadeOut) withObject:nil afterDelay:0.5];
if (sender == self || [sender tag] == CustomAlertViewButtonTagOk)
[delegate CustomAlertView:self wasDismissedWithValue:inputField.text];
else
{
if ([delegate respondsToSelector:@selector(customAlertViewWasCancelled:)])
[delegate customAlertViewWasCancelled:self];
}
}
- (void)alertDidFadeOut
{
[self.view removeFromSuperview];
[self autorelease];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.inputField becomeFirstResponder];
}
最后,我们实现了一个文本字段委托方法,以便当用户按下键盘上的返回键时,它会关闭对话框。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self dismiss:self];
return YES;
}
此时,我们已经完成了。我们现在可以使用这个自定义警报视图,就像我们使用UIAlertView一样:
CustomAlertView *alert = [[CustomAlertView alloc]init];
alert.delegate = self;
[alert show];
[alert release];
当然这对你有用。
答案 2 :(得分:0)
你必须自己创建一个这样的视图。
我能想到的最简单的方法就是创建一个UIView,然后以模态方式呈现它。
答案 3 :(得分:0)
查看http://starterstep.wordpress.com/2009/03/24/custom-uialertview-with-uitableview/和http://starterstep.wordpress.com/2009/09/16/custom-uialertview-with-tableview-part-2/
答案 4 :(得分:0)