2014-10-02 67 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