2014-10-02 69 views
-1

我正在編寫一個按鈕,當它被按下時發射彈丸。但是,我試圖禁用此按鈕,直到我希望它被使用。這是我的代碼,這是行不通的:按鈕不會禁用IOS目標C

#import "GameViewController.h" 

@interface GameViewController() 

@end 

@implementation GameViewController 

//What happens when shoot button is pressed 

-(IBAction)Shoot:(id)sender{ 

    Shoot.enabled = NO; 

    //Step 1: upload image Step 2: Load image into image view Step 3: set coordinates and size 
    UIImage * Bulletimage= [UIImage imageNamed:@"bullet.png"]; 
    UIImageView * Bullet = [[UIImageView alloc] initWithImage:Bulletimage]; 
    [Bullet setFrame:CGRectMake(130,430,35,35)]; 
    [self.view addSubview:Bullet]; 

    //Creating Animation 
    [UIView animateWithDuration:0.7 delay:0 options: UIViewAnimationOptionAllowAnimatedContent animations:^{ [Bullet setFrame: 
                               CGRectMake (130, 20, 35, 35)];} 
        completion:^(BOOL finished) {Bullet.hidden = YES;}]; 

} 


//what happens when ready button is pressed 
-(IBAction)Ready:(id)sender{ 

    //button disapears 
    Ready.hidden = YES; 

} 

-(IBAction)Ready2:(id)sender{ 

    //button disapears 
    Ready2.hidden = YES; 

    if (Ready.hidden= YES, Ready2.hidden = YES) 
    { 
     //declaring images for animation 
     Countdown.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageNamed:@"3.png"], 
            [UIImage imageNamed:@"2.png"], 
            [UIImage imageNamed:@"1.png"], 
            nil]; 

     [Countdown setAnimationRepeatCount:1]; 
     Countdown.AnimationDuration = 3; 
     [Countdown startAnimating]; 

     //end of loop 
     } 

} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

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

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 
+0

動畫是否發生? – 2014-10-02 23:09:24

+2

「不起作用」是什麼意思?這句話中幾乎沒有任何信息。 – Fogmeister 2014-10-02 23:30:59

回答

2

Shoot.enabled = NO;到您的-()viewDidLoad功能。現在你不告訴按鈕被禁用,直到按鈕被按下。另外,你應該養成在Objective-C中不用大寫變量和函數名中第一個字的第一個字母的習慣,例如你可以命名字符串myString或函數-(void)buttonPressed。您通常只會使用類名稱的第一個詞,如MyCustomObject

+0

我把Shoot.enabled = NO;進入視圖確實加載,但現在我有困難重寫,在射擊方法if語句..感謝您的幫助! – 2014-10-03 02:40:36

+0

我假設你有你的拍攝方法鏈接到你的拍攝按鈕?如果是這種情況,您將無法通過該方法重新啓用它。可能將'Shoot.enabled = YES;'添加到Ready方法的最後一個,以便在應用程序經過這些方法後啓用該按鈕。 – TyloBedo 2014-10-03 02:43:52

+0

解決了我的問題,謝謝你的幫助! – 2014-10-07 21:11:22