0

ViewController で tableView を使用してアプリに取り組んでいます。この tableView にはセクションがあり、ユーザーがUILongPressGestureRecognizerを使用してセクション間でセルを長押し、ドラッグ アンド ドロップできるようにしたいと考えています。

次のコードでは、ジェスチャ認識エンジンが機能しますが、セルをドラッグして tableView 内の任意の場所にドロップしようとすると、アプリがクラッシュします。クラッシュは 2 つの異なる行で発生します。セルを長押ししてセクション内tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) にドラッグすると、この行でクラッシュします: セクションの外にドラッグしようとすると、この行でクラッシュします:swap(&Categories.categories[indexPath!.row], &Categories.categories[Path.initialIndexPath!.row])

(各セクションと行のデータを含む配列は、カテゴリという名前の別のクラスにあります。セクションを含む配列は「セクション」と呼ばれ、行を含む配列は「カテゴリ」と呼ばれます。)

なぜ失敗したのか、どうすれば修正できるのかを知る手助けをいただければ幸いです。

func setupLongPress() {
    let longpress = UILongPressGestureRecognizer(target: self, action:   "longPressGestureRecognized:")
    tableView.addGestureRecognizer(longpress)
}

func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
    let longPress = gestureRecognizer as! UILongPressGestureRecognizer
    let state = longPress.state
    let locationInView = longPress.locationInView(tableView)
    let indexPath = tableView.indexPathForRowAtPoint(locationInView)

    struct My {
        static var cellSnapshot : UIView? = nil
    }
    struct Path {
        static var initialIndexPath : NSIndexPath? = nil
    }

    switch state {
    case UIGestureRecognizerState.Began:
        if indexPath != nil {
            Path.initialIndexPath = indexPath
            let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
            My.cellSnapshot  = snapshopOfCell(cell)
            var center = cell.center
            My.cellSnapshot!.center = center
            My.cellSnapshot!.alpha = 0.0

            tableView.addSubview(My.cellSnapshot!)

            UIView.animateWithDuration(0.25, animations: { () -> Void in
                center.y = locationInView.y
                My.cellSnapshot!.center = center
                My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)
                My.cellSnapshot!.alpha = 0.98
                cell.alpha = 0.0
                }, completion: { (finished) -> Void in
                    if finished {
                        cell.hidden = true
                }
            })
        }

    case UIGestureRecognizerState.Changed:
        var center = My.cellSnapshot!.center
        center.y = locationInView.y
        My.cellSnapshot!.center = center
        if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
            swap(&Categories.categories[indexPath!.row], &Categories.categories[Path.initialIndexPath!.row])
            tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
            Path.initialIndexPath = indexPath
        }

    default:
        let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
        cell.hidden = false
        cell.alpha = 0.0
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            My.cellSnapshot!.center = cell.center
            My.cellSnapshot!.transform = CGAffineTransformIdentity
            My.cellSnapshot!.alpha = 0.0
            cell.alpha = 1.0
            }, completion: { (finished) -> Void in
                if finished {
                    Path.initialIndexPath = nil
                    My.cellSnapshot!.removeFromSuperview()
                    My.cellSnapshot = nil
            }
        })
    }
}

func snapshopOfCell(inputView: UIView) -> UIView {
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
    inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()
    let cellSnapshot : UIView = UIImageView(image: image)
            cellSnapshot.layer.masksToBounds = false
            cellSnapshot.layer.cornerRadius = 0.0
            cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
            cellSnapshot.layer.shadowRadius = 1.0
            cellSnapshot.layer.shadowOpacity = 0.2
    return cellSnapshot
}

ありがとうございました!

4

0 に答える 0