我想在按钮上使用bool,如果我点击imageview的按钮图像改变了,如果再次单击按钮然后使用Swift反转图像

时间:2017-11-13 07:58:56

标签: swift image button boolean

我的代码是: func PickUpCarCost(){

        imgCheck.image = UIImage(named: "uncheck.png")
        imgUnCheck.image = UIImage(named: "uncheck.png")
        if CarPickUpButton?.tag == 1 {
            imgCheck.image = UIImage(named: "check.png")
           self.pickupChargelabel.text=self.PickUpFare.text!
        }
        else {
            imgUnCheck.image = UIImage(named: "uncheck.png")

        }

}

在这个我想要拿bool就像按钮选择图像视图图像将是check.png如果取消选择图像视图图像将uncheck.png如何在swift中为此编码。

3 个答案:

答案 0 :(得分:1)

你可以选择变量。并为按钮状态添加不同的图像。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list-item">
    <div class="list-content">
        <div class="list-updates">
            <a href="#" class="list-avatar"><img
                        src="images/avatar/avatar_<?php echo ( $i <= 8 ? '0' : NULL ) . $avatars[ $i ] ?>.jpg"
                        alt="user-avatar" width="40"
                        height="40"></a>
            <a href="#"><i class="material-icons"><?php echo get_icon( $update[ $i ] ) ?></i></a>
        </div>
        <div class="item-title"><a href="#"><?php echo $questions[ $i ] ?></a>
        </div>
        <div class="ans-box">
            <div class="ans-count"><?php echo $ans[ $i ] ?></div>
            <span class="item-1"><i class="material-icons meta-info-btn">keyboard_arrow_down</i></span>
        </div>
    </div>

    <div class="metas" id="item-1">
        <div class="update-info"><a href="#">commented</a> 1 hour ago in <a href="#">Plugins</a> by
            <a href="#">gurjyot</a>
        </div>
        <div class="counters">
            <span class="votes"><i class="material-icons">arrow_upward</i> +1245</span>
            <span class="views"><i class="material-icons">remove_red_eye</i> 50.2k</span>
        </div>
    </div>
</div><!-- list-item -->

答案 1 :(得分:0)

在此按钮上添加UITapGestureRecognizer。或者您可以使用 UIButton 自己的属性isSelected?

@IBOutlet weak var myBtn: UIButton!

Viewdidload 方法中添加以下行

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap")  

myBtn.addGestureRecognizer(tapGesture)

func handleTap(sender: UITapGestureRecognizer) {

  if sender.state == .ended {
      // handling code
  }
  else if sender.state == .began {

  }
}

根据您的需要添加手势。我使用了长按并轻按手势

答案 2 :(得分:0)

这段代码对我有用。全球定义var isclick = false

 func CarDeliverCost() {
        if (!isclick){

            imgUnCheck.image = UIImage(named: "check.png")
            isclick=true



        }
        else
        {
            imgUnCheck.image = UIImage(named: "uncheck.png")
            isclick=false



        }
    }