0

押されたときにサイズと色を変更する必要がある 2 つの UIButtons を持つアプリに取り組んでいます。私のコードは次のとおりです。

    UIButton *Answer1Button = [UIButton buttonWithType:UIButtonTypeCustom];

    UIButton *Answer2Button = [UIButton buttonWithType:UIButtonTypeCustom];

    [Answer1Button addTarget:self action:@selector(Answer1Action) forControlEvents:UIControlEventTouchUpInside];


    [Answer2Button addTarget:self action:@selector(Answer2Action) forControlEvents:UIControlEventTouchUpInside];

    [Answer1Button setBackgroundColor:[UIColor redColor]];
    [Answer2Button setBackgroundColor:[UIColor yellowColor]];
    Answer1Button.frame = CGRectMake(0, 0, 320, 45);
    Answer2Button.frame = CGRectMake(0, 50, 320, 45);

そして、私が作成した関数:

-(void)Answer1Action{


[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];

self.alpha = 0.5;

[UIButton commitAnimations];    
}

私が今遭遇する問題は、UIButton を押すと関数が呼び出されますが、self.alpha はボタンが配置されている UIView 全体に影響します。

私はまだ Objective-C の新人なので、忘れていた簡単なことだと思います。

4

3 に答える 3

3
[Answer1Button setBackgroundColor:[UIColor greenColor]];
[Answer1Button setAlpha:0.5];

//Include QuartzCore framework and import it to your viewController.h,and write the below line along with the above code to change the size and to do many animations.

Answer1Button.layer.affineTransform=CGAffineTransformScale(Answer1Button.transform, 1.2, 1.2);
于 2013-11-13T09:34:25.663 に答える
1

あなたは設定していますself.alpha = 0.5;、ここで self はあなたのボタンではありません。ボタンを設定する必要があります:

Answer1Button.backgroundColor = [UIColor greenColor];

Answer1Button.alpha = 0.5;

しかし、これはベスト プラクティスではありません。ボタンをパラメーターとしてハンドラー メソッドに送信できます (ObjC では変数名とメソッド名を大文字で始めないでください)。

[answer1Button addTarget:self action:@selector(answer1Action:) forControlEvents:UIControlEventTouchUpInside];

そして、送信者ボタンをハンドラー メソッドのパラメーターとして使用します。

-(void)answer1Action:(id)sender{
UIButton* myButton = (UIButton*)sender;

[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
myButton.backgroundColor = [UIColor greenColor];

myButton.alpha = 0.5;

[UIButton commitAnimations];    
}
于 2013-11-13T09:16:05.233 に答える