swift 有没有更简单的方法来以编程方式查看UIImageView?

rbpvctlc  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(80)

我有8个按钮,我想每按一次按钮就显示一张图片。
我想知道的是,我需要有8个功能来显示这些图像吗?或者有更简单的方法吗?
这就是我是如何做到这一点,它的工作原理,因为它应该,但我不想重复同样的事情一遍又一遍?

var imageView1:UIImageView!
var imageView2:UIImageView!
var imageView3:UIImageView!

    override func viewDidLoad() {
    super.viewDidLoad()
    showImage1()
    showImage2()
    showImage3()
    tapGestureRecognizerFunc()

  }

 @objc func button1Tap() {
    if self.imageView1.isHidden {
       self.imageView1.isHidden = false            
    }else{
       self.imageView1.isHidden = true
    }
}
@objc func button2Tap() {
    if self.imageView2.isHidden {
       self.imageView2.isHidden = false            
    }else{
       self.imageView2.isHidden = true
    }

}
@objc func button3Tap() {
    if self.imageView3.isHidden {
       self.imageView3.isHidden = false            
    }else{
       self.imageView3.isHidden = true
    }

}
func showImage1() {
    imageView1 = UIImageView(frame: CGRect(x: 271, y: 8, width: 29, height: 29))
    imageView1.image = UIImage(named: "Done.png")
    imageView1.contentMode = .scaleAspectFit
    View1.addSubview(imageView1)
    imageView1.isHidden = true
}
func showImage2() {
    imageView2 = UIImageView(frame: CGRect(x: 271, y: 8, width: 29, height: 29))
    imageView2.image = UIImage(named: "Done.png")
    imageView2.contentMode = .scaleAspectFit
    View2.addSubview(imageView2)
    imageView2.isHidden = true
}
func showImage3() {
    imageView2 = UIImageView(frame: CGRect(x: 271, y: 8, width: 29, height: 29))
    imageView2.image = UIImage(named: "Done.png")
    imageView2.contentMode = .scaleAspectFit
    View3.addSubview(imageView2)
    imageView2.isHidden = true
}
func tapGestureRecognizerFunc () {        
    let exercise1Tap = UITapGestureRecognizer(target: self, action: #selector(button1Tap))
    exercise1Tap.numberOfTapsRequired = 2
    View1.addGestureRecognizer(exercise1Tap)

    let exercise2Tap = UITapGestureRecognizer(target: self, action: #selector(button2Tap))
    exercise2Tap.numberOfTapsRequired = 2
    View2.addGestureRecognizer(exercise2Tap)

    let exercise3Tap = UITapGestureRecognizer(target: self, action: #selector(button3Tap))
    exercise3Tap.numberOfTapsRequired = 2
    View3.addGestureRecognizer(exercise3Tap)

}

是的,我是新手

zdwk9cvp

zdwk9cvp1#

因为我不知道,在哪里以及如何创建按钮,这是很难回答。以下只是一个提示。
1.您应该使用UIImageView的数组
1.您的回调应使用Form buttonAction(发送者:UIButton)
1.您可以使用按钮的标记来获取相应按钮的编号
例如:
类ViewController:UIViewController {

var imageviews : [UIImageView] = []
override func viewDidLoad() {
    super.viewDidLoad()

    for i in 1...8 {
        let imageview = UIImageView()
        view.addSubview(imageview)
        imageview.tag = i
        imageviews.append(imageview)
        let button = UIButton(frame: CGRect(x: 0, y: CGFloat(i)*50.0, width: 100, height: 30))
        view.addSubview(button)
        button.setTitle("Button \(i)", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.tag = i
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    }
}

@objc func buttonAction(sender : UIButton) {
    let index = sender.tag
    print("Button \(index) pressed")

    imageviews[index].isHidden = !imageviews[index].isHidden
}

}

相关问题