как предотвратить масштабирование parantView при масштабировании imageView - PullRequest
0 голосов
/ 26 октября 2018

Я создал CustomeView, который содержит вид прокрутки Scroll-view.inside, есть один контейнер, который содержит вид изображения плюс две кнопки (Okay and cancel).

Ниже приведена моя иерархия представлений. CustomeView -> ScrollView -> ContainerView -> (imageView + OtherComponent).

Есть две проблемы, с которыми я столкнулся.

  1. при увеличении и уменьшении изображения CustomeView также можно увеличивать и уменьшать уважение к Scrollview.
  2. Положение другого компонента изменяется при уменьшении масштаба.

    class cameraPreview : UIView , UIScrollViewDelegate {
    
    var selectedImage : UIImage!
    var backGroundView = UIView()
    var imageScrollview = UIScrollView()
    var metaData : [String:Any]?
    
    let backgroundImageView  = UIImageView()
    var closeButton : UIButton = {
        let button = UIButton(type: UIButtonType.custom)
        button.setImage(UIImage(named:"closeWhite"), for: .normal)
        button.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
        return button
    }()
    
    
    var okButton : UIButton = {
        let button = UIButton()
        button.setTitle("OK", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.cornerRadius = 15
        button.addTarget(self, action: #selector(okClick), for: .touchUpInside)
        return button
    }()
    
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commoninit()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commoninit()
    }
    
    
    required init(image:UIImage,frame:CGRect,metaData:[String:Any]?) {
        super.init(frame: frame)
        self.selectedImage = image
        self.metaData = metaData
        commoninit()
    }
    
    override func layoutSubviews() {
    
    
    }
    
    
    func commoninit() {
    
        backGroundView.backgroundColor = UIColor.white
        backgroundImageView.contentMode = .scaleAspectFit
    
        self.addSubview(imageScrollview)
        imageScrollview.addSubview(backGroundView)
        backGroundView.addSubview(backgroundImageView)
        backGroundView.addSubview(closeButton)
        backGroundView.addSubview(okButton)
    
    
    
    
        imageScrollview.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        backGroundView.snp.makeConstraints { (make) in
                    make.edges.equalTo(imageScrollview)
                    make.height.width.equalTo(self)
        }
    
        backgroundImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(backGroundView)
        }
        okButton.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(30)
            make.centerX.equalTo(backGroundView.snp.centerX)
            make.bottom.equalTo(backGroundView).offset(-20)
        }
    
    
    
        closeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.left.equalTo(20)
            make.top.equalTo(10)
        }
    
        backgroundImageView.image = selectedImage
        imageScrollview.delegate = self
        imageScrollview.minimumZoomScale = 1.0
        imageScrollview.maximumZoomScale = 6.0
    }
    
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return backgroundImageView
    }
    
    @objc func closeClick(sender:UIButton) {
        self.removeFromSuperview()
    }
    
    @objc func okClick(sender:UIButton) {
        if let topCotroller = UIApplication.shared.gettopMostViewController() {
            self.removeFromSuperview()
            let mediaDetailController = UploadDetailsViewController.instantiate(fromAppStoryboard: .Upload)
            mediaDetailController.mediaImage = selectedImage
            if metaData != nil {
                mediaDetailController.exifDictionary = metaData![kCGImagePropertyExifDictionary as String] as? [String : AnyObject]
            }
            topCotroller.navigationController?.pushViewController(mediaDetailController, animated: true)
        }
    }
    

    }

Ниже приведен код для добавления cameraPreview внутри текущего ControllerView

let imagePreview = cameraPreview(image: image, frame: UIScreen.main.bounds,metaData:metaData)
         self.view.addSubview(imagePreview)

1 Ответ

0 голосов
/ 10 ноября 2018

ваш другой компонент имеет масштабирование, потому что вы помещаете этот компонент в ScrollView. Если вы просто поместите этот компонент вне ScrollView, то ваш компонент не уменьшит масштаб относительно ScrollView.

Ниже приведен исходный код.

class cameraPreview : UIView , UIScrollViewDelegate {

    var selectedImage : UIImage!
    var backGroundView = UIView()
    var imageScrollview = UIScrollView()
    var metaData : [String:Any]?

    let backgroundImageView  = UIImageView()
    var closeButton : UIButton = {
        let button = UIButton(type: UIButtonType.custom)
        button.setImage(UIImage(named:"closeWhite"), for: .normal)
        button.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
        return button
    }()


    var okButton : UIButton = {
        let button = UIButton()
        button.setTitle("OK", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.cornerRadius = 15
        button.addTarget(self, action: #selector(okClick), for: .touchUpInside)
        return button
    }()





    override init(frame: CGRect) {
        super.init(frame: frame)
        commoninit()
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commoninit()
    }


    required init(image:UIImage,frame:CGRect,metaData:[String:Any]?) {
        super.init(frame: frame)
        self.selectedImage = image
        self.metaData = metaData
        commoninit()
    }

    override func layoutSubviews() {


    }


    func commoninit() {
        self.backgroundColor = UIColor.white
        backGroundView.backgroundColor = UIColor.clear
        backgroundImageView.contentMode = .scaleAspectFit

        self.addSubview(imageScrollview)
        imageScrollview.addSubview(backGroundView)
        backGroundView.addSubview(backgroundImageView)
        self.addSubview(closeButton)
        self.addSubview(okButton)




        imageScrollview.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        backGroundView.snp.makeConstraints { (make) in
            make.edges.equalTo(imageScrollview)
            make.height.width.equalTo(self)
        }

        backgroundImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(backGroundView)
        }
        okButton.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(30)
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-20)
        }



        closeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.left.equalTo(20)
            make.top.equalTo(10)
        }

        backgroundImageView.image = selectedImage
        imageScrollview.delegate = self
        imageScrollview.minimumZoomScale = 1.0
        imageScrollview.maximumZoomScale = 6.0
    }


    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return backgroundImageView
    }

    @objc func closeClick(sender:UIButton) {
        self.removeFromSuperview()
    }

    @objc func okClick(sender:UIButton) {
        if let topCotroller = UIApplication.shared.gettopMostViewController() {
            self.removeFromSuperview()
            let mediaDetailController = UploadDetailsViewController.instantiate(fromAppStoryboard: .Upload)
            mediaDetailController.mediaImage = selectedImage
            if metaData != nil {
                mediaDetailController.exifDictionary = metaData![kCGImagePropertyExifDictionary as String] as? [String : AnyObject]
            }
            topCotroller.navigationController?.pushViewController(mediaDetailController, animated: true)
        }
    }
}
...