问题描述
我有一个拖放功能(代码如下)为我的表工作很好。除此之外,我无法将单元格拖动到未显示在屏幕/视图中的位置。
换句话说,我还没有弄清楚如何使自动滚屏。
有人可以看看我的代码,并知道在用户将单元格拖动到视图的顶部还是底部时,添加代码的代码可以自动滚动吗?
func animateCellDrag(gestureRecognizer:UIGestureRecognizer,tableView:UITableView,whichList:String){
let longPress = gestureRecognizer as ! UILongPressGestureRecognizer
let state = longPress.state
var locationInView = longPress.locationInView(tableView)
var indexPath = tableView.indexPathForRowAtPoint(locationInView)
var count = 0
struct我的{
static var cellSnapshot:UIView? = nil
}
struct Path {
static var initialIndexPath:NSIndexPath? = nil
}
如果indexPath!= nil {
//采取单元格快照的步骤。在switch语句中调用的函数
func snapshotOfCell(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
}
switch state {
case UIGestureRecognizerState.Began:
//调用上述函数拍摄被保持的单元格的快照,动画弹出
//当单元格上长按手势开始时运行
if indexPath!= nil {
Path.initialIndexPath = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath!)as UITableViewCell!
My.cellSnapshot = snapshotOfCell(cell)
var center = cell.center
My.cellSnapshot!.center = center
My.cellSnapshot!.alpha = 0.0
tableView.addSubview(My.cellSnapshot!)
UIView.animateWithDuration(0.25,动画:{() - > 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
},完成:{(已完成) - >
如果已完成{
cell.hidden = true
}
})
}
case UIGestureRecognizerState.Changed:
如果My.cellSnapshot!= nil || indexPath!= nil {
//当用户放开单元格时运行
//将快照单元的CG Y坐标设置为表中当前位置的中心(捕捉到位)
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center
let appDel:AppDelegate =(UIApplication。 sharedApplication()。delegate as!AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:whichList)
let sortDescriptor = NSSortDescriptor(key:displayOrder,ascending:true)
fetchRequest.sortDescriptors = [sortDescriptor]
//如果indexPath不为0 AND与开始不同(没有移动):更新数组和表行顺序
if((indexPath!= nil)&&(indexPath!= Path.initialIndexPath)&((indexPath?.row< taskList_Cntxt.count)){
switch whichList {
caseTodayTask,TomTask,TBDTask FinTask
swap(& taskList_Cntxt [indexPath!.row],& taskList_Cntxt [Path.initialIndexPath!.row])
tableView.moveRowAtIndexPath(Path.initialIndexPath !, toIndexPath:indexPath!)
toolBox.updateDisplayOrder()
默认值:
swap(& curLifeList_Context [indexPath!.row],& curLifeList_Context [Path.initialIndexPath!.row])
tableView.moveRowAtIndexPath(Path.initialIndexPath !, toIndexPath:indexPath!)
toolBox。 llvc_updateDisplayOrder()
}
do {
try context.save()
} catch _ {print(拖放错误。状态:更改)}
Path.initialIndexPath = indexPath
}
}
默认值:
如果My.cellSnapshot!= nil || indexPath! = nil {
//长时间识别时持续运行$动画动画
让cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!)作为UITableViewCell!
单元格。 hidden = false
cell.alpha = 0.0
UIView.animateWithDuration(0.25,动画:{() - > Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
},完成:{(finished) - > Void in
如果完成{
//完成块删除sn apshot的单元格,清理所有东西
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})//竞赛结束动画结束
} //结束'如果nil'
} //结束开关
} // end of longPress if-nil
} // end of longPressGestureRecognized
我可以使用以下代码解决: p>
if locationOnScreen.y< 200&& indexPath!.row> 1 {
let indexPath = NSIndexPath(forRow:indexPath!.row-1,inSection:0)
tableView.scrollToRowAtIndexPath(indexPath,atScrollPosition:UITableViewScrollPosition.Top,animated:false)
} else如果locationOnScreen.y> 550&& indexPath!.row< listCount {
let indexPath = NSIndexPath(forRow:indexPath!.row + 1,inSection:0)
tableView.scrollToRowAtIndexPath(indexPath,atScrollPosition:UITableViewScrollPosition.Bottom,animated:false)
}
其中:
var locationOnScreen = longPress.locationInView(tableView.superview)
listCount = listArrayOrContext.count
插入交换机案例:
case UIGestureRecognizerState.Changed:
/ pre>
我将动画设置为false,否则我会非常干活,乱码。如果有人知道如何慢慢逐渐滚动,请张贴。
I have a drag and drop function (code below) for my table that works great. Except, I can't drag a cell to a location not showing on screen / in view.
In other words, I haven't figured out how to make it autoscroll.
Is anyone able to look at my code and know where to add code that enables autoscrolling when the user drags the cell towards the top or bottom of the view?
func animateCellDrag (gestureRecognizer: UIGestureRecognizer, tableView: UITableView, whichList: String) { let longPress = gestureRecognizer as! UILongPressGestureRecognizer let state = longPress.state var locationInView = longPress.locationInView(tableView) var indexPath = tableView.indexPathForRowAtPoint(locationInView) var count = 0 struct My { static var cellSnapshot : UIView? = nil } struct Path { static var initialIndexPath : NSIndexPath? = nil } if indexPath != nil { //Steps to take a cell snapshot. Function to be called in switch statement func snapshotOfCell(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 } switch state { case UIGestureRecognizerState.Began: //Calls above function to take snapshot of held cell, animate pop out //Run when a long-press gesture begins on a cell if indexPath != nil { Path.initialIndexPath = indexPath let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell! My.cellSnapshot = snapshotOfCell(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: if My.cellSnapshot != nil || indexPath != nil { //Runs when the user "lets go" of the cell //Sets CG Y-Coordinate of snapshot cell to center of current location in table (snaps into place) var center = My.cellSnapshot!.center center.y = locationInView.y My.cellSnapshot!.center = center let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) let context: NSManagedObjectContext = appDel.managedObjectContext! let fetchRequest = NSFetchRequest(entityName: whichList) let sortDescriptor = NSSortDescriptor(key: "displayOrder", ascending: true ) fetchRequest.sortDescriptors = [ sortDescriptor ] //If the indexPath is not 0 AND is not the same as it began (didn't move): Update array and table row order if ((indexPath != nil) && (indexPath != Path.initialIndexPath) && (indexPath?.row < taskList_Cntxt.count)) { switch whichList { case "TodayTask", "TomTask", "TBDTask", "FinTask": swap(&taskList_Cntxt[indexPath!.row], &taskList_Cntxt[Path.initialIndexPath!.row]) tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) toolBox.updateDisplayOrder() default: swap(&curLifeList_Context[indexPath!.row], &curLifeList_Context[Path.initialIndexPath!.row]) tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) toolBox.llvc_updateDisplayOrder() } do { try context.save() } catch _ { print("Drag and drop error. State: Changed")} Path.initialIndexPath = indexPath } } default: if My.cellSnapshot != nil || indexPath != nil { //Runs continuously while a long press is recognized / Animates cell movement 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 { //Completion block removes snapshot of cell, cleans everything up Path.initialIndexPath = nil My.cellSnapshot!.removeFromSuperview() My.cellSnapshot = nil } })//End of competion block & end of animation }//End of 'if nil' }//End of switch }//End of longPress if-nil }//End of longPressGestureRecognized
解决方案I was able to resolve it using this code:
if locationOnScreen.y < 200 && indexPath!.row > 1 { let indexPath = NSIndexPath(forRow: indexPath!.row-1, inSection: 0) tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false) } else if locationOnScreen.y > 550 && indexPath!.row < listCount { let indexPath = NSIndexPath(forRow: indexPath!.row+1, inSection: 0) tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false) }
Where:
var locationOnScreen = longPress.locationInView(tableView.superview) listCount = listArrayOrContext.count
Inserted in the switch case:
case UIGestureRecognizerState.Changed:
I set animate to false because otherwise I got very jerky, messy scrolling. If anyone figures out how to get a slow, gradual scroll, please post it.
这篇关于拖曳Drop Works - 自动滚屏不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!