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)
})
}
}
}
当我向上或向下平移并向下拖动图像视图时,背景是黑色的,尽管我指定它是灰色的。背景如何是我所选择的颜色。如果需要更多必要信息来回答这个问题,请告诉我
答案 0 :(得分:1)
您的panGestureRecognizerAction
看起来正在移动视图而不是backgroundImageView