如果您有一个UIScrollView
可以放大的,并且您将 iOS 13 上下文菜单交互添加到滚动视图内的视图(例如: a UIImageView
),当您执行交互时,它会奇怪地暂时放大图像,然后将其缩小显示上下文菜单,然后在退出此上下文菜单时,它会使图像放大得很远。它似乎超出了 UIImageView 的范围。
StackOverflow 似乎不支持嵌入视频/GIF,所以这是 Imgur 上的一段视频,显示了我的意思:https ://imgur.com/mAzWlJA
有没有办法防止这种行为?例如,在WKWebView
(UIScrollView
子类)中,长按图像不会表现出这种行为。
如果您想在一个简单的新 Xcode 项目中测试它,下面是显示它的示例的简单代码:
import UIKit
class RootViewController: UIViewController, UIScrollViewDelegate, UIContextMenuInteractionDelegate {
let scrollView = UIScrollView()
let imageView = UIImageView(image: UIImage(named: "cat.jpg")!)
override func viewDidLoad() {
super.viewDidLoad()
[view, scrollView].forEach { $0.backgroundColor = .black }
scrollView.delegate = self
scrollView.frame = view.bounds
scrollView.addSubview(imageView)
scrollView.contentSize = imageView.frame.size
view.addSubview(scrollView)
// Set zoom scale
let scaleToFit = min(scrollView.bounds.width / imageView.bounds.width, scrollView.bounds.height / imageView.bounds.height)
scrollView.maximumZoomScale = max(1.0, scaleToFit)
scrollView.minimumZoomScale = scaleToFit < 1.0 ? scaleToFit : 1.0
scrollView.zoomScale = scaleToFit
// Add context menu support
imageView.isUserInteractionEnabled = true
imageView.addInteraction(UIContextMenuInteraction(delegate: self))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
}
// MARK: - UIScrollView
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
// MARK: - Context Menus
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: { () -> UIViewController? in
return nil
}) { (suggestedElements) -> UIMenu? in
var children: [UIAction] = []
children.append(UIAction(title: "Upvote", image: UIImage(systemName: "arrow.up")) { (action) in
})
children.append(UIAction(title: "Downvote", image: UIImage(systemName: "arrow.down")) { (action) in
})
return UIMenu(title: "", image: nil, identifier: nil, options: [], children: children)
}
}
}
cat.jpg
如果你也喜欢的话,这里是: https ://imgur.com/hTTZaw4