实例iPhone编程入门教程-第十一天
2011-09-22 15:13
225 查看
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
今天来建立一个 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
相关文章推荐
- 实例iPhone编程入门教程-第六天
- 实例iPhone编程入门教程-第七天
- 实例iPhone编程入门教程-第八天
- 实例iPhone编程入门教程-第九天
- 实例iPhone编程入门教程-第十天
- 实例iPhone编程入门教程-第十二天
- 实例iPhone编程入门教程-第十三天
- 实例iPhone编程入门教程-第一天
- 实例iPhone编程入门教程-第二天
- 实例iPhone编程入门教程-第三天
- 实例iPhone编程入门教程-第四天
- 实例iPhone编程入门教程-第五天
- TensorFlow 教程 --新手入门--1.4 win10环境安装实例
- Maven入门(含实例教程)
- React 入门实例教程
- Antlr v4入门教程和实例
- php中的curl使用入门教程和常见用法实例
- hadoop入门教程及其实例代码
- React 入门实例教程
- Expression Blend实例中文教程(9) - 行为快速入门Behaviors