iphone - 我有基本功能的加速度计代码,现在我需要让它显示文本或播放音频等

标签 iphone objective-c ios xcode accelerometer

我正在使用加速度计创建一个学习练习项目。

我已经有了代码的基本功能,现在我只需要让它做点什么。显示消息、播放音频或显示图像。

<强>!!编辑!!

//  ACViewController.m
//  Accelerometer
//


#import "ACViewController.h"

@interface ACViewController ()

- (void) startAccelerometer;
- (void) stopAccelerometer;

@end

@implementation ACViewController

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

{     //!!EDIT!!
    double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
    || fabsf(acceleration.y) > kThreshold
    || fabsf(acceleration.z) > kThreshold)

    UILabel * theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 640)];
    theLabel.text = @"Hello earth";
    [self.theLabel addSubView:theLabel];

//编辑结束

- (void)startAccelerometer {
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 0.25;
}

- (void)stopAccelerometer {
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = nil;
}

- (void)viewDidAppear:(BOOL)animated {
    [self startAccelerometer];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self stopAccelerometer];
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

头文件:

//
//  ACViewController.h
//  Accelerometer


#import <UIKit/UIKit.h>

@interface ACViewController : UIViewController <UIAccelerometerDelegate>

@end

最佳答案

添加图像的示例(假设您使用 ARC):

在 NSLog 行中,键入:

[self.view addSubView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"myImage.png"]]];

标签示例 (ARC):

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 640)];
label.text = @"Hello earth";
[self.view addSubView:label];

不使用ARC时,将实例添加到 View 后释放。

关于iphone - 我有基本功能的加速度计代码,现在我需要让它显示文本或播放音频等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9951640/

相关文章:

iphone - 如何在 iPhone 中播放视频

iPhone - 行为类似于 UIViewController 的 UIView

javascript - 有更好的 Android 支持的 JQTouch 的 JQuery 替代品吗?

iphone - 在 iPhone 上管理 HTTP Cookie

ios - 从 ios 自定义键盘扩展上的 UIButton 动态文本输入

ios - 从模态视图返回时,向已打开的 View 写入一些内容

iphone - Twitter+OAuth+iPhone 的问题。 children 在哭

iphone - 如何在 iphone 的 UITableView 中设置单元格的背景颜色?

ios - "copy"选项不显示 UITableView 行

ios - 针对 iOS 的 Objective-C 方法优化性能