CATextLayerをUIImageViewレイヤーに追加した後にドラッグしてレイヤーを移動する方法。私はUIImageViewに複数のレイヤーを追加します。では、どのレイヤをクリックして別の位置に移動させるのかをどのように知ることができますか?それについてですドラッグしてレイヤーを移動する方法は?
@IBOutlet weak var imageView: ImageView!
let panGesture = UIPanGestureRecognizer()
var newLayer = CATextLayer()
// this is your code above, be careful, you want each layer "named"!
nextLayer = addText()
imageView.layer.addSubLayer(nextLayer)
imageView.addGestureRecognizer(panGesture)
func moveLayer(_ recognizer:UIPanGestureRecognizer) {
let p = recognizer.location(in: self)
if newLayer.hitTest(p) != nil {
newLayer.position = p
}
}
:
func addText() -> CATextLayer{
let rect:CGRect = CGRect(x: 50, y: 600, width: 120, height: 60)
let myAttributes = [
NSFontAttributeName: UIFont(name: "HelveticaNeue-Thin", size:18)!,
NSForegroundColorAttributeName: UIColor.black.cgColor
] as [String : Any]
let myAttributedString = NSAttributedString(string: "TEST STRING", attributes: myAttributes)
let textLayer = CATextLayer()
textLayer.frame = rect
textLayer.string = myAttributedString
textLayer.position = CGPoint(x:150,y:400)
textLayer.alignmentMode = kCAAlignmentCenter
return textLayer
}
私はカスタムビューの問題を解決し、ドラッグを制御するイベントハンドラを定義しました。 –