- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,我可以在其中跟踪我的青年足球队的替补情况。
遵循 Payal Gupta 的教程在拖放到集合和表格中时,我设法在两个 Collection View (PlayersOntoTheField 和 Substitutes)之间进行拖放工作 Screenshot .
当我将一名替补球员拖到我的 Playground 上时,它现在应该会捕捉到预定义的球队阵容(例如屏幕截图中的 3-2-1)。是否可以通过自定义 UICollectionViewLayout 获得此类行为,或者有人有其他建议吗?
非常感谢您的帮助。
丹尼
//Based on a work by:
//Payal Gupta (https://github.com/pgpt10/DragAndDrop-CollectionView)
import UIKit
class ViewController: UIViewController
{
private var substitutes = ["player1", "player2", "player3", "player4"]
private var players = [String]()
@IBOutlet weak var substitutesCollectionView: UICollectionView!
@IBOutlet weak var playersCollectionView: UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
//SubstitutesCollectionView drag and drop configuration
self.substitutesCollectionView.dragInteractionEnabled = true
self.substitutesCollectionView.dragDelegate = self
self.substitutesCollectionView.dropDelegate = self
//PlayersCollectionView drag and drop configuration
self.playersCollectionView.dragInteractionEnabled = true
self.playersCollectionView.dropDelegate = self
self.playersCollectionView.dragDelegate = self
self.playersCollectionView.reorderingCadence = .fast //default value - .immediate
}
//MARK: Private Methods
private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
{
let items = coordinator.items
if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath
{
var dIndexPath = destinationIndexPath
if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
{
dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
}
collectionView.performBatchUpdates({
if collectionView === self.playersCollectionView
{
self.players.remove(at: sourceIndexPath.row)
self.players.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
}
else
{
self.substitutes.remove(at: sourceIndexPath.row)
self.substitutes.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
}
collectionView.deleteItems(at: [sourceIndexPath])
collectionView.insertItems(at: [dIndexPath])
})
coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
}
}
private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
{
collectionView.performBatchUpdates({
var indexPaths = [IndexPath]()
for (index, item) in coordinator.items.enumerated()
{
let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
if collectionView === self.playersCollectionView
{
self.players.insert(item.dragItem.localObject as! String, at: indexPath.row)
}
else
{
self.substitutes.insert(item.dragItem.localObject as! String, at: indexPath.row)
}
indexPaths.append(indexPath)
}
collectionView.insertItems(at: indexPaths)
})
}
}
// MARK: - UICollectionViewDataSource Methods
extension ViewController : UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return collectionView == self.substitutesCollectionView ? self.substitutes.count : self.players.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
if collectionView == self.substitutesCollectionView
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! MyCollectionViewCell
cell.customImageView?.image = UIImage(named: self.substitutes[indexPath.row])
cell.customLabel.text = self.substitutes[indexPath.row].capitalized
return cell
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! MyCollectionViewCell
cell.customImageView?.image = UIImage(named: self.players[indexPath.row])
cell.customLabel.text = self.players[indexPath.row].capitalized
return cell
}
}
}
// MARK: - UICollectionViewDragDelegate Methods
extension ViewController : UICollectionViewDragDelegate
{
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]
{
let item = collectionView == substitutesCollectionView ? self.substitutes[indexPath.row] : self.players[indexPath.row]
let itemProvider = NSItemProvider(object: item as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem]
{
let item = collectionView == substitutesCollectionView ? self.substitutes[indexPath.row] : self.players[indexPath.row]
let itemProvider = NSItemProvider(object: item as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters?
{
if collectionView == substitutesCollectionView
{
let previewParameters = UIDragPreviewParameters()
previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 15, y: 5, width: 30, height: 30))
return previewParameters
}
return nil
}
}
// MARK: - UICollectionViewDropDelegate Methods
extension ViewController : UICollectionViewDropDelegate
{
func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool
{
return session.canLoadObjects(ofClass: NSString.self)
}
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal
{
if collectionView === self.substitutesCollectionView
{
if collectionView.hasActiveDrag
{
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
else
{
return UICollectionViewDropProposal(operation: .forbidden)
}
}
else
{
if collectionView.hasActiveDrag
{
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
else
{
return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
}
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
{
let destinationIndexPath: IndexPath
if let indexPath = coordinator.destinationIndexPath
{
destinationIndexPath = indexPath
}
else
{
// Get last index path of table view.
let section = collectionView.numberOfSections - 1
let row = collectionView.numberOfItems(inSection: section)
destinationIndexPath = IndexPath(row: row, section: section)
}
switch coordinator.proposal.operation
{
case .move:
self.reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
break
case .copy:
self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
default:
return
}
}
}
最佳答案
到目前为止,我对自定义 UICollectionViewLayout 的解决方法是:
import UIKit
class LineUp_3_2_1View: UICollectionViewLayout {
private var center: CGPoint!
private var itemSize: CGSize!
private var radiusOfCircleViews: CGFloat!
private var numberOfItems: Int!
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return }
radiusOfCircleViews = CGFloat(30.0)
itemSize = CGSize(width: radiusOfCircleViews * 2, height: radiusOfCircleViews * 2)
center = CGPoint(x: collectionView.bounds.midX, y: collectionView.bounds.midY)
numberOfItems = collectionView.numberOfItems(inSection: 0)
}
override var collectionViewContentSize: CGSize {
return collectionView!.bounds.size
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
if (indexPath.item == 0) {attributes.center = CGPoint(x: 169, y: 344)}
if (indexPath.item == 1) {attributes.center = CGPoint(x: 46, y: 250)}
if (indexPath.item == 2) {attributes.center = CGPoint(x: 169, y: 250)}
if (indexPath.item == 3) {attributes.center = CGPoint(x: 287, y: 250)}
if (indexPath.item == 4) {attributes.center = CGPoint(x: 80, y: 156)}
if (indexPath.item == 5) {attributes.center = CGPoint(x: 253, y: 156)}
if (indexPath.item == 6) {attributes.center = CGPoint(x: 169, y: 62)}
attributes.size = itemSize
return attributes
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return (0 ..< collectionView!.numberOfItems(inSection: 0))
.flatMap { item -> UICollectionViewLayoutAttributes? in // `compactMap` in Xcode 9.3
self.layoutAttributesForItem(at: IndexPath(item: item, section: 0))
}
}
}
关于swift - 自定义拖放 UICollectionViewLayout 捕捉到网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083273/
我关注了这个 Custom Collection View Layout tutorial从 raywenderlich.com 使用 xcode 8 和 swift 3。 当我在实现教程中要求的所有
假设您有一个带有普通自定义 UICollectionViewLayout 的 UICollectionView。 就是这样 >>> 不是 UICollectionViewLayoutAttribut
谁能指出我如何使用 UICollectionViewLayout 创建类似于 Pinterest 列布局的界面的正确方向? 我尝试在网上搜索,但似乎还没有很多例子。 最佳答案 1000memories
我正在尝试配置 Collection View 以在附图中显示以下自定义布局: 。本质上,我希望能够配置我的 Collection View ,使其看起来基于行而不是列。例如: 第 1 行:3 个大小
在UICollectionView中做这样的UICollectionViewLayout是真的吗?请给我一些想法,我该怎么做? 最佳答案 您可以使用 iCarousel ,用于此目的的第三方自定义控件
当我在没有自定义布局对象的情况下实现 UICollectionView 时,所有 16 个可重用单元格都会出现。也就是说,我有 1 个部分,该部分中有 16 个项目。当我创建自定义布局对象时,仅显示
我目前正在使用完全用 Swift 编码的 TreeMap 框架布局:https://github.com/yahoo/YMTreeMap 我创建了自己的 Layout 类,以便像这样自定义单元格: i
正如标题所说,我需要一个 uicollectionview,它能够如图所示在两个方向上分页。 每个方 block 都是ios屏幕,箭头表示允许分页的地方。 UICollectionViewFlowLa
我有两个单元格尺寸小(间距和插入前大约一半屏幕宽度)和大(插入前全屏宽度),这两种尺寸可以在下图中看到。 我希望 UICollectionView 根据给定的大小属性自动调整这些单元格的大小。我已经设
我正在尝试以编程方式设置 UICollectionViewLayout。我正在使用 UICollectionView,也没有使用 Storyboard,也没有设置其约束。 我关注了Ray Wender
我正在使用 UICollectionView 并尝试使用 UICollectionViewLayout 方法来调整单元格的大小,但它们从未被调用过。这些方法是: sizeForItemAtIndexP
我需要准备一个像 IOS spring board 这样的布局,即水平滚动的网格,启用分页,最重要的是单元格应该按行而不是按列排列(在第二张图片中)。我尝试使用 UICollectionViewFlo
我想从数据源中获取所有数据——包括可见的和不可见的单元格——以便计算可见单元格的属性。 collectionView:cellForItemAtIndexPath: 不起作用,因为它为不可见的单元格返
我正在尝试转换以下内容,但不断出现以下错误。 我正在尝试实现以下的 objective c 版本,效果很好: LSSwipeToDeleteCollectionViewLayout 是 UIColle
我已经实现了我自己的从 UICollectionViewLayout 子类化的集合 View 布局。在 prepare我计算和缓存布局属性。 在 layoutAttributesForElements
请帮我解决这个问题,我正在关注这个tutorial要制作自定义 UICollectionViewLayout,用户可以水平滑动以浏览各个部分,垂直滑动以显示项目。 我的问题是,我只希望用户在 UICo
我正在制作一个自定义 UICollectionViewLayout 类进行练习,本质上是尝试重现单元格无限水平布局的水平流布局。 一切都与我的 prepare() 和 layoutAttributes
我对 UICollectionViewLayout 进行了子类化以创建日历。我想让用户能够更改一些设置,例如屏幕上显示的天数(默认为 7)。 我将daysToShow保存在UserDefaults中。
我想使用具有自定义布局的 UICollectionView,其中集合的部分水平滚动,部分中的项目垂直滚动。 目前,我正在使用 UICollectionView,然后使用具有自己的 Collection
我正在将我的应用程序从 Obj-C 转换为 Swift。 我有 UICollectionViewLayout 的子类,它覆盖了已重命名为“prepare”的“prepareForLayout”。到目前
我是一名优秀的程序员,十分优秀!