我已将代码包括到我的分段控件和UIImageView中。任何帮助将是巨大的,谢谢。

let segmentedControl: UISegmentedControl = {
        let sc = UISegmentedControl(items: ["Login", "Register"])
        sc.translatesAutoresizingMaskIntoConstraints = false
        sc.tintColor = UIColor.white
        sc.selectedSegmentIndex = 0

        sc.addTarget(self, action: #selector(handleSegmentedControlChange), for: .valueChanged)
        return sc
    }()


// profile pic
    lazy var picture: UIImageView = {
        var pic = UIImageView()
        if segmentedControl.selectedSegmentIndex == 0 {
            pic.image = UIImage(named: "Logo")
        } else {
            pic.image = UIImage(named: "Profile")

            let mytapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))
            mytapGestureRecognizer.numberOfTapsRequired = 1
            pic.addGestureRecognizer(mytapGestureRecognizer)
            pic.isUserInteractionEnabled = true
        }
        pic.translatesAutoresizingMaskIntoConstraints = false
        pic.contentMode = .scaleAspectFill
        return pic
    }()


图片应该改变并启用触摸手势,但是它们都不起作用。

最佳答案

将If-else块移动到handleSegmentedControlChange选择器。

关于ios - 分段控件,以编程方式更改UIImageView Swift 4/5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56796736/

10-13 04:15