判断是否拖动单元格路径==节header路径
Determine if dragging cell path == section header path
我正在实现自定义拖放,我想要的是创建单元格的副本(只是用于显示的图像)
当此副本的位置与 header 相同时,我想更改 header 的背景颜色,如果位置超出 header 框架,则恢复其背景颜色再次.
我无法确定正确的路径,到目前为止我得到了这个:
var draggedCellIndexPath: NSIndexPath?
var draggingView: UIView?
var sectionCell: UICollectionReusableView?
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer)
{
let touchLocation = longPressRecognizer.locationInView(self.collectionView)
switch (longPressRecognizer.state) {
case UIGestureRecognizerState.Began:
draggedCellIndexPath = self.collectionView!.indexPathForItemAtPoint(touchLocation)
break;
case UIGestureRecognizerState.Changed:
if draggedCellIndexPath != nil {
draggingView!.center = CGPoint(x: touchLocation.x + touchOffsetFromCenterOfCell!.x, y: touchLocation.y + touchOffsetFromCenterOfCell!.y)
if !isAutoScrolling {
let scroller = self.shouldAutoScroll(touchLocation)
if (scroller.shouldScroll) {
self.autoScroll(scroller.direction)
}
}
let currentTouchLocation = self.longPressRecognizer.locationInView(self.collectionView!.superview)
draggedCellIndexPathOnLocation = self.collectionView!.indexPathForItemAtPoint(currentTouchLocation)
let attributes = self.collectionView?.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation!)
if draggedCellIndexPathOnLocation != nil
{
print("section \(draggedCellIndexPathOnLocation!.section)")
if attributes!.frame.intersects(draggingView!.bounds)
{
print("section number: \(draggedCellIndexPathOnLocation!.section)")
print("section is here")
}
break;
case UIGestureRecognizerState.Ended:
break;
default: ()
}
}
我在逻辑上缺少什么?
所以这是我想出的实现,基本上紫色是你的高亮状态(意味着你的单元格副本在部分 header 的框架内),白色是默认颜色header.
部分
奇怪的是单元格只在某个重叠位置突出显示,这有点奇怪
var sectionCell: UICollectionReusableView?
var tempSectionIndex = Int()
var tempPath: NSIndexPath?
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer)
{
// get the current location inside the view
let touchLocation = longPressRecognizer.locationInView(self.collectionView)
switch (longPressRecognizer.state) {
case UIGestureRecognizerState.Began:
// get indexPath from location
draggedCellIndexPath = self.collectionView!.indexPathForItemAtPoint(touchLocation)
if draggedCellIndexPath != nil {
// get cel for indexPath and create visual copy of the cell
let draggedCell = self.collectionView!.cellForItemAtIndexPath(draggedCellIndexPath!) as UICollectionViewCell!
draggingView = UIImageView(image: getRasterizedImageCopyOfCell(draggedCell))
draggingView!.center = draggedCell.center
self.collectionView!.addSubview(draggingView!)
// put copy cell on screen with animation
touchOffsetFromCenterOfCell = CGPoint(x: draggedCell.center.x - touchLocation.x, y: draggedCell.center.y - touchLocation.y)
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.draggingView!.transform = CGAffineTransformMakeScale(0.8, 0.8)
self.draggingView!.alpha = 0.8
})
}
break;
case UIGestureRecognizerState.Changed:
if draggedCellIndexPath != nil {
// update copy cell position
draggingView!.center = CGPoint(x: touchLocation.x + touchOffsetFromCenterOfCell!.x, y: touchLocation.y + touchOffsetFromCenterOfCell!.y)
if !isAutoScrolling {
let scroller = self.shouldAutoScroll(touchLocation)
if (scroller.shouldScroll) {
self.autoScroll(scroller.direction)
}
}
if let draggedCellIndexPathOnLocation = self.collectionView?.indexPathForItemAtPoint(touchLocation)
{
if tempPath != nil && tempPath?.section != draggedCellIndexPathOnLocation.section {
sectionCell!.backgroundColor = UIColor.whiteColor()
}
// get header section attributes for current indexPath
if let attributes = self.collectionView?.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation)
{
// get header section cell for current indexPath
if let tempSectionCell = self.collectionView?.supplementaryViewForElementKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation)
{
// check intersection between copy cell and header section cell
if attributes.frame.intersects(draggingView!.frame)
{
tempSectionCell.backgroundColor = UIColor.purpleColor()
}
else
{
tempSectionCell.backgroundColor = UIColor.whiteColor()
}
// set temp variables to keep path and section cell
sectionCell = tempSectionCell
tempPath = draggedCellIndexPathOnLocation
}
}
}
}
break;
case UIGestureRecognizerState.Ended:
// delete copy cell and reset variables
if draggingView != nil
{
self.draggingView!.removeFromSuperview()
}
self.draggingView = nil
self.draggedCellIndexPath = nil
self.isAutoScrolling = false
sectionCell!.backgroundColor = UIColor.whiteColor()
break;
default: ()
}
}
我正在实现自定义拖放,我想要的是创建单元格的副本(只是用于显示的图像) 当此副本的位置与 header 相同时,我想更改 header 的背景颜色,如果位置超出 header 框架,则恢复其背景颜色再次.
我无法确定正确的路径,到目前为止我得到了这个:
var draggedCellIndexPath: NSIndexPath?
var draggingView: UIView?
var sectionCell: UICollectionReusableView?
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer)
{
let touchLocation = longPressRecognizer.locationInView(self.collectionView)
switch (longPressRecognizer.state) {
case UIGestureRecognizerState.Began:
draggedCellIndexPath = self.collectionView!.indexPathForItemAtPoint(touchLocation)
break;
case UIGestureRecognizerState.Changed:
if draggedCellIndexPath != nil {
draggingView!.center = CGPoint(x: touchLocation.x + touchOffsetFromCenterOfCell!.x, y: touchLocation.y + touchOffsetFromCenterOfCell!.y)
if !isAutoScrolling {
let scroller = self.shouldAutoScroll(touchLocation)
if (scroller.shouldScroll) {
self.autoScroll(scroller.direction)
}
}
let currentTouchLocation = self.longPressRecognizer.locationInView(self.collectionView!.superview)
draggedCellIndexPathOnLocation = self.collectionView!.indexPathForItemAtPoint(currentTouchLocation)
let attributes = self.collectionView?.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation!)
if draggedCellIndexPathOnLocation != nil
{
print("section \(draggedCellIndexPathOnLocation!.section)")
if attributes!.frame.intersects(draggingView!.bounds)
{
print("section number: \(draggedCellIndexPathOnLocation!.section)")
print("section is here")
}
break;
case UIGestureRecognizerState.Ended:
break;
default: ()
}
}
我在逻辑上缺少什么?
所以这是我想出的实现,基本上紫色是你的高亮状态(意味着你的单元格副本在部分 header 的框架内),白色是默认颜色header.
部分奇怪的是单元格只在某个重叠位置突出显示,这有点奇怪
var sectionCell: UICollectionReusableView?
var tempSectionIndex = Int()
var tempPath: NSIndexPath?
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer)
{
// get the current location inside the view
let touchLocation = longPressRecognizer.locationInView(self.collectionView)
switch (longPressRecognizer.state) {
case UIGestureRecognizerState.Began:
// get indexPath from location
draggedCellIndexPath = self.collectionView!.indexPathForItemAtPoint(touchLocation)
if draggedCellIndexPath != nil {
// get cel for indexPath and create visual copy of the cell
let draggedCell = self.collectionView!.cellForItemAtIndexPath(draggedCellIndexPath!) as UICollectionViewCell!
draggingView = UIImageView(image: getRasterizedImageCopyOfCell(draggedCell))
draggingView!.center = draggedCell.center
self.collectionView!.addSubview(draggingView!)
// put copy cell on screen with animation
touchOffsetFromCenterOfCell = CGPoint(x: draggedCell.center.x - touchLocation.x, y: draggedCell.center.y - touchLocation.y)
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.draggingView!.transform = CGAffineTransformMakeScale(0.8, 0.8)
self.draggingView!.alpha = 0.8
})
}
break;
case UIGestureRecognizerState.Changed:
if draggedCellIndexPath != nil {
// update copy cell position
draggingView!.center = CGPoint(x: touchLocation.x + touchOffsetFromCenterOfCell!.x, y: touchLocation.y + touchOffsetFromCenterOfCell!.y)
if !isAutoScrolling {
let scroller = self.shouldAutoScroll(touchLocation)
if (scroller.shouldScroll) {
self.autoScroll(scroller.direction)
}
}
if let draggedCellIndexPathOnLocation = self.collectionView?.indexPathForItemAtPoint(touchLocation)
{
if tempPath != nil && tempPath?.section != draggedCellIndexPathOnLocation.section {
sectionCell!.backgroundColor = UIColor.whiteColor()
}
// get header section attributes for current indexPath
if let attributes = self.collectionView?.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation)
{
// get header section cell for current indexPath
if let tempSectionCell = self.collectionView?.supplementaryViewForElementKind(UICollectionElementKindSectionHeader, atIndexPath: draggedCellIndexPathOnLocation)
{
// check intersection between copy cell and header section cell
if attributes.frame.intersects(draggingView!.frame)
{
tempSectionCell.backgroundColor = UIColor.purpleColor()
}
else
{
tempSectionCell.backgroundColor = UIColor.whiteColor()
}
// set temp variables to keep path and section cell
sectionCell = tempSectionCell
tempPath = draggedCellIndexPathOnLocation
}
}
}
}
break;
case UIGestureRecognizerState.Ended:
// delete copy cell and reset variables
if draggingView != nil
{
self.draggingView!.removeFromSuperview()
}
self.draggingView = nil
self.draggedCellIndexPath = nil
self.isAutoScrolling = false
sectionCell!.backgroundColor = UIColor.whiteColor()
break;
default: ()
}
}