ios UIButton - 触摸更改图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5699374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:36:13  来源:igfitidea点击:

UIButton - On touch change image

iosobjective-ciphoneuibuttonuicontrolstate

提问by Sam007

When I touch the button at that time I want to change image & when i release the touch button image is as it is.

当我当时触摸按钮时,我想更改图像,当我释放触摸按钮时,图像原样。

I want to apply below code but it's not with my expectation.

我想应用下面的代码,但这不符合我的期望。

please give me any suggestion.....

请给我任何建议......

   -(IBAction)actionEnter:(id)sender{
            if ([sender isSelected]) {
                [sender setImage:[UIImage imageNamed:@"enter-hover.png"] 
                        forState:UIControlStateNormal];
                [sender setSelected:NO];
            } else {
                [sender setImage:[UIImage imageNamed:@"enter.png"] 
                        forState:UIControlStateSelected];
                [sender setSelected:YES];
            }

采纳答案by taskinoor

You can use UIControlStateHighlighted for this.

您可以为此使用 UIControlStateHighlighted。

[myButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
          forState:UIControlStateHighlighted];

You can also set this from interface builder by setting the image for highlighted state.

您还可以通过设置突出显示状态的图像从界面构建器中设置此项。

回答by visakh7

I think this should do it. Set the images after creating the button

我认为应该这样做。创建按钮后设置图像

[yourButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
            forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:@"enter.png"]  
            forState:UIControlStateNormal];

and do this

并这样做

- (IBAction)actionEnter:(id)sender{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
}

回答by Jhaliya

I think, you could set the image in the beginning for normal and selected state ..

我认为,您可以在开始时将图像设置为正常和选定状态..

Try with below when you create the UIButtonobject. [Use the images as per your requirement]

创建UIButton对象时,请尝试以下操作。[根据您的要求使用图像]

[myButton setImage:[UIImage imageNamed:@"enter.png"] 
          forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
          forState:UIControlStateSelected];

回答by jxmorris12

In Swift:

在斯威夫特:

button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])

button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])

回答by JScarry

@7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.

@7KV7 让我思考。我有最喜欢和忽略的按钮,我想用它们来标记最喜欢的图片和我再也不想看到的图片。我使用他的方法来初始化按钮,然后稍微修改他的方法来打开和关闭按钮。

In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.

在此示例中,如果您将图片标记为收藏,您希望关闭忽略按钮,反之亦然。委托处理数据库的东西。

 self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ignoreButton   = [UIButton buttonWithType:UIButtonTypeCustom];

        [self.favoriteButton setImage:[UIImage imageNamed:@"Favorite-Selected"] 
                             forState:UIControlStateSelected];
        [self.favoriteButton setImage:[UIImage imageNamed:@"Favorite"] 
                             forState:UIControlStateNormal];

        [self.ignoreButton setImage:[UIImage imageNamed:@"Ignore-Selected"] 
                           forState:UIControlStateSelected];
        [self.ignoreButton setImage:[UIImage imageNamed:@"Ignore"] 
                           forState:UIControlStateNormal];

If you are just toggling a button on or off, you won't need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.

如果您只是打开或关闭按钮,则不需要将其设置为属性,因为 buttonPressed 发送者知道按下了哪个按钮。我需要让它们成为财产,因为我需要告诉相反的按钮关闭其突出显示。

- (void)favoriteIgnore:(UIButton *)buttonPressed {

     // Toggle the tapped button
     buttonPressed.selected = ( buttonPressed.selected) ?  NO : YES;

    id <ScoringToolbarDelegate> TB_delegate = _delegate;

    // Turn off the other button and call the delegate
    if ([buttonPressed.currentTitle isEqualToString:@"favorite"]) {

        self.ignoreButton.selected = NO;
        [TB_delegate favoriteButtonPressed];

    } else {

        self.favoriteButton.selected = NO;
        [TB_delegate ignoreButtonPressed];
    }
}

回答by Magic Bullet Dave

to change the image immediately use the backgroundImage property.

要立即更改图像,请使用 backgroundImage 属性。