Collection View Programming Guide for iOS---(五)---Incorporating Gesture Support
Incorporating Gesture Support
结合手势支持
You can add greater interactivity to your collection views through the use of gesture recognizers. Like any view, you can add a gesture recognizer to a collection view and use it to trigger actions when those gestures occur. For a collection view, there are two types of actions that you might likely want to implement:
你可以通过使用手势识别给collection 视图添加更大的交互性(interactivity)。跟任何视图一样,你可以给一个collection 视图添加手势识别,当那些手势发生时可以用它来触发各种操作。collection 视图有两种类型的操作是你可能想要实现的,它们是:
You want to trigger changes to the collection view’s layout information
- 你想要触发collection 视图的布局信息改变.
You want to manipulate cells and views directly.
- 你想要直接操作单元格(cells)和视图.
You should always attach your gesture recognizers to the collection view itself and not to a specific cell or view. The UICollectionView
class is a descendant ofUIScrollView
. Attaching your gesture recognizers to the collection view is less likely to interfere with the other gestures that must be tracked. In addition, because the collection view has access to your data source and your layout object, you still have access to all the information you need to manipulate cells and views appropriately.
你应该总是给collection 视图本身添加(attach)手势识别,而不是给某个特定的cell 或视图。UICollectionView 类是UIScrollView 类的一个子类(descendant). 把手势识别添加到collection 视图不太可能会妨碍其它必须被跟踪(tracked)的各种手势。另外, 因为collection 视图已经访问了你的数据源和布局对象,你还可以访问了你需要相应地(appropriately)操作cells 和视图的所有的信息。
Using a Gesture Recognizer to Modify Layout Information
一、 使用手势识别来修改布局信息
A gesture recognizer offers an easy way to modify layout parameters dynamically. For example, you might use a pinch gesture recognizer to change the spacing between items in a custom layout. The process for configuring such a gesture recognizer is relatively simple:
手势识别给动态修改布局参数提供一条简单的方法。 比如, 你可能使用一个捏合(pinch)手势识别来改变一个自定义布局上数据项之间的距离。配置这样一个手势识别相对比较简单:
Create the gesture recognizer.
创建手势识别
Attach the gesture recognizer to the collection view.
给collection 视图添加手势识别
Use the handler method of the gesture recognizer to update the layout parameters and invalidate the layout object.
使用手势识别的处理方法来更新布局参数以及无效化布局对象。
Creating a gesture recognizer is the same alloc/init process that you use for all objects. During initialization, you specify the target object and action method to call when the gesture is triggered. You then call the collection view’saddGestureRecognizer:
method to attach it to the view. Most of the actual work happens in the action method you specify at initialization time.
创建一个手势识别跟所有对象一样都是alloc/init 过程。 初始化时,你指定目标对象和当手势触发时要调用的操作方法。然后调用collection 视图的addGestureRecognizer: 方法来把它添加到视图。 大部分的实际工作发生在初始化时你指定的操作方法中。
Listing 4-1 shows an example of an action method that is called by a pinch gesture recognizer attached to a collection view. In this example, the pinch data is used to change the distance between cells in a custom layout. The layout object implements the custom updateSpreadDistance
method, which validates the new distance value and stores it for use during the layout process later. The action method then invalidates the layout and forces it to update the position of items based on the new value.
列表4-1 显示了一个操作方法例子,该方法由一个collection 视图中的捏合手势识别(pinch gesture recognizer)调用。 在该例子中, 捏合(pinch)数据被用于改变一个自定义布局上cells之间的距离。 该布局对象实现了自定义 updateSpreadDistance 方法,它激活新的距离值并保存它以便在以后的布局过程中使用。操作方法然后无效化布局并强制布局根据新值更新数据项的位置。
Listing 4-1 Using a gesture recognizer to change layout values
列表4-1 使用一个手势识别来改变布局变量值
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender { |
if ([sender numberOfTouches] != 2) |
return; |
// Get the pinch points. |
CGPoint p1 = [sender locationOfTouch:0 inView:[self collectionView]]; |
CGPoint p2 = [sender locationOfTouch:1 inView:[self collectionView]]; |
// Compute the new spread distance. |
CGFloat xd = p1.x - p2.x; |
CGFloat yd = p1.y - p2.y; |
CGFloat distance = sqrt(xd*xd + yd*yd); |
// Update the custom layout parameter and invalidate. |
MyCustomLayout* myLayout = (MyCustomLayout*)[[self collectionView] collectionViewLayout]; |
[myLayout updateSpreadDistance:distance]; |
[myLayout invalidateLayout]; |
} |
For more information about creating gesture recognizers and attaching them to views, see Event Handling Guide for iOS.
关于创建手势识别和给视图添加手势识别的更多信息,请看Event Handling Guide for iOS.
Working with the Default Gesture Recognizers
二、用默认手势识别工作
The parent class of UICollectionView
class installs a default tap gesture recognizer and a default long-press gesture recognizer to handle scrolling interactions. You should never try to reconfigure these default gesture recognizers or replace them with your own versions. If you want to add custom tap or long-press gestures to a collection view, configure the values of your gesture recognizer to be different than the default ones that are already installed. For example, you might configure a tap gesture recognizer to respond only to double-taps. You must then link your custom gesture recognizer to the default versions using therequireGestureRecognizerToFail:
method. That method causes the default gesture recognizer to fire only when your gesture recognizer decides not to fire.
UICollectionView 类的父类带有一个默认的叩击(tap)手势识别和一个默认的长按(long-press)手势识别来处理滚动交互。 你决不能试着重新识别这些默认手势识别,或用你自己的版本来替换它们。 如果你想给collection 视图添加自定义叩击或长按手势,给你的手势识别设置一个跟默认手势不一样的值。比如,你可能设置一个叩击(tap)手势来只响应双叩击(double-taps)。 然后你必须用requireGestureRecognizerToFail: 方法来把你自定义的手势识别连接到默认版本。 该方法导致默认手势识别只在你的手势识别决定响应的时候才响应。
Listing 4-2 shows how you might link a custom tap gesture recognizer to the default recognizer associated with a collection view. In this case, the owning view controller adds the gesture recognizer to the collection view at load time. After creating the custom gesture recognizer, the code iterates through the collection view’s default set of gesture recognizers looking for one of the same type. It then links the default gesture recognizer to the custom one that was just created. Finally, the view controller adds the gesture recognizer to the collection view. It adds the gesture recognizer last to avoid encountering it during the for
loop.
列表4-2 显示了你可能如何把一个自定义叩击手势识别连接到collection 视图中的默认手势识别上。 在这种情况下,所属视图控制器在加载时把手势识别添加到collection 视图。 创建完自定义手势识别后,代码迭代地在collection 视图的默认手势识别集合中搜索一个相同的类型。 然后把它连接到该默认手势识别上。 最后,视图控制器把手势识别添加到collection 视图上。 它在最后添加手势识别是为了避免在循环时遇见t(encountering)。
Listing 4-2 Linking a default gesture recognizer to a custom gesture recognizer
列表4-2 把一个默认手势识别连接到一个自定义手势识别上
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; |
NSArray* recognizers = [self.collectionView gestureRecognizers]; |
// Make the default gesture recognizer wait until the custom one fails. |
for (UIGestureRecognizer* aRecognizer in recognizers) { |
if ([aRecognizer isKindOfClass:[UITapGestureRecognizer class]]) |
[aRecognizer requireGestureRecognizerToFail:tapGesture]; |
} |
// Now add the gesture recognizer to the collection view. |
tapGesture.numberOfTapsRequired = 2; |
[self.collectionView addGestureRecognizer:tapGesture]; |
Manipulating Cells and Views
三、操作Cells 和视图
How you use a gesture recognizer to manipulate cells and views depends on the types of manipulations you plan to make. Simple insertions and deletions can be performed inside the action method of a standard gesture recognizer. But if you plan more complex manipulations, you probably need to define a custom gesture recognizer to track the touch events yourself.
如何使用一个手势识别来操作cells 和视图依赖于你计划做的操作的类型。 简单的插入和删除可以在一个标准手势识别的操作方法内执行。 但是如果你计划更复杂的操作,你可能需要定义一个自定义手势识别来跟踪你自己的触摸事件。
One type of manipulation that requires a custom gesture recognizer is moving a cell in your collection view from one location to another. The most straightforward way to move a cell is to delete it (temporarily) from the collection view, use the gesture recognizer to drag around a visual representation of that cell, and then insert the cell at its new location when the touch events end. All of this requires managing the touch events yourself, working closely with the layout object to determine the new insertion location, manipulating the data source changes, and then inserting the item at the new location.
一种要求一个自定义手势识别的操作类型是把collection视图中的一个cell 从一个位置移到另一个位置。 把一个cell 从collection 视图上(暂时)删除的最直接方法是使用手势识别来拖动该cell拖到一个可视化替代,然后当触摸事件结束时把该cell插入它的新位置。 所有这些操作都要求管理自身的触摸事件,和布局对象紧密工作来决定新的插入位置,操作数据源的改变,然后把数据线插入新位置。
For more information about creating custom gesture recognizers, see Event Handling Guide for iOS.
关于创建自定义手势识别的更多信息,请看 Event Handling Guide for iOS.
Collection View Programming Guide for iOS---(五)---Incorporating Gesture Support的更多相关文章
- Collection View Programming Guide for iOS---(一)----About iOS Collection Views
Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...
- Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example
Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...
- View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views
Views Because view objects are the main way your application interacts with the user, they have many ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations
Animations Animations provide fluid visual transitions between different states of your user inter ...
- Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate
Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...
- View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture
View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...
- View Programming Guide for iOS ---- iOS 视图编程指南(一)
Next About Windows and Views 关于窗口和视图 In iOS, you use windows and views to present your application’s ...
- View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows
Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...
随机推荐
- ubuntu harddisk uuid already exists
就是virtualbox下先用u盘启动的虚拟机,把U盘的vhdk文件拷贝到本机,然后再启动,就有问题,提示什么uuid already exist 找了半天,网上基本都是说windows下如何用的.. ...
- 阿里云 ubuntu 14.04 模板上安装 docker
ubuntu 14.04 的内核是 3.13 ,所以内核不用升级. 安装过程例如以下: # apt-get update # apt-get install apt-transport-https # ...
- 使用ViewPager多页面滑动切换以及动画效果
https://github.com/eltld/Viewpager
- 全志Android SDK编译详解(一)
1.编译环境说明 Linux 服务器(任意电脑都可以,建议选择配置高的)安装ubuntu 10.04 作为编译的主机,其他开发者在xp 下,通过SSH/Telent 等协议登录linux 主机进行编. ...
- T-SQL查询进阶--变量
概述 变量对于一种语言是必不可少的一部分,当然,对于T-SQL来讲也是一样.在简单查询中,往往很少用到变量,但无论对于复杂的查询或存储过程中,变量都是必不可少的一部分. 变量的种类 在T-SQL中,变 ...
- 责任链模式-Chain of Responsibility
责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 责任链模式结构图: 代码实现: 责任链模式 ...
- debian apt-get工作的原理
1 apt-get update apt-get update并没有将远程仓库的包都下载到本地,而是通过访问远程仓库创建或者更新了远程仓库的本地索引,索引文件放在/var/lib/apt/lists目 ...
- 使用jsoncpp解析生成json
在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧) 在电脑上安装Python,运行amalgamate.py,生成的di ...
- POJ 1737 Connected Graph(高精度+DP递推)
题面 \(solution:\) 首先做个推销:带负数的压位高精度(加减乘+读写) 然后:由 \(N\) 个节点组成的无向图的总数为: \(2^{N*(N-1)/2}\) (也就是说这个图总共有 \( ...
- 20170228 METHOD handle_data_changed-
CALL METHOD er_data_changed->add_protocol_entry METHOD handle_data_changed. DATA: ls_modi TYPE ...