Give Uicollectionviewcell a hand gesture long press example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.