单次点击 Tint 2 UIButton

Posted

技术标签:

【中文标题】单次点击 Tint 2 UIButton【英文标题】:Tint 2 UIButton on single Click 【发布时间】:2013-09-06 12:28:14 【问题描述】:

我有两个按钮说 button1 和 button2 声明为

- (IBAction)button1:(id)sender;
  @property (strong, nonatomic) IBOutlet UIButton *button2;

我想要做的是,当 button1 被点击时,button 2 的色调颜色应该改变,所以它就像 button1 和 2 一样,当我点击 button1 时,色调也应该在 button2 中生效,就像 button2 也被点击一样,所以它的外观应该就像我点击button1时一次点击两个按钮一样。这可能吗?

我的 button2 的 UserInteraction 被禁用,因为我不需要它的点击事件。在我的 button1 上单击时,我需要 button2 也对其进行着色,使其看起来像同时单击按钮 1 和 2。

编辑: 你们中的一些人仍然没有明白我的意思。假设在运行时屏幕上有两个按钮,Button1 和 Button2,假设 Button1 是不可见的。现在,如果我单击 Button1,它应该会在我刚刚单击 Button2 的屏幕上看到。设置 tint color 只会设置 button2 tintcolor 但不会给按钮点击效果。

【问题讨论】:

这些不是按钮声明,它们是您命名为button1:button2: 的按钮操作。 我已经更新了,我只需要对 button1 进行操作 @Gamerlegend 请查看我的回答,并仅将代码添加到您的第一个操作... 【参考方案1】:

是的,您所要做的就是将插座连接到按钮...您可以随意调用它们。将此代码 BOTH 放入您的 IBActions。

   your1stButton.tintColor = [UIColor redColor];
   your2ndButton.tintColor = [UIColor redColor];

在不同的操作中更改颜色,并确保将your1stButtonyour2ndButton 更改为您的网点名称。

【讨论】:

问一个问题,当我点击 button1 时,按钮 2 会着色,例如:如果我设置 your2ndButton.tintColor = [UIColor redColor];在 button1 动作上,现在只有当我点击 button2.Right 时才会生效?如何在 button1 中看到 button2 的色调。 @Gamerlegend 如果添加 your2ndButton.tintColor = [UIColor redColor];到您的按钮 1 操作,当您按下按钮 1 时它会被调用。 我同意,按钮不着色,您的代码将设置着色颜色,但当我单击 button1 时它不会使 button2 着色 @Gamerlegend 你连接按钮2的插座了吗? @Gamerlegend 你是如何让按钮不可见的?【参考方案2】:
- (IBAction)button1:(id)sender;

   btn1.tintColor = [UIColor redColor];
   btn2.tintColor = [UIColor redColor];



- (IBAction)button2:(id)sender;


btn1.tintColor = [UIColor redColor];
   btn2.tintColor = [UIColor redColor];



【讨论】:

【参考方案3】:

您需要在标题中将 button1 和 button2 设置为 @property(出口),并声明方法。在- (IBAction)button1:(id)sender; 方法中,您可以像这样设置色调颜色:self.button2.tintColor = [UIColor redColor];

- (IBAction)button1:(id)sender

  self.button2.tintColor = [UIColor redColor];


- (IBAction)button2:(id)sender

  self.button1.tintColor = [UIColor redColor];

【讨论】:

button2 没有着色,我创建了它的出口,但在这里你将 tintColor 设置为红色,你没有调用 tint 按钮 在针对 ios 7 的项目中,我无法让按钮的色调颜色发生变化,如果这是一个错误,我想知道。这段代码适用于 ios 6+,但不适用于 7 问一个问题,当我点击 button1 时,在 button1 动作上,你已经为 button2 设置了颜色,现在只有当我点击 button2 时才会生效。对吗? 由于button2被设置为一个属性并且你只设置了button1的触摸方法,它会改变tintColor,它是button2的一个属性。基本上你在声明出口时所做的就是将 button2 声明为变量 您不再需要 - (IBAction)button2:(id)sender 方法,因为无论如何都禁用了 userInteraction

以上是关于单次点击 Tint 2 UIButton的主要内容,如果未能解决你的问题,请参考以下文章

根据状态更改 UIButton 中 ImageView 的 tint 颜色

UIButton tint color 不适用于整个按钮?在故事板中设置?

Android 安装包优化Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )

ImageView的tint属性

ImageView的tint属性

Android开发小技巧-动态设置Drawable与Tint