代码之家  ›  专栏  ›  技术社区  ›  user8822312

Swift UIImage as按钮

  •  0
  • user8822312  · 技术社区  · 7 年前

    我现在正在学习制作一个录音应用程序。基本上,我想在UIView中添加一个点击手势来触发/停止录制。触发我的录音机后,图像应更改为另一个。我的问题是:

    我应该控制两个@iAction函数还是只控制一个?如何区分“触发”和“停止”动作?我的猜测是我可能只需要一个功能,在它的开始,我检查图像名称:如果它是录制图标或停止图标,并做相应的事情。但是,我无法找到UIImage的属性来识别它包含的图像。

    我是ios新手,所以这可能不是一个好问题,但请容忍我。顺便说一句,我正在使用interface builder进行此操作。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Ravi    7 年前

    你可以使用 highlightedImage isHighlighted 的属性 UIImageView . 你可以使用单 IBAction

    viewDidLoad 方法:

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

    您也可以在界面生成器中设置这些图像。正如您在下图中所看到的,您需要为这两个设置图像 Image Highlighted 属性。

    UIImageView interface builder

    在你的行动方法中:

    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 
    }
    
        2
  •  1
  •   Anshul Bhatheja    7 年前

    你只需要一个 IBAction 同样,您可以通过操作内的以下代码检查图像:

    if yourImageView.image == UIImage(named: "yourImageName") {
    //perform some action here
    } else {
    //perform some action here 
    }
    
        3
  •  1
  •   iOS Geek    7 年前

    步骤1-在ViewController类中声明一个属性以检查按钮的状态

    如果选中=true//正在进行录制

    如果为false//则停止录制

    //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
            }
        }