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];
  • 相关阅读:
    java基础—面向对象2
    java基础—java读取properties文件
    java基础—super关键字
    java基础—this关键字
    java基础—object类
    java基础—equals方法
    java基础—哈希编码
    Struct2(五)处理表单
    struct2(四)编写Struct2 的Action
    Struct2(三) Struct2 标签
  • 原文地址:https://www.cnblogs.com/maxfong/p/2503412.html
Copyright © 2011-2022 走看看