簡體   English   中英

Swift UIImage作為按鈕

[英]Swift UIImage as button

我現在正在學習制作錄音機應用。 基本上,我想向UIView添加輕擊手勢以觸發/停止記錄。 觸發我的記錄器后,圖像應更改為另一幅。 我的問題是:

我應該控制兩個@IBAction函數還是僅一個? 如何區分“觸發”和“停止”動作? 我的猜測是,我可能只需要一個功能,在它的開頭,我會檢查圖像名稱:是錄制圖標還是停止圖標,並相應地執行一些操作。 但是,我找不到UIImage的屬性來標識它包含的圖像。

我是ios的新手,所以這可能不是一個好問題,但是請耐心等待。 順便說一句,我正在使用界面生成器進行此操作。

您只需要一個IBAction可以了,您可以通過操作內的以下代碼檢查圖像:

if yourImageView.image == UIImage(named: "yourImageName") {
//perform some action here
} else {
//perform some action here 
}

通過使用這樣做highlightedImageisHighlighted的性質UIImageView 您可以使用單個IBAction來完成此操作

viewDidLoad方法中:

yourImageView.image = UIImage(name:"RecordImage/TriggerImage")
yourImageView.highlightedImage = UIImage(name:"StopImage")

您也可以在界面構建器中設置這些圖像。 如下面的圖像所示,您需要同時為ImageHighlighted屬性設置圖像。

UIImageView界面構建器

在您的操作方法內:

yourImageView.isHighlighted = !yourImageView.isHighlighted

if yourImageView.isHighlighted
{
  //so now the UIImageView shows stop image that means we are in recording mode
  // do the actions that are to be done in recording mode like start recording updating other UI etc 
}
else
{
 //so now the UIImageView shows Record/Trigger image that means are in normal mode or not recording
  // do the actions that are to be done in normal mode like stop recording (if required )updating other UI etc 
}

第一步-在ViewController類中聲明一個屬性以檢查按鈕的狀態

如果被選中= true //錄制正在進行

如果為假//錄制停止

//declaration in ViewController class
    var isChecked = false

第2步-添加按鈕imageView並分配其按鈕動作,如下所示

 @IBAction func BottomImageBtnAction(_ sender: UIButton) {
        isChecked = !isChecked
        if isChecked {
            //here şet image as stop
        }
        else {
            //here set image as Start
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM