如何突出显示栏按钮项? (迅速)

时间:2022-07-05 22:58:10

i use toolbar items for using action properties, i want when i tapped on on of the items, images be change to highlighted image.

我使用工具栏项目来使用动作属性,我希望当我点击项目时,图像将更改为突出显示的图像。

this is my all three buttons :

这是我的全部三个按钮:

@IBAction func audio(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(0,0);
}

@IBAction func mainBtn(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(415,0);
}

@IBAction func videoBtn(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(830,0);
}

@IBOutlet var inpt: UITextField!
@IBOutlet var scView: UIScrollView!

this is my app picture: image

这是我的应用图片:图片

thanks

1 个解决方案

#1


0  

If all you want is to show the button with a highlighted images while you are pressing the button - you can do that through the storyboard - you can set the image for each button in the default state and also in highlighted - but when you release the button, it goes back to the original image.

如果您只想在按下按钮时显示带有突出显示图像的按钮 - 您可以通过故事板进行操作 - 您可以为默认状态下的每个按钮设置图像,也可以突出显示 - 但是当您释放时按钮,它会回到原始图像。

If you want the image to toggle when you select the button, you're going to have to add outlets for the buttons, and a button state variable (if it only changes once, and never goes back to the original, then you don't need the state)

如果你想要在选择按钮时切换图像,你将不得不为按钮添加插座,以及一个按钮状态变量(如果它只改变一次,并且永远不会回到原始状态,那么你就不要需要国家)

Here's an example of how you might do it - updated to reset the 'other' button

这是一个如何操作的示例 - 更新以重置“其他”按钮

@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var videoButton: UIButton!

var mainButtonSelected : Bool = false
var videoButtonSelected : Bool = false

@IBAction func mainButton(sender: AnyObject)
{
    mainButtonSelected = !mainButtonSelected // toggle state
    if mainButtonSelected
    {
        // highlight the mainButton
        mainButton.setImage(UIImage(named: "imageMainHighlight.png"), forState: .Normal)

        // clear the highlight (if any) on videoButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the videoButtonSelected flag is correct
        videoButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on mainButton, no need to do anything with videoButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}

@IBAction func videoButton(sender: AnyObject)
{
    videoButtonSelected = !videoButtonSelected // toggle state
    if videoButtonSelected
    {
        // highlight videoButton
        videoButton.setImage(UIImage(named: "imageVideoHighlight.png"), forState: .Normal)
        // clear the highlight (if any) on mainButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the mainButtonSelected flag is correct
        mainButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on videoButton, no need to do anything for mainButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}

#1


0  

If all you want is to show the button with a highlighted images while you are pressing the button - you can do that through the storyboard - you can set the image for each button in the default state and also in highlighted - but when you release the button, it goes back to the original image.

如果您只想在按下按钮时显示带有突出显示图像的按钮 - 您可以通过故事板进行操作 - 您可以为默认状态下的每个按钮设置图像,也可以突出显示 - 但是当您释放时按钮,它会回到原始图像。

If you want the image to toggle when you select the button, you're going to have to add outlets for the buttons, and a button state variable (if it only changes once, and never goes back to the original, then you don't need the state)

如果你想要在选择按钮时切换图像,你将不得不为按钮添加插座,以及一个按钮状态变量(如果它只改变一次,并且永远不会回到原始状态,那么你就不要需要国家)

Here's an example of how you might do it - updated to reset the 'other' button

这是一个如何操作的示例 - 更新以重置“其他”按钮

@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var videoButton: UIButton!

var mainButtonSelected : Bool = false
var videoButtonSelected : Bool = false

@IBAction func mainButton(sender: AnyObject)
{
    mainButtonSelected = !mainButtonSelected // toggle state
    if mainButtonSelected
    {
        // highlight the mainButton
        mainButton.setImage(UIImage(named: "imageMainHighlight.png"), forState: .Normal)

        // clear the highlight (if any) on videoButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the videoButtonSelected flag is correct
        videoButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on mainButton, no need to do anything with videoButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}

@IBAction func videoButton(sender: AnyObject)
{
    videoButtonSelected = !videoButtonSelected // toggle state
    if videoButtonSelected
    {
        // highlight videoButton
        videoButton.setImage(UIImage(named: "imageVideoHighlight.png"), forState: .Normal)
        // clear the highlight (if any) on mainButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the mainButtonSelected flag is correct
        mainButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on videoButton, no need to do anything for mainButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}