Gesture binding is the most commonly used, but inexperienced friends, for the first time using Swift to bind each cell in the Uicollectionview collection, are somewhat dazed. If the implementation of swift gestures and CollectionView is not yet clear, check out my two swift versions of the gestures feature and tutorials: Swift uses the Collectionview+coredata principle + code Comment, The rest is the more painful question I have encountered: How to bind gestures to Uicollectionviewcell, or to bind gestures to each cell!!
First you have to implement the proxy
The code is as follows |
Copy Code |
Class Viewcontroller: Uiviewcontroller, Uicollectionviewdelegateflowlayout, Uicollectionviewdatasource, Uigesturerecognizerdelegate//Gesture Agent { ...... }
|
Implement code binding gestures for each cell
The code is as follows |
Copy Code |
Func CollectionView (Collectionview:uicollectionview, Cellforitematindexpath Indexpath:nsindexpath)-> Uicollectionviewcell { Let cell = Collectionview.dequeuereusablecellwithreuseidentifier ("Newcell", Forindexpath:indexpath) as Mycollectionviewcell
Cell.backgroundcolor=uicolor.blackcolor () var z:anyobject! = Dataarr[indexpath.row].valueforkey ("Zhangdanri") var h:anyobject! = Dataarr[indexpath.row].valueforkey ("Huankuanri") Cell.textlabel? Text = "(z) ~ (h)" Cell.textlabel1? Text=dataarr[indexpath.row].valueforkey ("bank") as? String This tag is very useful. Cell.tag = Indexpath.row The most important is the following three lines, remember is here to bind gestures, I just started not to know LP = Uilongpressgesturerecognizer (Target:self, Action:selector ("longpress:")) Lp.delegate = Self Cell.addgesturerecognizer (LP) return cell } |
Implement the callback function of long press
The code is as follows |
Copy Code |
Func longpress (Recognizer:uilongpressgesturerecognizer) { This is the array subscript of the cell you currently selected var index = recognizer.view!. Tag Code section if (recognizer.state = =. began)//very IMPORTANT!! is to start with 0.5 seconds to trigger the event. { ..... } } |
Each event is stateful, and the state is also a process: start-change-"end, if the above code does not start to judge, then the long press 0.5 seconds after your actual code will be executed several times!!!
Began
Changed
Ended
Cancelled
Failed