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)手势识别来改变一个自定义布局上数据项之间的距离。配置这样一个手势识别相对比较简单:

  1. Create the gesture recognizer.

    创建手势识别

  2. Attach the gesture recognizer to the collection view.

    给collection 视图添加手势识别

  3. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

    Views Because view objects are the main way your application interacts with the user, they have many ...

  4. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  5. View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

      Animations Animations provide fluid visual transitions between different states of your user inter ...

  6. 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 ...

  7. View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture

    View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...

  8. View Programming Guide for iOS ---- iOS 视图编程指南(一)

    Next About Windows and Views 关于窗口和视图 In iOS, you use windows and views to present your application’s ...

  9. 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 ...

随机推荐

  1. MySql 初始化权限脚本

    刚装好MySql后无法用客户端工具连接,通过命令行登录后,运行下面的脚本: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'     IDENTIFIED BY 'y ...

  2. setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果

    [self.indicator setImage:[UIImage imageNamed:@"01_login_moreicon@2x.png"] forState:UIContr ...

  3. windows redis 服务安装坑

    环境 winserver 2012 最新版的redis:3.0.503 redis-server.exe   --service-install   redis.windows.conf    --m ...

  4. 【深度探索c++对象模型】关于对象

    Linux进程的五个段 BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属 ...

  5. innodb 修改表共享空间为独立空间

    最近在优化mysql innodb存储引擎,准备把共享表空间转换成独立表空间.刚开始的没考虑这么多,过段时间又要推广,所以优化一下,看看效果如何.说一个转换过程. 1,查看一下是共享表空间,还是独立表 ...

  6. java:BufferedImage推断图像通道顺序并转RGB/BGR

    一般来说java ImageIO处理读取图像时.通常是RGB或ARGB格式,可是有的时候.我们须要图像是BGR格式. 比方通过JNI将图像矩阵传递给动态库,动态库里用OpenCV来处理矩阵,而用Ope ...

  7. Visual Studio自动生成文件版本信息

    一.     前言 通常,要控制输出文件的版本信息,只需要手动修改资源rc文件中的Version,即可在输出文件的文件属性里查看到对应的版本信息.如下图:    但是,版本号是会随时都更新的,每次bu ...

  8. 电脑突然死机,编译报错dll缺少依赖项

    由于ASP.NET缓存没更新的问题(我的就是这个问题.电脑突然死机导致的). 把这个文件夹下的文件所有删除C:\Windows\Microsoft.NET\Framework\v2.0.50727\T ...

  9. hibernate预编译SQL语句中的setParameter和setParameterList

    使用预编译SQL语句和占位符參数(在jdbc中是?),可以避免由于使用字符串拼接sql语句带来的复杂性.我们先来简单的看下.使用预编译SQL语句的优点. 使用String sql = "se ...

  10. 2016/06/13 phpexcel 未完待续

    ①准备工作: 1,php版本不能太低 2,去官网下载PHPExcel插件    http://phpexcel.codeplex.com/ 3,解压后提取classes文件夹到工作目录,并重命名为PH ...