Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton目标自身动画_Ios_Objective C_Uibutton - Fatal编程技术网

Ios UIButton目标自身动画

Ios UIButton目标自身动画,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我正在开发一个有两个UIButton的应用程序,当按下它们时,它们必须改变大小和颜色。我的代码是: UIButton *Answer1Button = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton *Answer2Button = [UIButton buttonWithType:UIButtonTypeCustom]; [Answer1Button addTarget:self action:@sele

我正在开发一个有两个UIButton的应用程序,当按下它们时,它们必须改变大小和颜色。我的代码是:

    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的新手,所以我想这可能是我忘记的简单的事情。

您正在设置
self.alpha=0.5,这里的self不是您的按钮。您应该设置按钮:

Answer1Button.backgroundColor = [UIColor greenColor];

Answer1Button.alpha = 0.5;
但这不是最佳做法。您可以将按钮作为参数发送到处理程序方法: (变量和方法名称在ObjC中不应以大写字母开头。)

并将sender按钮用作处理程序方法中的参数:

-(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];    
}

您正在设置
self.alpha=0.5,这里的self不是您的按钮。您应该设置按钮:

Answer1Button.backgroundColor = [UIColor greenColor];

Answer1Button.alpha = 0.5;
但这不是最佳做法。您可以将按钮作为参数发送到处理程序方法: (变量和方法名称在ObjC中不应以大写字母开头。)

并将sender按钮用作处理程序方法中的参数:

-(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];    
}

设置按钮alpha而不是self。alpha=0.5

-(void)Answer1Action{


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

   Answer1Button.alpha = 0.5;

  [UIButton commitAnimations];    
    }

设置按钮alpha而不是self。alpha=0.5

-(void)Answer1Action{


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

   Answer1Button.alpha = 0.5;

  [UIButton commitAnimations];    
    }

每当我尝试将self更改为answer1按钮时,我都会使用未声明的标识符。那么我的问题是,我如何调用Answer1操作中的Answer1按钮?您必须全局声明Answer1按钮,这样您就不会将错误作为未声明的标识符;在.m中使用它,它就会起作用。ie in.m将是这样的-Answer1Button=[UIButtonButtonWithType:UIButtonTypeCustom];每当我尝试将self更改为answer1按钮时,我都会使用未声明的标识符。那么我的问题是,我如何调用Answer1操作中的Answer1按钮?您必须全局声明Answer1按钮,这样您就不会将错误作为未声明的标识符;在.m中使用它,它就会起作用。ie in.m将是这样的-Answer1Button=[UIButtonButtonWithType:UIButtonTypeCustom];太好了,很好,谢谢!同时也感谢您的大写字母通知!我如何在Answer1操作中更改Answer2按钮的背景色?您应该为按钮创建变量。我建议你使用@property.Great这个很好,谢谢!同时也感谢您的大写字母通知!我如何在Answer1操作中更改Answer2按钮的背景色?您应该为按钮创建变量。我建议您使用@property。