透過此程式 會學到:
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
沒有留言:
張貼留言