如何在缩放imageView时防止parantView缩放

时间:2018-10-26 08:50:26

标签: ios swift uiscrollview uiimageview zooming

我创建了包含Scroll-view的CustomeView。在滚动视图中,有一个包含图像视图的容器视图以及两个按钮(确定和取消)。

以下是我的视图层次结构。 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)
        }
    }
    

    }

以下是在当前ControllerView内添加cameraPreview的代码

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

1 个答案:

答案 0 :(得分:1)

其他组件是放大视图,因为您将这些组件放在了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)
        }
    }
}