2017-01-24 2 views
0

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 
} 
+0

나는 사용자 정의보기의 문제 사용을 해결하고 드래그를 제어하는 ​​이벤트 핸들러를 정의합니다. –

답변

0

UIGestureRecognizerhitTest() 갈 방법은 다음과 같습니다

여기에 그리기 텍스트 내 코드입니다. 내 코드는 이미지 뷰 내부의 레이어를 원한다고 가정하는 골격입니다. 그러나 당신이 원한다면 그들은 superview의 일부가 될 수 있습니다.

다시 말하지만 레이어를 설정하는 방법에주의하십시오. 이동할 레이어가 여러 개인 경우 특별히 액세스해야합니다. 내 코드는 당신의 기능을 사용하려는 시도였다. 어쩌면 당신은 얼마나 많은 배열이 있는지 알지 못한다면 배열에 배열을 저장하거나 imageView 나 뷰 컨트롤러에 전역 변수로 저장할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. 그러나 2 개의 레이어가 같은 위치에 겹쳐 있다면 어떤 레이어가 움직일 것입니까? –

+0

사실,이 하나의 https://youtu.be/qdtzZGn7NaM?t=14m2s와 같은 텍스트 그리기 기능을 구현하고 싶습니다. 텍스트 주위에 테두리 모양이 있고 사용자가 드래그, 편집 또는 삭제할 수 있습니다. 레이어의 문제 사용을 해결할 수 있습니까? –

관련 문제