mercredi 27 janvier 2021

I want to make if statement so each Image will be full screen when the user tsp

here is the code that I check if they images are tapped but only the 3 image is apply full screen. func imageTapped() { let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:))) if detailsImage[0].isUserInteractionEnabled == false { detailsImage[0].isUserInteractionEnabled = true detailsImage[0].addGestureRecognizer(tapGestureRecognizer) self.statusImageView = detailsImage[0] } let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:))) detailsImage[1].isUserInteractionEnabled = true detailsImage[1].addGestureRecognizer(tapGestureRecognizer1) self.statusImageView = detailsImage[1]

    let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
        detailsImage[2].isUserInteractionEnabled = true
        detailsImage[2].addGestureRecognizer(tapGestureRecognizer2)
        self.statusImageView = detailsImage[2]
 
}



let blackBackgroundColor = UIView()
let tappedImage = UIImageView()
var statusImageView: UIImageView?
let navigationBarView = UIView()


@objc func imageZoom(tapGestureRecognizer: UITapGestureRecognizer) {
    annimationImage(detailsImage: statusImageView!)
}

func annimationImage(detailsImage: UIImageView) {
    
    if let startingFrame = statusImageView?.superview?.convert(statusImageView!.frame, to: nil) {
        
        statusImageView!.alpha =  0
        
    blackBackgroundColor.frame = self.view.frame
    blackBackgroundColor.backgroundColor = UIColor.black
    blackBackgroundColor.alpha = 0
    view.addSubview(blackBackgroundColor)
        
        navigationBarView.frame = CGRect(x: 0, y: 0, width: 1000, height: 100)
        navigationBarView.backgroundColor = UIColor.black
        navigationBarView.alpha = 0
        
        if let keyWindow = UIApplication.shared.keyWindow {
            keyWindow.addSubview(navigationBarView)
        }

    let tappedImage = UIImageView()
    tappedImage.backgroundColor = .gray
    tappedImage.frame = startingFrame
    tappedImage.contentMode = .scaleToFill
    tappedImage.isUserInteractionEnabled = true
    tappedImage.image = statusImageView?.image
    tappedImage.clipsToBounds = true
    view.addSubview(tappedImage)
      
       
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    tappedImage.addGestureRecognizer(tap)
    
    UIView.animate(withDuration: 0.75, animations: {
        
        let height = (self.view.frame.width/startingFrame.width)*startingFrame.height
        let y = self.view.frame.height / 2 - height / 2

        tappedImage.frame = CGRect(x: 0, y: y, width: self.view.frame.size.width, height: 300)

            self.blackBackgroundColor.alpha = 1
            self.navigationBarView.alpha = 1
        })
    }
}

@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    if let startingFrame = statusImageView!.superview?.convert(statusImageView!.frame, to: nil) {
   
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
        self.tappedImage.frame = startingFrame
        self.blackBackgroundColor.alpha = 0
        self.navigationBarView.alpha = 0
   }, completion: {(didComplate) -> Void in
        self.navigationBarView.removeFromSuperview()
        self.statusImageView?.alpha = 1
        sender.view?.removeFromSuperview()
        })
    }
}

Aucun commentaire:

Enregistrer un commentaire