平移图像后面的黑色背景。斯威夫特 3
Posted
技术标签:
【中文标题】平移图像后面的黑色背景。斯威夫特 3【英文标题】:Black background behind panned images. Swift 3 【发布时间】:2017-07-21 12:10:07 【问题描述】: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)
)
当我向上或向下平移并将图像视图向下拖动时,即使我将其指定为灰色,背景也是黑色的。背景怎么可能是我选择的颜色。如果需要更多所需信息来回答问题,请告诉我
【问题讨论】:
【参考方案1】:看起来您的 panGestureRecognizerAction
正在移动 view 而不是 backgroundImageView
【讨论】:
我怎样才能让它移动 backgroundImageView 当我尝试这样做时它给了我一个错误。非常感谢您的回答无论如何都很有意义 这里有一个很好的教程:ioscreator.com/tutorials/dragging-views-gestures-tutorial-ios10 ... 几乎没有进行任何更改以将“可拖动”UIView 与“可拖动”UIImageView 匹配。以上是关于平移图像后面的黑色背景。斯威夫特 3的主要内容,如果未能解决你的问题,请参考以下文章
UIPageViewController 中控制器后面的黑色背景
使用图像作为 UITableView 背景;背景仍然显示黑色