Ios 如何在UICollectionView上绘制接触(UIBezierPath)?
好吧,我花了好几个小时搜索和尝试了一些东西,但结果是空的。希望你们都能帮忙 我需要用苹果铅笔(或手指)在UICollectionView上画画。想想绕着细胞旋转,画箭头之类的事情。我正在使用Apple的示例对一个UIView进行子类化以进行绘制,并且我已经能够将它设置为其他视图的子视图,以便能够毫无问题地在这些视图之上进行绘制。但是,当我使用UICollectionView执行此操作时,它不起作用。如果我将视图和CollectionView放在viewController中的同一个“级别”上,并将UICollectionView设置为hidden,它就会工作。如果UICollectionView可见,则仍会绘制线,但通过UICollectionView当然不可见。任何其他方式,都没有 我尝试过将UICollectionViewController子类化,以便自己想出一些东西,但似乎不知道如何使其工作。任何帮助都将不胜感激 谢谢 编辑:在下面添加更多详细信息 IB(在PieChartView上绘制)中的此设置工作正常: 我使用的代码如下(当然,删除了无关代码): 。。。但事实并非如此。(请原谅这个图表,不能发布超过2个链接) 我尝试按如下方式子类化UICollectionViewController,但结果不一:Ios 如何在UICollectionView上绘制接触(UIBezierPath)?,ios,swift,uicollectionview,uibezierpath,Ios,Swift,Uicollectionview,Uibezierpath,好吧,我花了好几个小时搜索和尝试了一些东西,但结果是空的。希望你们都能帮忙 我需要用苹果铅笔(或手指)在UICollectionView上画画。想想绕着细胞旋转,画箭头之类的事情。我正在使用Apple的示例对一个UIView进行子类化以进行绘制,并且我已经能够将它设置为其他视图的子视图,以便能够毫无问题地在这些视图之上进行绘制。但是,当我使用UICollectionView执行此操作时,它不起作用。如果我将视图和CollectionView放在viewController中的同一个“级别”上,并
import UIKit
class CollectionViewCanvas: UICollectionViewController {
// MARK: - VARIABLES
var visualizeAzimuth = false
var table: Table!
var detailView: DetailViewController!
let reuseIdentifier = "cell" // also set as cell identifier in storyboard
var textView: UITextView!
var clearButton: UIButton!
var workingData: [Double]! = []
var canvasView = CanvasView()
// MARK: - FUNCTIONS
override func loadView() {
super.loadView()
self.collectionView?.addSubview(canvasView)
canvasView.hidden = false
collectionView?.hidden = true
}
// MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//return self.items.count
return self.table.COUNT
}
// make a cell for each cell index path
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// cell formatting here
}
// MARK: - UICollectionViewDelegate protocol
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// doing stuff in here that sets up the cells
collectionView.reloadItemsAtIndexPaths(indices)
}
// MARK: - UICOllectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return -0.5
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellWidth = collectionView.frame.width / 5.0
return CGSize(width: cellWidth, height: 50)
}
// MARK: - Touches for CanvasView
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
canvasView.endTouches(touches, cancel: false)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
guard let touches = touches else { return }
canvasView.endTouches(touches, cancel: true)
}
override func touchesEstimatedPropertiesUpdated(touches: Set<NSObject>) {
canvasView.updateEstimatedPropertiesForTouches(touches)
}
}
导入UIKit
类CollectionViewCanvas:UICollectionViewController{
//标记:-变量
方位角=假
var表格:表格!
var-detailView:DetailViewController!
让reuseIdentifier=“cell”//也设置为故事板中的单元标识符
var textView:UITextView!
var clearButton:UIButton!
变量工作数据:[Double]!=[]
var canvasView=canvasView()
//马克:-功能
重写func loadView(){
super.loadView()
self.collectionView?.addSubview(画布视图)
canvasView.hidden=false
collectionView?隐藏=真
}
//标记:-UICollectionViewDataSource协议
//告诉集合视图要生成多少个单元格
重写func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
//返回self.items.count
返回self.table.COUNT
}
//为每个单元格索引路径创建一个单元格
重写func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
//这里的单元格格式
}
//标记:-UICollectionViewDelegate协议
重写func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath){
//在这里做一些事情来建立细胞
collectionView.ReloadItemsAndExpaths(索引)
}
//标记:-UICOllectionViewDelegateFlowLayout
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumItemSpacingForSectionAtIndex节:Int)->CGFloat{
回报率-0.5
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,minimumLineSpacingForSectionAtIndex节:Int)->CGFloat{
返回0
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:NSIndexPath)->CGSize{
设cellWidth=collectionView.frame.width/5.0
返回CGSize(宽度:cellWidth,高度:50)
}
//马克:-触摸画布视图
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
canvasView.DrawTouchs(Touchs,withEvent:event)
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
canvasView.DrawTouchs(Touchs,withEvent:event)
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
canvasView.DrawTouchs(Touchs,withEvent:event)
canvasView.EndTouchs(触摸,取消:false)
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
保护让接触=接触其他{return}
canvasView.EndTouchs(触摸,取消:true)
}
覆盖功能触摸估计属性更新(触摸:设置){
canvasView.UpdateEstimatedProperties for touches(触摸)
}
}
在这种情况下,当collectionView设置为hidden时,我可以用铅笔触发TouchsBegind()。如果collectionView可见,则触摸会直接转到didSelectItemAtIndexPath,仅此而已
希望这是有益的澄清 MichaelDotKnox更新道:“你能发布一些你已经尝试过的、有效的和无效的代码吗?”。如果您想了解更多信息,请告诉我!
V View
V CollectionView
CanvasView
import UIKit
class CollectionViewCanvas: UICollectionViewController {
// MARK: - VARIABLES
var visualizeAzimuth = false
var table: Table!
var detailView: DetailViewController!
let reuseIdentifier = "cell" // also set as cell identifier in storyboard
var textView: UITextView!
var clearButton: UIButton!
var workingData: [Double]! = []
var canvasView = CanvasView()
// MARK: - FUNCTIONS
override func loadView() {
super.loadView()
self.collectionView?.addSubview(canvasView)
canvasView.hidden = false
collectionView?.hidden = true
}
// MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//return self.items.count
return self.table.COUNT
}
// make a cell for each cell index path
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// cell formatting here
}
// MARK: - UICollectionViewDelegate protocol
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// doing stuff in here that sets up the cells
collectionView.reloadItemsAtIndexPaths(indices)
}
// MARK: - UICOllectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return -0.5
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellWidth = collectionView.frame.width / 5.0
return CGSize(width: cellWidth, height: 50)
}
// MARK: - Touches for CanvasView
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
canvasView.drawTouches(touches, withEvent: event)
canvasView.endTouches(touches, cancel: false)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
guard let touches = touches else { return }
canvasView.endTouches(touches, cancel: true)
}
override func touchesEstimatedPropertiesUpdated(touches: Set<NSObject>) {
canvasView.updateEstimatedPropertiesForTouches(touches)
}
}