zoukankan      html  css  js  c++  java
  • 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];
  • 相关阅读:
    Git远程库
    Git的使用
    如何利用IO流复制文件
    Lambda表达式
    Lambda表达式的标准格式
    线程池的使用步骤
    解决线程安全的三个方法
    多线程
    位运算
    如何用javadoc生成java帮助文档
  • 原文地址:https://www.cnblogs.com/maxfong/p/2503412.html
Copyright © 2011-2022 走看看