今天老師要我寫一個觸控螢幕 畫面就會顯示你觸控位置的座標的小程式
查了一下 剛好UITouch有提供locationInView 這個method 可以達到這個功能
只要在程式裡面加入此method
//當手觸摸螢幕時 就會自動跑到以下程式
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//取得觸摸點
UITouch *touch = [touches anyObject];
// 取得觸摸點的座標
CGPoint pt = [touch locationInView:self.view];
//顯示(x,y)
NSString *locationMessage = [[NSString alloc] initWithFormat:@"座標: x=%.0f, y=%.0f", pt.x, pt.y];
//顯示在Label上
display.text=locationMessage;
[locationMessage release];
}
Library:
UITouch
搜尋此網誌
2010年4月27日 星期二
2010年4月25日 星期日
iphone開發-G-Sensor 應用
最近花了一些時間寫一個 甩骰子遊戲 跟電腦比大小
透過此程式 會學到:
1.How to use UIImageView
2.圖片切換 形成的動畫
3.How to use Accelerometer
4.同個class裡 呼叫其他method
"test6ViewController.h"
//使用UIAccelerometerDelegate Protocol
@interface test6ViewController : UIViewController <UIAccelerometerDelegate>
{
UILabel *label;
UILabel *display;
UILabel *display2;
}
"test6ViewController.m"
//剛載入view時
- (void)viewDidLoad {
//取得加速計object給系統
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
//讓UIViewController取得代理
accelerometer.delegate = self;
//每1/60秒更新加速計的data
accelerometer.updateInterval = 1.0f/60.0f;
//呼叫同class's play method
[self play];
[super viewDidLoad];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
//shakeCount 計算搖晃次數
static NSInteger shakeCount=0;
//產生NSDate object
static NSDate *shakeStart;
NSDate *now=[[NSDate alloc] init];
//加兩秒到當前的時間
NSDate *checkDate=[[NSDate alloc] initWithTimeInterval:2.0f sinceDate:shakeStart];
//如果now大於checkdate 也就是超過兩秒
if ([now compare:checkDate]==NSOrderedDescending||shakeStart==nil){
//重新計算搖晃次數
shakeCount=0;
[shakeStart release];
shakeStart=[[NSDate alloc] init];
}
[now release];
[checkDate release];
//三軸搖晃的G力超過2就將搖晃次數加1 ps:fabsf:絕對值
if (fabsf(acceleration.x)>2.0 || fabsf(acceleration.y)>2.0|| fabsf(acceleration.z)>2.0){
shakeCount++;
//如果兩秒內搖晃次數大於4
if (shakeCount>4){
shakeCount=0;
[shakeStart release];
shakeStart=[[NSDate alloc] init];
//呼叫compare method
[self compare];
}
}
}
-(int)getRandomNumber:(int)from to:(int)to {
return (int)from + arc4random() % (to-from+1);
}
-(void)compare
{
//產生1~6隨機變數
int randomNumber = [self getRandomNumber:1 to:6];
//將圖片放到(x,y,w,h)=(130,150)的位置 原點為螢幕上左上角 w,h 為圖片的寬和高
CGRect myImageRect = CGRectMake(130.0f, 150.0f, 64.0f, 71.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
switch (randomNumber) {
case 1:
//將one.png設定為 要顯示的圖片
[myImage setImage:[UIImage imageNamed:@"one.png"]];
break;
case 2:
[myImage setImage:[UIImage imageNamed:@"two.png"]];
break;
case 3:
[myImage setImage:[UIImage imageNamed:@"three.png"]];
break;
case 4:
[myImage setImage:[UIImage imageNamed:@"four.png"]];
break;
case 5:
[myImage setImage:[UIImage imageNamed:@"five.png"]];
break;
case 6:
[myImage setImage:[UIImage imageNamed:@"six.png"]];
break;
default:
break;
}
//處理電腦的骰子點數
int randomNumber2=[self getRandomNumber:1 to:6];
NSString* Text = [[NSString alloc] initWithFormat:@"%d", randomNumber2];
NSString* Text1=@"電腦骰出";
NSString* ConText = [NSString stringWithFormat:@"%@%@", Text1,Text];
display2.text=ConText;
//你骰出的點數比電腦大 就勝利
if (randomNumber>randomNumber2) {
display.text=@"You Win!";
}
//平手
else if(randomNumber==randomNumber2)
{
display.text=@"drawn game";
}
//其餘就是你輸
else {
display.text=@"Sorry,You Lose!";
}
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release];
}
//開始搖晃前 產生骰子動畫
-(void)play
{
CGRect myImageRect = CGRectMake(130.0f, 150.0f, 64.0f, 71.0f);
//將6個點數的骰子圖片 分別放入array
UIImageView *fishAni = [[UIImageView alloc] initWithFrame:myImageRect];
fishAni.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:@"one.png"],[UIImage imageNamed:@"two.png"],[UIImage imageNamed:@"three.png"],[UIImage imageNamed:@"four.png"],[UIImage imageNamed:@"five.png"],[UIImage imageNamed:@"six.png"],nil ];
//設定動畫播放時間
fishAni.animationDuration=6.0;
//設定重複播放次數,0 為不斷重複
fishAni.animationRepeatCount=0;
//開始撥放動畫
[fishAni startAnimating];
//將ImageView 加入到目前view的subview
[self.view addSubview:fishAni];
}
@end
Library:
UIAccelerometer
NSDate
UIImageView
透過此程式 會學到:
1.How to use UIImageView
2.圖片切換 形成的動畫
3.How to use Accelerometer
4.同個class裡 呼叫其他method
"test6ViewController.h"
//使用UIAccelerometerDelegate Protocol
@interface test6ViewController : UIViewController <UIAccelerometerDelegate>
{
UILabel *label;
UILabel *display;
UILabel *display2;
}
"test6ViewController.m"
//剛載入view時
- (void)viewDidLoad {
//取得加速計object給系統
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
//讓UIViewController取得代理
accelerometer.delegate = self;
//每1/60秒更新加速計的data
accelerometer.updateInterval = 1.0f/60.0f;
//呼叫同class's play method
[self play];
[super viewDidLoad];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
//shakeCount 計算搖晃次數
static NSInteger shakeCount=0;
//產生NSDate object
static NSDate *shakeStart;
NSDate *now=[[NSDate alloc] init];
//加兩秒到當前的時間
NSDate *checkDate=[[NSDate alloc] initWithTimeInterval:2.0f sinceDate:shakeStart];
//如果now大於checkdate 也就是超過兩秒
if ([now compare:checkDate]==NSOrderedDescending||shakeStart==nil){
//重新計算搖晃次數
shakeCount=0;
[shakeStart release];
shakeStart=[[NSDate alloc] init];
}
[now release];
[checkDate release];
//三軸搖晃的G力超過2就將搖晃次數加1 ps:fabsf:絕對值
if (fabsf(acceleration.x)>2.0 || fabsf(acceleration.y)>2.0|| fabsf(acceleration.z)>2.0){
shakeCount++;
//如果兩秒內搖晃次數大於4
if (shakeCount>4){
shakeCount=0;
[shakeStart release];
shakeStart=[[NSDate alloc] init];
//呼叫compare method
[self compare];
}
}
}
-(int)getRandomNumber:(int)from to:(int)to {
return (int)from + arc4random() % (to-from+1);
}
-(void)compare
{
//產生1~6隨機變數
int randomNumber = [self getRandomNumber:1 to:6];
//將圖片放到(x,y,w,h)=(130,150)的位置 原點為螢幕上左上角 w,h 為圖片的寬和高
CGRect myImageRect = CGRectMake(130.0f, 150.0f, 64.0f, 71.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
switch (randomNumber) {
case 1:
//將one.png設定為 要顯示的圖片
[myImage setImage:[UIImage imageNamed:@"one.png"]];
break;
case 2:
[myImage setImage:[UIImage imageNamed:@"two.png"]];
break;
case 3:
[myImage setImage:[UIImage imageNamed:@"three.png"]];
break;
case 4:
[myImage setImage:[UIImage imageNamed:@"four.png"]];
break;
case 5:
[myImage setImage:[UIImage imageNamed:@"five.png"]];
break;
case 6:
[myImage setImage:[UIImage imageNamed:@"six.png"]];
break;
default:
break;
}
//處理電腦的骰子點數
int randomNumber2=[self getRandomNumber:1 to:6];
NSString* Text = [[NSString alloc] initWithFormat:@"%d", randomNumber2];
NSString* Text1=@"電腦骰出";
NSString* ConText = [NSString stringWithFormat:@"%@%@", Text1,Text];
display2.text=ConText;
//你骰出的點數比電腦大 就勝利
if (randomNumber>randomNumber2) {
display.text=@"You Win!";
}
//平手
else if(randomNumber==randomNumber2)
{
display.text=@"drawn game";
}
//其餘就是你輸
else {
display.text=@"Sorry,You Lose!";
}
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release];
}
//開始搖晃前 產生骰子動畫
-(void)play
{
CGRect myImageRect = CGRectMake(130.0f, 150.0f, 64.0f, 71.0f);
//將6個點數的骰子圖片 分別放入array
UIImageView *fishAni = [[UIImageView alloc] initWithFrame:myImageRect];
fishAni.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:@"one.png"],[UIImage imageNamed:@"two.png"],[UIImage imageNamed:@"three.png"],[UIImage imageNamed:@"four.png"],[UIImage imageNamed:@"five.png"],[UIImage imageNamed:@"six.png"],nil ];
//設定動畫播放時間
fishAni.animationDuration=6.0;
//設定重複播放次數,0 為不斷重複
fishAni.animationRepeatCount=0;
//開始撥放動畫
[fishAni startAnimating];
//將ImageView 加入到目前view的subview
[self.view addSubview:fishAni];
}
@end
Library:
UIAccelerometer
NSDate
2010年4月19日 星期一
iphone開發-字串連接
在java裡面我們常常用到"+"來連接兩個字串 但在objective c似乎不是這樣用
以下用一個例子解釋
float weight=benefit2/7700;
//將float轉成string
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//將三個字串連結在一起
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
以下用一個例子解釋
float weight=benefit2/7700;
//將float轉成string
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//將三個字串連結在一起
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
iphone開發-UISlider
這幾天寫了一個可以計算你一天吃多少熱量之後 一個月後會瘦幾公斤 或變胖
有用到UISlider 剛好拿來記錄一下
"MyViewController.h"
@interface MyViewController : UIViewController {
UITextField *textfield;
UITextField *textfield2;
UITextField *textfield3;
UITextField *textfield4;
UILabel *display;
UILabel *display2;
//產生一個UISlider object
UISlider *slider;
NSString *string;
NSString *string2;
NSString *string3;
NSString *string4;
}
@property(nonatomic,retain)IBOutlet UITextField *textfield;
@property(nonatomic,retain)IBOutlet UITextField *textfield2;
@property(nonatomic,retain)IBOutlet UITextField *textfield3;
@property(nonatomic,retain)IBOutlet UITextField *textfield4;
//告訴IB有一個UISlider
@property(nonatomic,retain)IBOutlet UISlider *slider;
@property(nonatomic,retain)IBOutlet UILabel *display;
@property(nonatomic,retain)IBOutlet UILabel *display2;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,copy)NSString *string2;
@property(nonatomic,copy)NSString *string3;
@property(nonatomic,copy)NSString *string4;
-(IBAction)calculate:(id)sender;
-(IBAction)reset:(id)sender;
//對應UISlider的event
-(IBAction)sliderChanged:(id)sender;
-(IBAction)female:(id)sender;
-(IBAction)male:(id)sender;
@end
"MyViewController.m"
@implementation MyViewController
@synthesize textfield;
@synthesize textfield2;
@synthesize textfield3;
@synthesize textfield4;
@synthesize slider;
@synthesize display;
@synthesize display2;
@synthesize string;
@synthesize string2;
@synthesize string3;
@synthesize string4;
float a;
int characterselect;
//移動slider的event
-(IBAction)sliderChanged:(id)sender
{
NSString *newText;
UISlider *slider=(UISlider *)sender;
//先在IB裡面設定範圍 此處我設定為0~4 user移動slider之後 就會是0~4其中一個
int progressAsInt=(int)(slider.value+0.5f);
switch (progressAsInt) {
case 0:
{
newText=@"很少運動或完全沒運動";
a=1.2;
break;
}
case 1:
{
newText=@"每周運動1-3次";
a=1.375;
break;
}
case 2:
{
newText=@"每周運動3-5次";
a=1.55;
break;
}
case 3:
{
newText=@"每周運動6-7次";
a=1.725;
break;
}
case 4:
{
newText=@"每天非常重度的運動或體力勞動者";
a=1.9;
break;
}
default:
newText=@"exception error";
break;
}
//在diplay2這個label顯示字串
display2.text=newText;
[newText release];
}
-(IBAction)calculate:(id)sender
{
self.string=textfield.text;
self.string2=textfield2.text;
self.string3=textfield3.text;
self.string4=textfield4.text;
//將字串轉成int
int stringint=[string intValue];
int stringint2=[string2 intValue];
int stringint3=[string3 intValue];
int stringint4=[string4 intValue];
if (characterselect==0) {
//基礎代謝率公式
float kg=9.6*stringint2;
float cm=1.8*stringint;
float age=4.7*stringint3;
float benefit=655+kg+cm-age;
float result=a*benefit;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
if (characterselect==1) {
float kg=13.8*stringint2;
float cm=5.0*stringint;
float age=6.8*stringint3;
float benefit=66+kg+cm-age;
float result=a*benefit;
//浮點數轉成字串
//NSString* myNewString = [[NSString alloc] initWithFormat:@"%f", a];
//display.text=myNewString;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
}
//按鈕按下去會觸發的event
-(IBAction)female:(id)sender
{
characterselect=0;
}
-(IBAction)male:(id)sender
{
characterselect=1;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
[textfield resignFirstResponder];
[textfield2 resignFirstResponder];
[textfield3 resignFirstResponder];
[textfield4 resignFirstResponder];
return YES;
}
-(IBAction)reset:(id)sender
{
textfield.text=@"";
textfield2.text=@"";
textfield3.text=@"";
textfield4.text=@"";
display.text=@"";
display2.text=@"";
}
- (void)dealloc {
[textfield release];
[textfield2 release];
[textfield3 release];
[string release];
[string2 release];
[string3 release];
[display release];
[slider release];
[super dealloc];
}
@end
有用到UISlider 剛好拿來記錄一下
"MyViewController.h"
@interface MyViewController : UIViewController {
UITextField *textfield;
UITextField *textfield2;
UITextField *textfield3;
UITextField *textfield4;
UILabel *display;
UILabel *display2;
//產生一個UISlider object
UISlider *slider;
NSString *string;
NSString *string2;
NSString *string3;
NSString *string4;
}
@property(nonatomic,retain)IBOutlet UITextField *textfield;
@property(nonatomic,retain)IBOutlet UITextField *textfield2;
@property(nonatomic,retain)IBOutlet UITextField *textfield3;
@property(nonatomic,retain)IBOutlet UITextField *textfield4;
//告訴IB有一個UISlider
@property(nonatomic,retain)IBOutlet UISlider *slider;
@property(nonatomic,retain)IBOutlet UILabel *display;
@property(nonatomic,retain)IBOutlet UILabel *display2;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,copy)NSString *string2;
@property(nonatomic,copy)NSString *string3;
@property(nonatomic,copy)NSString *string4;
-(IBAction)calculate:(id)sender;
-(IBAction)reset:(id)sender;
//對應UISlider的event
-(IBAction)sliderChanged:(id)sender;
-(IBAction)female:(id)sender;
-(IBAction)male:(id)sender;
@end
"MyViewController.m"
@implementation MyViewController
@synthesize textfield;
@synthesize textfield2;
@synthesize textfield3;
@synthesize textfield4;
@synthesize slider;
@synthesize display;
@synthesize display2;
@synthesize string;
@synthesize string2;
@synthesize string3;
@synthesize string4;
float a;
int characterselect;
//移動slider的event
-(IBAction)sliderChanged:(id)sender
{
NSString *newText;
UISlider *slider=(UISlider *)sender;
//先在IB裡面設定範圍 此處我設定為0~4 user移動slider之後 就會是0~4其中一個
int progressAsInt=(int)(slider.value+0.5f);
switch (progressAsInt) {
case 0:
{
newText=@"很少運動或完全沒運動";
a=1.2;
break;
}
case 1:
{
newText=@"每周運動1-3次";
a=1.375;
break;
}
case 2:
{
newText=@"每周運動3-5次";
a=1.55;
break;
}
case 3:
{
newText=@"每周運動6-7次";
a=1.725;
break;
}
case 4:
{
newText=@"每天非常重度的運動或體力勞動者";
a=1.9;
break;
}
default:
newText=@"exception error";
break;
}
//在diplay2這個label顯示字串
display2.text=newText;
[newText release];
}
-(IBAction)calculate:(id)sender
{
self.string=textfield.text;
self.string2=textfield2.text;
self.string3=textfield3.text;
self.string4=textfield4.text;
//將字串轉成int
int stringint=[string intValue];
int stringint2=[string2 intValue];
int stringint3=[string3 intValue];
int stringint4=[string4 intValue];
if (characterselect==0) {
//基礎代謝率公式
float kg=9.6*stringint2;
float cm=1.8*stringint;
float age=4.7*stringint3;
float benefit=655+kg+cm-age;
float result=a*benefit;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
if (characterselect==1) {
float kg=13.8*stringint2;
float cm=5.0*stringint;
float age=6.8*stringint3;
float benefit=66+kg+cm-age;
float result=a*benefit;
//浮點數轉成字串
//NSString* myNewString = [[NSString alloc] initWithFormat:@"%f", a];
//display.text=myNewString;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
}
//按鈕按下去會觸發的event
-(IBAction)female:(id)sender
{
characterselect=0;
}
-(IBAction)male:(id)sender
{
characterselect=1;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
[textfield resignFirstResponder];
[textfield2 resignFirstResponder];
[textfield3 resignFirstResponder];
[textfield4 resignFirstResponder];
return YES;
}
-(IBAction)reset:(id)sender
{
textfield.text=@"";
textfield2.text=@"";
textfield3.text=@"";
textfield4.text=@"";
display.text=@"";
display2.text=@"";
}
- (void)dealloc {
[textfield release];
[textfield2 release];
[textfield3 release];
[string release];
[string2 release];
[string3 release];
[display release];
[slider release];
[super dealloc];
}
@end
2010年4月16日 星期五
iphone開發-AVAudioPlayer
前幾天 我寫了一個陽春版的吹風機程式(意思就是打開按鈕 會有吹風機的聲音)是一個無裡頭的程式XD 這次的程式其實是延續上篇的UISwitch 加入播放音樂的功能 而iphone裡面 有一個AVAudioplayer可以直接用來播放音樂用 以下就用我前幾天寫的程式來說明
"AudioPlayerViewController.h"
#import <UIKit/UIKit.h>
//記得先import 以下這個 才能使用AVAudioplayer
#import <AVFoundation/AVFoundation.h>
@interface AudioPlayerViewController : UIViewController {
//產生AVAudioPlayer object
AVAudioPlayer *audioPlayer;
//產生UISwitch object
UISwitch *switched;
}
@property(nonatomic,retain)IBOutlet UISwitch *switched;
-(IBAction)switchange:(id)sender;
@end
"AudioPlayerViewController.m"
#import "AudioPlayerViewController.h"
@implementation AudioPlayerViewController
@synthesize switched;
-(IBAction)switchange:(id)sender
{
UISwitch *whichswitch=(UISwitch *)sender;
//當switch的按鈕切換到ON
if (whichswitch.isOn==YES) {
//讓audioPlayer可以重頭播放
audioPlayer.currentTime = 0;
//開始播放
[audioPlayer play];
}
else
//停止播放 但不會讓音樂重頭播放
[audioPlayer stop];
}
//程式剛起始時 會運作的部分
- (void)viewDidLoad {
[super viewDidLoad];
//指定音樂檔名和格式
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
}
- (void)dealloc {
//記得釋放記憶體
[audioPlayer release];
[super dealloc];
}
@end
最後記得 點xcode旁邊有個Resources 右鍵 add->existing files
選擇你想播放的音樂 記得將"copy items into destination group...."打勾 按add就可以了
另外還有一些其他method 像是
// 0.0 - no volume; 1.0 full volume
"AudioPlayerViewController.h"
@interface AudioPlayerViewController : UIViewController {
//產生AVAudioPlayer object
AVAudioPlayer *audioPlayer;
//產生UISwitch object
UISwitch *switched;
}
@property(nonatomic,retain)IBOutlet UISwitch *switched;
-(IBAction)switchange:(id)sender;
@end
"AudioPlayerViewController.m"
#import "AudioPlayerViewController.h"
@implementation AudioPlayerViewController
@synthesize switched;
-(IBAction)switchange:(id)sender
{
UISwitch *whichswitch=(UISwitch *)sender;
//當switch的按鈕切換到ON
if (whichswitch.isOn==YES) {
//讓audioPlayer可以重頭播放
audioPlayer.currentTime = 0;
//開始播放
[audioPlayer play];
}
else
//停止播放 但不會讓音樂重頭播放
[audioPlayer stop];
}
//程式剛起始時 會運作的部分
- (void)viewDidLoad {
[super viewDidLoad];
//指定音樂檔名和格式
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
}
- (void)dealloc {
//記得釋放記憶體
[audioPlayer release];
[super dealloc];
}
@end
最後記得 點xcode旁邊有個Resources 右鍵 add->existing files
選擇你想播放的音樂 記得將"copy items into destination group...."打勾 按add就可以了
另外還有一些其他method 像是
// 0.0 - no volume; 1.0 full volume
audioPlayer.volume = 0.5;
//暫停
[audioPlayer pause];
2010年4月11日 星期日
iphone開發-UISwitch
前幾天看到老師給我看的刮鬍刀程式 我也想來寫一個無厘頭的程式 因此我想寫一個吹風機程式XD 在iphone裡面 有一個像開關的按鈕 稱作UISwitch 在這邊紀錄一下 UISwitch 怎麼使用 我用我前幾天寫的程式 來教學
首先在.h 檔 做以下宣告
"test4AppDelegate.h"
@interface test4AppDelegate : NSObject {
UIWindow *window;
UILabel *display;
//產生一個 switch object
UISwitch *switchchange;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic,retain)IBOutlet UISwitch *switchchange;
@property(nonatomic,retain)IBOutlet UILabel *display;
-(IBAction)switchchanged:(id)sender;
@end
" test4AppDelegate.m"
@implementation test4AppDelegate
@synthesize window;
@synthesize switchchange;
@synthesize display;
//這邊控制按鈕的動作
-(IBAction)switchchanged:(id)sender
{
UISwitch *whichswitch=(UISwitch *)sender;
//當按鈕移動到ON 就印出test的字串
if (whichswitch.isOn==YES) {
display.text=@"test";
}
//當移動到Off 就印出test2
else
display.text=@"test2";
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
首先在.h 檔 做以下宣告
UIWindow *window;
UILabel *display;
//產生一個 switch object
UISwitch *switchchange;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic,retain)IBOutlet UISwitch *switchchange;
@property(nonatomic,retain)IBOutlet UILabel *display;
-(IBAction)switchchanged:(id)sender;
@end
"
@synthesize window;
@synthesize switchchange;
@synthesize display;
//這邊控制按鈕的動作
-(IBAction)switchchanged:(id)sender
{
UISwitch *whichswitch=(UISwitch *)sender;
//當按鈕移動到ON 就印出test的字串
if (whichswitch.isOn==YES) {
display.text=@"test";
}
//當移動到Off 就印出test2
else
display.text=@"test2";
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
2010年4月9日 星期五
iphone開發-型別轉換
// 將字串轉換成int
NSString *intString = @"1";
int value = [intString intValue];
// 將字串轉換成float
NSString *floatString = @"10.00";
float value = [floatString floatValue];
// 將字串轉換成double
NSString *doubleString = @"10.000000";
double value = [doubleString doubleValue];
//將float轉換成字串
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
//釋放記憶體
[Text release];
//將int轉換成字串
int weight=5;
NSString* Text = [[NSString alloc] initWithFormat:@"%d", weight];
//釋放記憶體
[Text release];
iphone開發-object與method寫法
在objective C中 我們常可以看到[receiver message]的東西 以下將介紹這個
譬如說 我們產生一個label object
UILabel *label
我們想在label中顯示一段字串 我們可以這麼寫
[label setText:@"This is a test"];
@開頭表示是一串字串 接著用雙引號括號起來
其實它也可以寫成像java 用dot的寫法 這兩個是一樣的
label.text=@"This is a test";
也就是object.method
譬如說 我們產生一個label object
UILabel *label
我們想在label中顯示一段字串 我們可以這麼寫
[label setText:@"This is a test"];
@開頭表示是一串字串 接著用雙引號括號起來
其實它也可以寫成像java 用dot的寫法 這兩個是一樣的
label.text=@"This is a test";
也就是object.method
2010年4月8日 星期四
iphone開發-dismiss the keyboard
今天花了蠻多時間在解決這個問題 在iphone中輸入文字or數字 他會自動彈出虛擬鍵盤 但是輸入完成後 按下return or done(要看你在UI中 設定成什麼) 並不會自動消失 因此需要自行加入某樣東西才行 以下用我今天寫的東西來介紹
(MyViewController.h)
//UIVewController 使用UITextFieldDelegate這個protocol 這樣才能用到使鍵盤消失的method
@interface MyViewController : UIViewController <UITextFieldDelegate> {
//產生一個可以在上面輸入文字的object
UITextField *textField;
UITextField *textField2;
//產生一個label object
UILabel *label;
NSString *string;
NSString *string2;
}
//用IBOulet作為UI的識別
@property (nonatomic,retain)IBOutlet UITextField *textField;
@property (nonatomic,retain)IBOutlet UITextField *textField2;
@property (nonatomic,retain)IBOutlet UILabel *label;
@property (nonatomic,copy)NSString *string;
@property (nonatomic,copy)NSString *string2;
//按鈕宣告
-(IBAction)calculate:(id)sender;
@end
(MyViewController.m)
#import "MyViewController.h"
@implementation MyViewController
@synthesize textField;
@synthesize textField2;
@synthesize label;
@synthesize string;
@synthesize string2;
//按鈕按下要執行的事件
-(IBAction)calculate:(id)sender
{
//將user輸入的文字 放到string中
self.string=textField.text;
self.string2=textField2.text;
float bmi;
float hight;
//string 轉換成 float (ps:這邊我會另外再簡介)
float stringFloat = [string floatValue];
hight=stringFloat/100;
float stringFloat2 = [string2 floatValue];
float hight2=hight*hight;
bmi=stringFloat2/hight2;
if (bmi<18.5) text="@">=18.5&&bmi<24) text="@">=24&&bmi<27) text="@">=27)
{
label.text=@"太胖了..沒救了";
}
}
//這邊就是當user按下虛擬鍵盤的return or done之後 會呼叫此function 接著鍵盤會自動消失
-(Bool)textFieldShouldReturn:(UITextField *)TextField
{
//因為我有兩個textField (也就是可以讓使用者輸入文字的地方)
[textField resignFirstResponder];
[textField2 resignFirstResponder];
return YES;
}
之後就是記得在UI設計那邊 要將兩個textField 連到file's owner 讓file's owner當作代理程式
(MyViewController.h)
//UIVewController 使用UITextFieldDelegate這個protocol 這樣才能用到使鍵盤消失的method
@interface MyViewController : UIViewController <UITextFieldDelegate> {
//產生一個可以在上面輸入文字的object
UITextField *textField;
UITextField *textField2;
//產生一個label object
UILabel *label;
NSString *string;
NSString *string2;
}
//用IBOulet作為UI的識別
@property (nonatomic,retain)IBOutlet UITextField *textField;
@property (nonatomic,retain)IBOutlet UITextField *textField2;
@property (nonatomic,retain)IBOutlet UILabel *label;
@property (nonatomic,copy)NSString *string;
@property (nonatomic,copy)NSString *string2;
//按鈕宣告
-(IBAction)calculate:(id)sender;
@end
(MyViewController.m)
#import "MyViewController.h"
@implementation MyViewController
@synthesize textField;
@synthesize textField2;
@synthesize label;
@synthesize string;
@synthesize string2;
//按鈕按下要執行的事件
-(IBAction)calculate:(id)sender
{
//將user輸入的文字 放到string中
self.string=textField.text;
self.string2=textField2.text;
float bmi;
float hight;
//string 轉換成 float (ps:這邊我會另外再簡介)
float stringFloat = [string floatValue];
hight=stringFloat/100;
float stringFloat2 = [string2 floatValue];
float hight2=hight*hight;
bmi=stringFloat2/hight2;
if (bmi<18.5) text="@">=18.5&&bmi<24) text="@">=24&&bmi<27) text="@">=27)
{
label.text=@"太胖了..沒救了";
}
}
//這邊就是當user按下虛擬鍵盤的return or done之後 會呼叫此function 接著鍵盤會自動消失
-(Bool)textFieldShouldReturn:(UITextField *)TextField
{
//因為我有兩個textField (也就是可以讓使用者輸入文字的地方)
[textField resignFirstResponder];
[textField2 resignFirstResponder];
return YES;
}
之後就是記得在UI設計那邊 要將兩個textField 連到file's owner 讓file's owner當作代理程式
2010年4月6日 星期二
iphone開發-declare and define(2)
接續上篇所介紹的 今天剛好用到隨機函數 拿這個例子來當作舉例
首先在.h的檔案中 加入下列這行來宣告這個method
-(int)getRandomNumber:(int)from to:(int)to {
//這邊的用法 和C裡面的srand()一樣
return (int)from + arc4random() % (to-from+1);
}
之後在.m裡面 可以加入下列這行來呼叫這個method
//隨機產生1~10
首先在.h的檔案中 加入下列這行來宣告這個method
-(int)getRandomNumber:(int)from to:(int)to;
接著在.m的檔案中 加入下列這行來定義,也就是告訴他要做些什麼事-(int)getRandomNumber:(int)from to:(int)to {
//這邊的用法 和C裡面的srand()一樣
return (int)from + arc4random() % (to-from+1);
}
之後在.m裡面 可以加入下列這行來呼叫這個method
//隨機產生1~10
int
randomNumber = [self getRandomNumber:1 to:10];
2010年4月1日 星期四
iphone開發-declare and define篇
今天終於把objective c++ K過一遍了 將一些心得寫上來 順便記住一些重點
在開發時 當我新建立一個專案時 我們會看到 XXX.h 和XXX.m 兩個檔案
事實上 開發iphone時 我們會發現 他們將不同功能 不同作用的程式碼 分出來 寫到不同的檔案上
我們先來介紹一下 .h 和.m 主要是在做什麼
"XXX.h" 其實就像是C++ header一樣 在裡面宣告類別以及變數 而objective c++稱作
interface
"XXX.m" 在objective c++裡面稱作implement的部份 也就是定義 該類別要作什麼動作
範例如下:
EX: XXX.h
在開發時 當我新建立一個專案時 我們會看到 XXX.h 和XXX.m 兩個檔案
事實上 開發iphone時 我們會發現 他們將不同功能 不同作用的程式碼 分出來 寫到不同的檔案上
我們先來介紹一下 .h 和.m 主要是在做什麼
"XXX.h" 其實就像是C++ header一樣 在裡面宣告類別以及變數 而objective c++稱作
interface
"XXX.m" 在objective c++裡面稱作implement的部份 也就是定義 該類別要作什麼動作
範例如下:
EX: XXX.h
#import
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
EX:XXX.m
#import "Fraction.h"
#import
@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end
訂閱:
文章 (Atom)