簡體   English   中英

平移圖像后面的黑色背景。 迅捷3

[英]Black background behind panned images. Swift 3

class PhotoViewController: UIViewController {

override var prefersStatusBarHidden: Bool {
    return true
}

private var backgroundImage: UIImage

init(image: UIImage) {
    self.backgroundImage = image
    super.init(nibName: nil, bundle: nil)
    print(image)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
    super.viewDidLoad()






    self.view.backgroundColor = UIColor.gray
    let backgroundImageView = UIImageView(frame: view.frame)
    backgroundImageView.image = backgroundImage
    view.addSubview(backgroundImageView)




    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction(_:)))
    self.view.addGestureRecognizer(panGestureRecognizer)

    let cancelButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: 20.0, height: 20.0))
    cancelButton.setImage(#imageLiteral(resourceName: "cancel"), for: UIControlState())
    cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
    let next = UIButton(frame: CGRect(x: (view.frame.width)-90, y: (view.frame.height)-125, width: 70, height: 70))
    next.setImage(#imageLiteral(resourceName: "Next"), for: UIControlState())
    next.addTarget(self, action: #selector(cancel), for: .touchUpInside)
    view.addSubview(next)
    view.addSubview(cancelButton)
}




func panGestureRecognizerAction(_ gesture: UIPanGestureRecognizer){
    let translation = gesture.translation(in: view)
    view.frame.origin.y = translation.y

    if gesture.state == .ended{

        if view.frame.origin.y > 100 && view.frame.origin.y < 200{
            UIView.animate(withDuration: 0.5, animations: { 

                dismiss(animated: false, completion: nil)

            })
        } else{
            UIView.animate(withDuration: 0.3, animations: { 

                self.view.frame.origin = CGPoint(x: 0, y: 0)
            })
        }
    }
}

當我向上或向下拖動並將圖像視圖向下拖動時,背景是黑色的,盡管我將其指定為灰色。 背景如何是我選擇的顏色。 如果需要更多必要的信息來回答問題,請告訴我

看起來您的panGestureRecognizerAction正在移動視圖,而不是backgroundImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM