2016-10-10 72 views
0

請知道我是Swift和IOS的新手。我正在使用平移手勢來移動標籤。完成後,我想自動添加新標籤。我試圖在pan函數的末尾調用函數,但添加新標籤時,我移動的標籤會從屏幕上移開。我試圖使用「panGR.state.rawValue == 3」,「panGR.state == UIGestureRecognizerState.ended」和一個計時器 - 沒有成功。請幫忙。如何在平移手勢後立即調用某個函數?

但願這是需要解釋的問題代碼:

func initGestureRecognizers() { 

    let panGR = UIPanGestureRecognizer(target: self, action: 
    #selector(ShapeView.didPan(_:))) 
    addGestureRecognizer(panGR) 

} 

func didPan(_ panGR: UIPanGestureRecognizer) { 

    self.superview!.bringSubview(toFront: self) 
    var size: CGFloat = 53 
    var translation = panGR.translation(in: self) 
    translation = translation.applying(self.transform) 
    self.center.x += translation.x 
    self.center.y += translation.y 
    panGR.setTranslation(CGPoint.zero, in: self) 

    if panGR.state.rawValue == 1 { // save the last position 
     let startX = self.center.x 
     let startY = self.center.y 
    } 

    if panGR.state.rawValue == 3 { // when move is finished 

     // this will remove ability to move tile 
     removeGestureRecognizer(panGR) 

     // the following code should replace the moved tile, 
     // with a new tile, but leave the moved tile where it is. 

     // BUT - it moves the moved tile to coordinates (x=0,y=0), if 
     // the following 2 lines are executed. 

     self.center.x = 0 
     self.center.y = 0 

     // if the above 2 lines are not executed, the new tile 
     // does not get displayed 

     replaceMovedTile() 

    } 
} 

func replaceMovedTile() { 
     // position new tile 
     let tapPoint = CGPoint(x: 46 + 22, y: 596 + 22) 
     let shapeView = ShapeView(origin: tapPoint) 
     addSubview(shapeView) 
} 
+0

我們需要更多的代碼,您的選擇器的平移手勢代碼。 –

+0

顯示你的代碼,然後我可以幫你 –

+1

歡迎來到堆棧溢出!請參閱[問]和[mcve]。 – Mat

回答

0

addSubview是爲您創建一個功能? 如果沒有,請將self.view放在

之前
self.view.addSubview(shapeView) 
+0

感謝您的幫助,但那也沒有奏效。 –

相關問題