Help us understand the problem. What is going on with this article?

UIPanGestureRecognizerでDragする。

More than 3 years have passed since last update.

iOSアプリでドラッグ&ドロップしてみる件についてをSwiftでやってみました。

環境

  • iOS: 8.4.1
  • Xcode6
  • ProjectTemplate: Single View Application

Objectの用意

ドラッグするためのオブジェクトを用意します。今回はViewを置きました。右のピンクのViewで説明します!
a.png

ドラッグの実装

まず、右下のオブジェクトがたくさんあるところから、Pan Gesture RecognizerをドラッグしたいViewにドラッグアンドドロップします。
そしたら、このタッチイベントの宣言とアクション紐付けします。
Main.storyboardでViewController.swiftと2画面開いて、OutletとActionで紐付けします。
typeをUIPanGestureRecognizerにしてnameはpanGestureにしてあります。

ViewController.swift
    @IBOutlet var panGesture: UIPanGestureRecognizer! //紐付けすると出てきます。

    @IBAction func dragGesture(sender: UIPanGestureRecognizer) { //こちらも紐付けによって

        var point: CGPoint = sender.translationInView(self.view)
        var movedPoint: CGPoint = CGPointMake(sender.view!.center.x + point.x,
        sender.view!.center.y + point.y)
        sender.view!.center = movedPoint
        sender.setTranslation(CGPointZero, inView: self.view)
    }

初めの方の紐付けがちゃんとできてれば動く!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした