Skip to main content
2 / 2
本文に 67 文字追加
h2so5
  • 4,103
  • 2
  • 16
  • 36

ViewControllerにコードを追加する必要がありますが、このような方法はどうでしょうか。
ボタンが3つある場合のサンプルです。NSLog("button released: %d", index)の部分で最終的にタッチを離した位置のボタン番号を取得しています。

class ViewController: UIViewController {
  @IBOutlet weak var button0: TestButton!
  @IBOutlet weak var button1: TestButton!
  @IBOutlet weak var button2: TestButton!
  var buttons: [UIButton] = []

  override func viewDidLoad() {
    super.viewDidLoad()
    self.buttons = [button0, button1, button2]
    for button: UIButton in buttons {
      button.addTarget(self, action: "buttonTouchDrag:event:", forControlEvents: UIControlEvents.TouchDragInside)
      button.addTarget(self, action: "buttonTouchDrag:event:", forControlEvents: UIControlEvents.TouchDragOutside)
      button.addTarget(self, action: "buttonTouchUp:event:", forControlEvents: UIControlEvents.TouchUpInside)
      button.addTarget(self, action: "buttonTouchUp:event:", forControlEvents: UIControlEvents.TouchUpOutside)
    }
  }

  func buttonTouchDrag(sender:UIButton, event: UIEvent) {
    let point = event.allTouches()?.anyObject()?.locationInView(self.view);
    for button: UIButton in buttons {
      if CGRectContainsPoint(button.convertRect(button.bounds, toView: self.view), point!) {
        button.sendActionsForControlEvents(UIControlEvents.TouchDragEnter);
      } else {
        button.sendActionsForControlEvents(UIControlEvents.TouchDragExit);
      }
    }
  }
  
  func buttonTouchUp(sender:UIButton, event: UIEvent) {
    let point = event.allTouches()?.anyObject()?.locationInView(self.view);
    for (index, button) in enumerate(buttons) {
      if CGRectContainsPoint(button.convertRect(button.bounds, toView: self.view), point!) {
        NSLog("button released: %d", index)
      }
      button.sendActionsForControlEvents(UIControlEvents.TouchDragExit);
    }
  }
}

Screenshot

h2so5
  • 4,103
  • 2
  • 16
  • 36