2012-12-23 21 views
0

我創建了抽樣點擊計數應用程序。但我不知道如何計算一圈。例如:當命中數爲100時,圈數爲1.當命中數爲200圈時爲2.我使用以下代碼。謝謝。我是xcode初學者。Tap Count&Lap

下面的代碼是ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

-(IBAction)plus { 
    counter=counter +1; 
    count.text = [NSString stringWithFormat:@"%i", counter]; 
} 

-(IBAction)minus { 
    counter=counter -1; 
    count.text = [NSString stringWithFormat:@"%i", counter]; 
} 

-(IBAction)zero { 
    counter=0; 
    count.text = [NSString stringWithFormat:@"%i", counter]; 
} 
- (void)viewDidLoad 
{ 
    counter=0; 
    [email protected]"0"; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

下面的代碼是ViewController.h #進口

int counter; 

@interface ViewController : UIViewController { 
    IBOutlet UILabel *count; 
    IBOutlet UILabel *lap; 
} 

-(IBAction)plus; 
-(IBAction)minus; 
-(IBAction)zero; 
@end 
+1

不知道,這個問題是什麼,但看起來像你有你在你的代碼需要的每一件事情。你能更具體地說明你想做什麼嗎?是上面的代碼工作,還是你想要做一些額外的事情,你不清楚。 – Srikanth

+0

感謝您的評論。是的,這個代碼是可行的,但我需要一些額外的。例如:當命中數爲100時,圈數爲1.當命中數爲200圈時爲2.我使用以下代碼。謝謝。我是xcode初學者。 – ArnoHlaMoe

+2

不確定您的圈數值是否可以根據計數器的值而改變,但無論是明智的,您只需在counter = counter + 1,counter = counter-1和counter = 0之後執行以下操作「一圈。text = [NSString stringWithFormat:%i「,counter/100];」這樣做的整數除法,並會給你正確的答案 – Srikanth

回答

0

這裏是我做到了這一點。現在請注意,我沒有使用您的確切設置,但這應該起作用,或者至少給您正確的想法。這在理論上應該更難,因爲你真的想要檢查100圈的無限數量,所以我們需要一些邏輯檢查,除非我們使用int。詮釋是不精確的,所以如果我們說50/100,它仍然會返回0.然而,100/100 = 1,200/100 = 2,275/100仍然等於2.這將完成你想要的東西。

下面是一個簡單的例子,只有一個增加數量的按鈕,我使用全局變量來計算圈數和點擊次數。

在.H

#import <UIKit/UIKit.h> 

@interface TapCounterViewController : UIViewController 
- (IBAction)buttonTouched:(id)sender; 

@end 

在.M

#import "TapCounterViewController.h" 

@interface TapCounterViewController() 

@end 

@implementation TapCounterViewController 

int lapNumber = 0; 
int buttonTaps = 0; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)buttonTouched:(id)sender { 
    buttonTaps = buttonTaps + 1; 
    NSLog(@"Current Button Taps = %i", buttonTaps); 

    //Check for Lap 
    [self checkForLap]; 
} 


-(void)checkForLap { 
    //We must divide by 100 to check for the number if laps. 
    int laps = buttonTaps/100; 
    NSLog(@"Number of laps = %i", laps); 
    lapNumber = laps; 
} 
@end 

這可以很容易地適應遞減,重置你不管。只要你改變按鈕點擊,執行checkForLap。

祝你好運,如果你需要更多的信息讓我知道!

EDIT

對於振動:

有一些帶一個參數kSystemSoundID_Vibrate兩個看似類似的功能:

1)AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 2)AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

兩者的功能振動蘋果手機。但是,當您在不支持振動的設備上使用第一個功能時,會發出嘟嘟聲。另一方面,第二個功能在不支持的設備上不做任何事情。因此,如果您要持續振動設備,請按常識說,使用功能2.請參閱「iPhone教程:更好的方法來檢查iOS設備的功能」一文。

頭文件導入:

#import <AudioToolbox/AudioServices.h> 
+0

注意:如果你想要更精確的圈數,可以使用float而不是int。 – Josiah

+0

我還有一個問題。我怎樣才能振動每一圈? – ArnoHlaMoe

+1

看到我的新編輯。 – Josiah