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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.