2016-10-10 11 views
0

私はSwiftとIOSに慣れています。私はパンのジェスチャーを使ってラベルを移動しています。終了したら、自動的に新しいラベルを追加します。私は汎関数の最後に関数を呼び出そうとしましたが、新しいラベルが追加されたときに移動したラベルは画面外に移動します。私は "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

前self.viewを置きます。 –

+0

あなたのコードを表示すると、私はあなたを助けることができます –

+1

ようこそ! [ask]と[mcve]を参照してください。 – Mat

答えて

0

addSubviewがあなたのために作成した関数である:

がうまくいけば、この問題を説明するために必要なコードですか? ない場合は、我々はより多くのコード、パンジェスチャーのためのあなたのセレクタの郵便番号が必要になり

self.view.addSubview(shapeView) 
+0

助けてくれてありがとうが、それもうまくいきませんでした。 –

関連する問題