为什么我不能在这个应用程序中使用其他铅笔或颜色?它只能画黑色。这是我的代码:
import UIKit
import PencilKit
import PhotosUI
class ViewController: UIViewController, PKCanvasViewDelegate, PKToolPickerObserver {
@IBOutlet weak var pencilButton: UIBarButtonItem!
@IBOutlet weak var canvasView: PKCanvasView!
let canvasWidth: CGFloat = 768
let canvasOverScrollHeight: CGFloat = 500
let drawing = PKDrawing()
override func viewDidLoad() {
super.viewDidLoad()
canvasView.drawing = drawing
canvasView.delegate = self
canvasView.alwaysBounceVertical = true
canvasView.drawingPolicy = .anyInput
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let canvasScale = canvasView.bounds.width / canvasWidth
canvasView.minimumZoomScale = canvasScale
canvasView.maximumZoomScale = canvasScale
canvasView.zoomScale = canvasScale
updateContentSizeForDrawing()
canvasView.contentOffset = CGPoint(x: 0, y: -canvasView.adjustedContentInset.top)
}
override var prefersHomeIndicatorAutoHidden: Bool{
return true
}
@IBAction func fingerOrPencil (_ sender: Any) {
canvasView.allowsFingerDrawing.toggle()
pencilButton.title = canvasView.allowsFingerDrawing ? "Finger" : "Pencil"
}
@IBAction func saveToCameraRoll(_ sender: Any) {
UIGraphicsBeginImageContextWithOptions(canvasView.bounds.size, false, UIScreen.main.scale)
canvasView.drawHierarchy(in: canvasView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if image != nil {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image!)
}, completionHandler: {success, error in
})
}
}
func updateContentSizeForDrawing() {
let drawing = canvasView.drawing
let contentHeight: CGFloat
if !drawing.bounds.isNull {
contentHeight = max(canvasView.bounds.height, (drawing.bounds.maxY + self.canvasOverScrollHeight) * canvasView.zoomScale)
} else {
contentHeight = canvasView.bounds.height
}
canvasView.contentSize = CGSize(width: canvasWidth * canvasView.zoomScale, height: contentHeight)
}
// Delegate Methods
func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
updateContentSizeForDrawing()
}
func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {
}
func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {
}
func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {
}
}
以下是控制台中的输出:
2023 - 01 - 04 18:34:04.429420 + 0300绘图[45460:449613][Assert] UINavigationBar对于UINavigationController解码为解锁,或者navigationBar委托设置不正确。配置不一致可能会导致问题。navigationController =〈UINavigationController:0x123024000〉,导航栏=〈UI导航栏:0x12140a0a0;帧=(0 47; 0 50);不透明=否;自动调整大小= W;图层=〈CALayer:0x6000030afae0〉〉委托= 0x123024000
2023 - 01 - 04 18:34:04.468831 + 0300图纸[45460:449613]金属API确认已启用
2023 - 01 - 04 18:34:04.705019 + 0300工程图[45460:449613][工具选择器]缺少默认字典,无法恢复以下项的状态:PK调色板命名默认值
2023 - 01 - 04 18:35:00.196200 + 0300图纸[45460:449613]键盘无法显示视图控制器(尝试显示〈UIColorPickerViewController:0x121846e00〉)
2条答案
按热度按时间wgeznvg71#
toolPicket
在超出方法范围时释放。您应该在ViewController中有toolPicker的示例。uujelgoq2#
我通过改变来解决我的问题
变成
并按照@noppefoxwolf的建议在顶部添加工具选择器