2015-07-12 55 views
0

我正在使用tableView,需要通過拖放來啓用交換項目。使用Swift 2.0拖放交換

我主要是在那裏,但卡住了一個問題。拖動到的項目未與從中拖出的項目交換。相反,將新的拖動項目添加到現有項目的頂部,它只是將現有項目向下移動一行。

case UIGestureRecognizerState.Ended: 
      var center = My.cellSnapshot!.center 
      center.y = locationInView.y 
      My.cellSnapshot!.center = center 

      if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) { 
       //check to see if this is a valid place to swap 
       var numberOfSlots:Int = 0 //the number of valid positions to swap 
       numberOfSlots = thePlayers[Path.initialIndexPath!.row].SwapPosition.count 
       for index in 1...numberOfSlots { 
        if thePlayers[Path.initialIndexPath!.row].SwapPosition[index - 1] == thePlayers[indexPath!.row].PlayerPosition { 
         //this is a valid swap 
         print("Swap it!") 
         OkToSwap = true 
         break 
        } 
        else { 
         print (thePlayers[Path.initialIndexPath!.row].SwapPosition[index - 1]) 
        } 
       } 
      } 
      if OkToSwap == true { //if this is a valid slot for a swap, then swap it. 
        // swap(&itemsArray[indexPath!.row], &itemsArray[Path.initialIndexPath!.row]) 
        //***TODO: Add Webservice call to swap on the website*** 

        //move the selectedRow to the original (dragged from) position 
        tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) 
        My.cellSnapshot?.alpha = 0.0 
        let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell! 
        cell.alpha = 1 

      } 
        //put it back 
        let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell! 
        cell.hidden = false 
        cell.alpha = 0.0 
        UIView.animateWithDuration(0.25, animations: {() -> Void in 
         My.cellSnapshot!.center = originalCenter 
         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 
          } 
        }) 

     default: 
      let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell! 
      cell.hidden = false 
      cell.alpha = 0.0 
      // Clean up. 

      UIView.animateWithDuration(0.25, 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 
        } 
      }) 
     } 
    } 

任何想法如何做到使用雨燕2.0行的真正的交換 - 我發現了一些簡單的代碼片段,但語法變化使他們無法工作,我被困。

回答

1

我能得到與雨燕2.0

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 = 5.0 
    cellSnapshot.layer.shadowOpacity = 0.4 
    return cellSnapshot 
} 

// MARK: Gesture Methods 
func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) { 
    let longPress = gestureRecognizer as! UILongPressGestureRecognizer 

    let state = longPress.state 

    var locationInView = longPress.locationInView(tableView) 

    var 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 
        } 
      }) 
     } 
     break 
    case UIGestureRecognizerState.Changed: 
     var center = My.cellSnapshot!.center 
     center.y = locationInView.y 
     My.cellSnapshot!.center = center 
     if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) { 
      //------=-=-=-=[  
      // This line errors when it is uncommented. When I comment it out, the error is gone, 
      // and the cells /do/ reorder.. ¯\_(ツ)_/¯ 
      //swap(&itemsArray[indexPath!.row], &itemsArray[Path.initialIndexPath!.row]) 
      tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) 
      Path.initialIndexPath = indexPath 
     } 
     break 
    default: 
     let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell! 
     cell.hidden = false 
     cell.alpha = 0.0 
     UIView.animateWithDuration(0.25, 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 
       } 
     }) 
     break 

    }//.switch 


}// .longPressGestureRecognized 
該位工作