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
}
나는 사용자 정의보기의 문제 사용을 해결하고 드래그를 제어하는 이벤트 핸들러를 정의합니다. –