您的位置:首页 > 产品设计 > UI/UE

iOS开发笔记-UIActionSheet的相关属性设置

2015-09-23 22:37 399 查看
在iOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张。而是很温和地在继续流程之前给用户提供了诸多选择。

1.普通的sheet框使用

同UIAlertView一样,sheet也可以很简单的创建并且显示.

1 - (IBAction)actionSheetShow:(id)sender {
2     //destructiveButton 的颜色和其他按钮不同,呈现红色
3     //表示用户需要注意,一般用于不可逆操作
4     UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"Hello" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"此项需要用户注意" otherButtonTitles: nil];
5     [sheet showInView:self.view];
6 }


注意:如果我们开发了一款基于多标签页的程序(UITabbar),在每个标签页中显示sheet框的方法会发生不同。这时要调用“showFromTabBar”的方式来代替“showInView:aView”,否则界面的显示层次会发生问题。同样的,如果视图的底部有一条工具栏,我们可以调用“showFromToolbar”的方法来显示sheet框而不是“shwoInView:aView”.

如果显示的选择项过多,会出现滚动条选项在actionSheet

之后,同样可以使用代理方法获取用户的选择

1 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
2 {
3     id buttonSheet=[actionSheet buttonTitleAtIndex:buttonIndex];
4     NSLog(@"按下了%@按钮",buttonSheet);
5 }


2.含进度条的sheet框

我们介绍进度条的sheet框,不显示任何按钮。同样借用模态的特性达到开发的某些目的。

1 -(void)showProgressOnView:(UIView *)aView
2 {
3     if(!aView)
4     {
5         return;
6     }
7
8     //sheet的创建
9     UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"这时进度条\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
10
11     //进度条创建
12     UIProgressView *progress=[[UIProgressView alloc]initWithFrame:CGRectMake(0.0f, 40.0f, 220.0f, 90.0f)];
13
14     //进度条配置
15     progress.progressViewStyle = UIProgressViewStyleDefault;
16     progress.progress=0.0f;
17     [sheet addSubview:progress];
18
19     //起一个定时器,来更新进度条
20     [NSTimer scheduledTimerWithTimeInterval:0.03f target:self selector:@selector(updateProgress:) userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:progress,sheet, nil] forKeys:[NSArray arrayWithObjects:@"progress",@"sheet", nil]] repeats:YES];
21
22     [sheet showInView:self.view];
23     progress.center=CGPointMake(CGRectGetWidth(sheet.bounds)/2.0, CGRectGetHeight(sheet.bounds)/2.0);
24 }


NSTimer的响应函数如下:

1 //NSTimer的响应函数如下
2 -(void)updateProgress:(NSTimer *)aTimer
3 {
4     UIProgressView *progress = nil;
5     UIActionSheet *sheet =nil;
6
7     //将定时器中有用的内容取出来
8     NSDictionary *dicInfo =aTimer.userInfo;
9     if(!dicInfo)
10     {
11         return;
12     }
13
14     progress = [dicInfo objectForKey:@"progress"];
15     if(!progress)
16     {
17         return;
18     }
19
20     //进度条满了
21     if(progress.progress>=3.0f)
22     {
23         sheet = [dicInfo objectForKey:@"sheet"];
24         //将sheet关闭
25         if(sheet)
26         {
27             [sheet dismissWithClickedButtonIndex:0 animated:YES];
28         }
29         //定时器销毁
30         [aTimer invalidate];
31
32     }
33     else
34     {
35         //进度条的速度
36         progress.progress+=0.01f;
37     }
38 }


3.含自定义控件的sheet框

sheet框是一个UIView的子类,因而他不仅能够提供按钮选项,一定也支持往他身上加其他功能的控件。

=================================

延伸拓展:NSTimer类的使用

创建一个 Timer

+ scheduledTimerWithTimeInterval: invocation: repeats:

+ (NSTimer )scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;

+ scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

创建返回一个新的NSTimer对象和时间表,在当前的默认模式下循环调用一个实例方法。

+ timerWithTimeInterval: invocation: repeats:

+ (NSTimer )timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;

+ timerWithTimeInterval: target:selector: userInfo:repeats:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

– initWithFireDate: interval: target: selector: userInfo: repeats:

- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

预订一个Timer,设置一个时间间隔。

表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1

target:(id)aTarget

表示发送的对象,如self

selector:(SEL)aSelector

方法选择器,在时间间隔内,选择调用一个实例方法

userInfo:(id)userInfo

此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。

repeats:(BOOL)yesOrNo

当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。

invocation:(NSInvocation *)invocation

启动 Timer

• – fire

停止 Timer

• – invalidate

Timer设置

• – isValid

• – fireDate

• – setFireDate:

• – timeInterval

• – userInfo

NSTimeInterval类:是一个浮点数字,用来定义秒

例子:

iphone为我们提供了一个很强大得时间定时器 NSTimer

他可以完成任何定时功能:

我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理

delegate和事件处理方法@selector();就可以用

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 来初始化一个 时间定时器

下面我写了一个很简单得例子

初始化一个定时器:

-(void)initTimer
{
//时间间隔
NSTimeInterval timeInterval =1.0 ;
//定时器
NSTimer   showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime
target:self
selector:@selector(handleMaxShowTimer:)
userInfo:nil
repeats:NO];
}
//触发事件
-(void)handleMaxShowTimer:(NSTimer *)theTimer
{
NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateFormat = @"yyyy-MM-dd  HH:mm:ss";
NSString *date = [dateformater stringFromDate:[NSDate date]];
if([date isEqualToString:@"2011-11-09 23:59:59"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME
message:@"现在马上就有新的一天了!"
delegate:self
ancelButtonTitle:nil
otherButtonTitles:CONFIRM_TITLE, nil];
[alert show];
[alert release];
}
[data release];
[dateFormator release];
}
另外附一个例子:方框赛跑
-(void)viewDidLoad
{

[super viewDidLoad];
CGRect workingFrame;

workingFrame.origin.x = 15;
workingFrame.origin.y = 400;
workingFrame.size.width = 40;
workingFrame.size.height = 40;

for(int i = 0; i < 6; i++)
{
UIView *myView = [[UIView alloc] initWithFrame:workingFrame];
[myView setTag:i];//标记方块
[myView setBackgroundColor:[UIColor blueColor]];

workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;
[self.view addSubview:myView];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: