关闭

NSTimer的使用

363人阅读 评论(0) 收藏 举报
phone为我们提供了一个很强大得时间定时器 NSTimer
他可以完成任何定时功能:
我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理
delegate和事件处理方法@selector();就可以用
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 来初始化一个 时间定时器

NSTimer是Cocoa中比较常用的定时器类,基本操作如下:
handleTimer方法可以自行定义。在需要的地方创建timer即可,handleTimer就可以每0.5秒执行一次。

例:

 

- (void) handleTimer: (NSTimer *) timer
{
   //在这里进行处理
}
 
NSTimer *timer;
 
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
             target: self
             selector: @selector(handleTimer:)
             userInfo: nil
             repeats: YES];

 

 

 

下面我写了一个很简单得例子
-(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:@"2010-11-09 23:59:59"])
          {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME
                                                    message:@"现在马上就有新的一天了!"
                                                   delegate:self
                                          cancelButtonTitle: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];

                }

               
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:476042次
    • 积分:6224
    • 等级:
    • 排名:第4101名
    • 原创:62篇
    • 转载:594篇
    • 译文:0篇
    • 评论:16条
    最新评论