实例iPhone编程入门教程-第十一天

DAY ELEVEN – PlumbBob

今天来建立一个 iPhone app 软件,用你的iPhone 当作测量水平器。



纲要:
- 在程序显示前运行代码 -
- 加入QUartzCore Frameworks -
- 关于iPhone的“Utility Application” 运用 -
- UIAccelerometer (加速度检波器) 代码运用 -


首先运行以安装好的 xCode

选择: File->New Project.

从 "New Project" 窗口
 
选择 : iPhone OS ->Applications-> Utility Application
命名 : 我这里命名为 “PlumbBob”

 

(1)  在xCode右键点击 Frameworks ->Add->Existing Framework;在Frameworks文件夹下选择 QuartzCore.framework, 按Add



(2) 导入下面图片文件

下载下面图片,放入 PlumbBob 文件夹内并命名为下面名称

PlumbBob.png


在xCode下右键点击 PlumbBob->Add->Existing Files; 在 PlumbBob 文件夹内,选择下载好的图片,按 Add
 

(3)  在xCode打开 MainView.h 文件,加入下面代码

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController <UIAccelerometerDelegate> {
 UIImageView* plumbBobView;
}
- (void)rotatePlumbStringToDegree:(CGFloat)positionInDegrees;
@end

CGFloat DegreesToRadians(CGFloat degrees);
CGFloat RadiansToDegrees(CGFloat radians);

 
(4)  在xCode打开 MainView.m 文件,加入下面代码

#import "MainViewController.h"
#import "MainView.h"
#import <QuartzCore/QuartzCore.h>
// Constant for the number of times per second (Hertz) to sample acceleration.
#define kAccelerometerFrequency 40

@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 // 设置背景色为黑色
 self.view.backgroundColor = [UIColor blackColor];
 
 // 设置 "测锤" 图案
 UIImage* image = [UIImage imageNamed:@"PlumbBob.png"];
 plumbBobView = [[UIImageView alloc] initWithImage:image];
 
 // 移动锚点到底部节拍器区域中间
 plumbBobView.layer.anchorPoint = CGPointMake(0.5, 0.0);
 
 //确定锚点后设置帧,显示的将会在正确启动位置.
 plumbBobView.frame = CGRectMake(self.view.frame.size.width/2 - 20, 0, 40, 450);
 
 [self.view addSubview:plumbBobView];
 [plumbBobView release];
 
 // 配置和启动加速度检波器
 [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
 [[UIAccelerometer sharedAccelerometer] setDelegate:self];
 
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
 [plumbBobView release];
    [super dealloc];
}


#pragma mark -
#pragma mark === Accelerometer delegate ===
#pragma mark -

// UIAccelerometerDelegate method, 当设备加速时呼出.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
 [self rotatePlumbStringToDegree:-acceleration.x* 30];
}

#pragma mark -
#pragma mark === Swing the plumb and string ===
#pragma mark -

- (void)rotatePlumbStringToDegree:(CGFloat)positionInDegrees {
    [plumbBobView.layer removeAllAnimations];
 
    CATransform3D rotationTransform = CATransform3DIdentity;
    rotationTransform = CATransform3DRotate(rotationTransform, DegreesToRadians(positionInDegrees), 0.0, 0.0, 1.0);
    plumbBobView.layer.transform = rotationTransform;
}

@end

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};


最后在 xCode 选择 Build->Build and Go; Save All.

 

下载今天教程文件: PlumbBob.zip


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值