FirstApp,iphone开发学习总结13,方向感应和通告

方向感应:

在ImageViewController.h中添加UIAccelerometerDelegate:

@interface ImageViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAccelerometerDelegate>{
//...

ImageViewController.m文件内,初始化UIAccelerometer,在viewWillAppear:animated中:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIAccelerometer *acc = [UIAccelerometer sharedAccelerometer];
    [acc setUpdateInterval:0.1];//0.1s
    [acc setDelegate:self];
}

当view被移除,需要释放:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}

实现委托:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSLog(@"x:%f, y:%f, z:%f", [acceleration x], [acceleration y], [acceleration z]);
    //[self setNeedsDisplay];//可以刷新view
}

通告部分:

    //接受通告
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(nc:) name:@"a" object:nil];
    
    //创建通告
    NSDictionary *info =[NSDictionary dictionary];
    NSNotification *note = [NSNotification notificationWithName:@"a" object:self userInfo:info];
    [[NSNotificationCenter defaultCenter] postNotification:note];
posted @ 2012-05-16 09:22  Maxfong  阅读(327)  评论(0编辑  收藏  举报