如何设置UIButton按下后高亮状态

发布于 2024-12-02 01:44:10 字数 519 浏览 0 评论 0原文

我有一个典型的要求,其中我需要在按下按钮后将其保持在突出显示状态。我需要执行一项仅当按钮处于突出显示状态时才有效的任务。实际上,我正在以编程方式将按钮状态设置为突出显示。

[sender setHighlighted:YES];

一旦按钮处于突出显示状态,我需要执行另一个操作。

- (IBAction)changeState: (UIButton*)sender
{   
    if (sender.highlighted == YES)
    {
        [self performSomeAtion:sender];
    }
}

但是,令我恐惧的是,每当我按下任何按钮时,上述条件就会变为现实,并且该操作会被重复执行。有什么方法可以让 UIButton 的状态在按下后保持突出显示吗?

编辑 - 实际上我需要对按钮的 3 种不同状态执行 3 种不同的操作。我已经在使用选定状态和正常状态。现在,我需要利用突出显示的状态。

I have a typical requirement wherein I need to keep a button in highlighted state after pressing it. I need to perform a task which should work only when a button is in highlighted state. Actually I am setting a button state to highlighted programatically.

[sender setHighlighted:YES];

And once the button is in highlighted state i need to perform another action.

- (IBAction)changeState: (UIButton*)sender
{   
    if (sender.highlighted == YES)
    {
        [self performSomeAtion:sender];
    }
}

But, to my horror, whenever I press any button, the above condition is becoming true and the action is being performed repeatedly. Is there any way in which i can keep a UIButton's state to be highlighted after pressing it?

EDIT - Actually I need to perform 3 different actions for 3 different states of the button. I am already making use of selected state and normal state. Now, I need to make use of the highlighted state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

寒尘 2024-12-09 01:44:10
[sender setSelected:YES]; 

或者您可以使用 UIButton 的两个图像(notselectedimage.pngselectedimage.png)来模拟此效果,然后使用 BOOL 变量(如 BOOL)跟踪按钮状态按钮当前状态;。然后在 .h 文件中:

BOOL buttonCurrentStatus;

和在 .m 文件中

// connect this method with Touchupinside function
- (IBAction)changeState:(UIButton*)sender
{
    /* if we have multiple buttons, then we can
       differentiate them by tag value of button.*/
    // But note that you have to set the tag value before use this method.

  if([sender tag] == yourButtontag){

    if (buttonCurrentStatus == NO)
    {
        buttonCurrentStatus = YES;
        [butt setImage: [UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }
    else
    {
        buttonCurrentStatus = NO;
        [butt setImage:[UIImage imageNamed:@"notSelectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }   
  }
}
[sender setSelected:YES]; 

or you can simulate this effect with two image for your UIButton (notselectedimage.png and selectedimage.png), then keep track button state with a BOOL variable like BOOL buttonCurrentStatus;. Then in .h file:

BOOL buttonCurrentStatus;

and in .m file

// connect this method with Touchupinside function
- (IBAction)changeState:(UIButton*)sender
{
    /* if we have multiple buttons, then we can
       differentiate them by tag value of button.*/
    // But note that you have to set the tag value before use this method.

  if([sender tag] == yourButtontag){

    if (buttonCurrentStatus == NO)
    {
        buttonCurrentStatus = YES;
        [butt setImage: [UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }
    else
    {
        buttonCurrentStatus = NO;
        [butt setImage:[UIImage imageNamed:@"notSelectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }   
  }
}
仅冇旳回忆 2024-12-09 01:44:10
- (void)mybutton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = ![button isSelected]; // Important line
    if (button.selected)
    {
        NSLog(@"Selected");
        NSLog(@"%i",button.tag);
    }
    else
    {
        NSLog(@"Un Selected");
        NSLog(@"%i",button.tag);

    }
 }
- (void)mybutton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = ![button isSelected]; // Important line
    if (button.selected)
    {
        NSLog(@"Selected");
        NSLog(@"%i",button.tag);
    }
    else
    {
        NSLog(@"Un Selected");
        NSLog(@"%i",button.tag);

    }
 }
残花月 2024-12-09 01:44:10

突出显示状态用于在触摸按钮时突出显示按钮。按钮中的触地事件会突出显示它。您应该改用“选定”状态。

如果您想要做的是在按下按钮后执行操作,请不要将您的方法附加到状态更改事件,而将您的方法附加到 TouchUpInside 事件。

The highlighted state is used to highlight the button while it is being touched. A touch down event in the button highlights it. You should use the "selected" state instead.

If what you want to do is perform an action after the button is pressed, don't attach your method to the state change event, attach your method to the TouchUpInside event.

强者自强 2024-12-09 01:44:10

我只是找到一种方法,所以我分享它,以防万一......

我保留了我的 UIButton 并为每个状态设置一个图像(这样你就可以进入 4 个状态按钮)。
我将 UserInteractionEnabled 设置为 NO ->此按钮不会受到任何触摸。
第一个按钮的目的是显示

我创建第二个自定义 UIButton 的状态,其框架与第一个按钮相同。对于此按钮,不会为该状态设置任何图像(它是一个完全透明的按钮)。该按钮的目的是捕获触摸事件。因此,我在 TouchUpInside 事件上向该按钮添加了一个目标。然后,当事件触发时,我将第一个按钮的状态更改为“禁用”、“突出显示”、“选定”或“无”这些状态(= 默认状态)。

一切都很顺利!

I just find a way, so I share it, just in case...

I kept my UIButton and set one image for each state (so you could go up to a 4 states button).
I set the UserInteractionEnabled to NO -> This button won't receive any touch.
The purpose of this first button is to show a state

I create a second custom UIButton with the same frame than the first one. For this one, none image will be set for the state (it's a fully transparent button). The purpose of this button is to catch the touch event. So I added a target to this button on the TouchUpInside event. And then when the event is fired, I change the state of the first button to Disabled, Highlighted, Selected, or none of these state (= Default state).

Everything is working like a charm!

云仙小弟 2024-12-09 01:44:10

按照您描述的方式,您最好继承 UIView 来创建您自己的三状态按钮。

实际上,您甚至应该实现自己的多状态 ButtonView,并通过用于外观的 PNG 数组和用于了解其被按下次数的状态数组来管理其内部状态。

The way you describe it, you'd be better off subclassing UIView to create your own three-state button.

Actually, you should even implement your own multistate buttonView, and manage the state it's in internally via an array of PNG for the looks and an array of states to know how many times it's been pressed.

橘和柠 2024-12-09 01:44:10

使用[sender setSelected: YES];,我认为这对你有用。

Use [sender setSelected: YES];, I think it will be useful to you.

谢绝鈎搭 2024-12-09 01:44:10
UIButton *btn_tmp=sender;
    if(!(btn_tmp.selected))
    {
[btn_temp setHighlighted:YES];

}
UIButton *btn_tmp=sender;
    if(!(btn_tmp.selected))
    {
[btn_temp setHighlighted:YES];

}
瀟灑尐姊 2024-12-09 01:44:10

仅适用于 iOS 7:您应该考虑将图像 renderMode 设置为 UIImageRenderingModeAlwaysTemplate。然后您可以使用tintColor 来表示各种状态。

请参阅如何将tintColor应用于UIImage?

参阅为 UIView 及其所有子视图着色

For iOS 7 only: you should consider setting the image renderMode to UIImageRenderingModeAlwaysTemplate. You can then use the tintColor to represent various states.

see How to apply a tintColor to a UIImage?
and

see Tint a UIView with all its subviews

荒芜了季节 2024-12-09 01:44:10

解决方案很棘手,但也是可能的。

问题是您尝试更改按钮操作方法中的突出显示状态,我认为这会在操作结束时进行清理或检查过程并切换突出显示状态。当您尝试调试它时,您会得到突出显示的= 1,但它最终会发生变化。

奇怪的是,当您想将按钮保持在“突出显示”模式(例如“选定”模式)以根据 3 种状态获得不同的操作时,“3 种状态按钮”有时很有用。
唯一的问题是您无法在按钮操作方法中分析它或将其切换到突出显示模式,因为当用户按下它并在最后将其切换回来时,这将立即切换到突出显示模式。

解决方案是使用调度。

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [theButton setHighlighted:YES];
});

这样就可以了,您可以使用 3 种状态。

The solution is tricky but it's possible.

The problem is that you tried to change the highlighted status in the button action method, which I suppose makes a clean up or check process at the end of the action and switch the highlighted status. When you try to debug it you get the highlighted = 1 but it will change at the end.

Strange but your "3 statuses button" is sometimes useful, when you'd like to keep a button in "highlighted" mode like the "selected" mode to get different action depending on the 3 statuses.
The only problem that you couldn't analyze this or switch it to highlighted mode in the button action method as this will switch to highlighted mode immediately as the user push it AND switch it back at the end.

The solution is using a dispatch.

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [theButton setHighlighted:YES];
});

This will do the trick and you could use the 3 statuses.

私野 2024-12-09 01:44:10

根据苹果的说法,UIButton 具有 imageView

虽然此属性是只读的,但它自己的属性是读/写的。使用这些属性来配置按钮视图的外观和行为

这意味着您可以在 IB 中(在 Storyboard 中)进行设置为此按钮设置图片并设置突出显示的图片:

  1. 打开属性检查器。
  2. 按钮部分下,选择一个图像。
  3. 在同一部分中,将状态配置更改为突出显示。请注意,您在默认情况下选择的图像现已消失,现在您可以为突出显示设置新图片。
  4. 现在您有了一个具有 2 种状态配置的按钮,并且您在运行时所需要做的就是更改 button.highlighted = true。另外,请检查配置控件下的UIControl更多状态的属性。

您还可以通过编程方式执行此操作,如下所示:

Swift(在 Objective-C 中几乎相同):

// Setting the highlighted image
self.someButton.imageView?.highlightedImage = UIImage(named: "imageNameFromImageAssest")
// someButton will now some the highlighted image and NOT the image set in the IB
self.someButton.imageView?.highlighted = true

According to apple, UIButton has a property of imageView:

Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view

This means that you can set in the IB (in the storyboard) a picture for this button and set the highlighted picture:

  1. Open the Attribute inspector.
  2. Under Button section, choose an image.
  3. In the same section, change the State Config to Highlighted. Notice the image you chose under default is now gone and now you can set a new picture for the Highlighted.
  4. Now you have a button with 2 state config and all you have to do during runtime to change the button.highlighted = true. Also, check the UIControl under Configuring the Control’s Attributes for more states.

You can also do it programatically as follows:

Swift (and almost the same in Objective-C):

// Setting the highlighted image
self.someButton.imageView?.highlightedImage = UIImage(named: "imageNameFromImageAssest")
// someButton will now some the highlighted image and NOT the image set in the IB
self.someButton.imageView?.highlighted = true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文