2017-01-29 5 views
1

これが尋ねられたかどうかはわかりませんが、解決策を見つけることができませんでした。私はボタン上にパンニングジェスチャーを実装していますが、そのアイデアは:ボタンはある位置に固定されています。ユーザーがドラッグすると、ボタンのコピーが作成され、ジェスチャーとともに移動します。元のものは最初の位置にとどまります(ビューには2つのボタンがあります)。パンが終了すると、新しいボタンがいくつかの処理に使用され、その後は消えてしまいます(元のボタンはそのままの状態ですので、このプロセス全体が繰り返されます)。現在、私が持っているものは以下の通りです:iOS:ボタンのコピーをドラッグする

private func addPanGesture() { 
    for btn in self.selectors { //selectors is a list of buttons which needs this gesture 
     let pan = UIPanGestureRecognizer(target: self, action:#selector(self.panDetected(_:))) 
     pan.minimumNumberOfTouches = 1 
     pan.maximumNumberOfTouches = 1 
     btn.addGesturerecognizer(pan) 
    } 
} 

@objc private func panDetected(_ panGesture: UIPanGestureRecognizer) { 
    var translation = panGesture.translation(in: view) 
    panGesture.setTranslation(CGPoint(x: 0, y: 0), in: view) 

    var newButton = UIButton() 
    if let initButton = panGesture.view as? UIButton { 
     print ("Button recognized!") // this msg is printed out 
     newButton.center = CGPoint(x: initButton.center.x + translation.x, y: initButton.center.y + translation.y) 
     newButton.setImage(UIImage(named: "somename"), for: .normal) 
    } 

    if panGesture.state == UIGestureRecognizerState.began { 
     self.view.addSubview(newButton) 
    } 
    if panGesture.state == UIGestureRecognizerState.ended { 
     //some other processing 
    } 
    if panGesture.state == UIGestureRecognizerState.changed { 
     self.view.addSubview(newButton) 
    } 
    // printed-out msgs show began, ended, changed states have all been reached 
} 

しかし、新しいボタンは私のビューには表示されません。これを解決する方法を知ってもいいですか?

答えて

0
  1. あなたが作成し、のみ.beganにサブビューとして新しいボタンを追加し、.endedにそれを削除する必要があります。
  2. したがって、新しいボタンへの参照を保持する必要があります。
  3. 新しいボタンの中心を設定していますが、サイズは設定していません。 .frameを設定することができます。
  4. パンジェスチャーに翻訳を設定する必要はありません。 var translation = panGesture.translation(in: view)を取得すると、必要なものすべてが手に入ります。
  5. は、私が唯一のボタンに以下のコードを書いていますが、あなたは、ボタンの同時ドラッグを許可しようとしている場合は、移動ボタンのリストを維持する代わりに var movingButton: UIButton?

    private func addPanGesture() { 
         let pan = UIPanGestureRecognizer(target: self, action:#selector(self.panDetected(_:))) 
         pan.minimumNumberOfTouches = 1 
         pan.maximumNumberOfTouches = 1 
         btn.addGestureRecognizer(pan) 
        } 
    
        @objc private func panDetected(_ panGesture: UIPanGestureRecognizer) { 
         let translation = panGesture.translation(in: view) 
    
         let initButton = panGesture.view as! UIButton 
    
    
         if panGesture.state == UIGestureRecognizerState.began { 
          // this is just copying initial button 
          // this might be overkill 
          // just make sure you set the frame, title and image of the new button correctly 
          let initButtonData = NSKeyedArchiver.archivedData(withRootObject: initButton) 
          let newButton = NSKeyedUnarchiver.unarchiveObject(with: initButtonData) as! UIButton 
    
          // we store new button's reference since we will just move it while it is added to view 
          movingButton = newButton 
          self.view.addSubview(movingButton!) 
    
         } 
         if panGesture.state == UIGestureRecognizerState.ended { 
          //some other processing 
    
          // when we are done just we just remove it from superview 
          movingButton!.removeFromSuperview() 
         } 
         if panGesture.state == UIGestureRecognizerState.changed { 
    
          // at any change, all we need to do is update movingButton's frame 
          var buttonFrame = initButton.frame; 
          buttonFrame.origin = CGPoint(x: buttonFrame.origin.x + translation.x, y: buttonFrame.origin.y + translation.y) 
          movingButton!.frame = buttonFrame 
         } 
        } 
    
する必要があります
0

それをデバッグせずに言うのは難しいが、私が見るいくつかのこと:

あなたはpanDetectedを通じて新しいボタンを毎回作成し、ビューにするたびにそれを追加します。 .began状態でのみボタンを追加してください。

init(frame:)を使用してボタンを作成し、画像のサイズに合わせて初期化する必要があります。

パンのジェスチャーをボタンに付けているようです。次に、ボタンの座標系でパン座標を取得しますが、これは意味をなさないものです。パンジェスチャーをボタンのスーパービューの座標系に変換する必要があります。パンジェスチャーの状態が.beganの場合を除いてsetTranslationを呼び出すべきではありません。

「1st.changed」メッセージが表示されるたびに、ボタンの座標をパンジェスチャの新しい位置に設定する必要があります。