方向感应:

在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];

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2021-04-16
  • 2022-01-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案