简体   繁体   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)
            })
        }
    }
}

When I pan up or down and drag the image view down the background is black eventhough i specified it to be gray. 当我向上或向下拖动并将图像视图向下拖动时,背景是黑色的,尽管我将其指定为灰色。 How can the background be of my chosen color. 背景如何是我选择的颜色。 If any more required info is needed to answer the question please let me know 如果需要更多必要的信息来回答问题,请告诉我

看起来您的panGestureRecognizerAction正在移动视图,而不是backgroundImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM