結合手勢支持html
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:ios
你能夠經過使用手勢識別給collection 視圖添加更大的交互性(interactivity)。跟任何視圖同樣,你能夠給一個collection 視圖添加手勢識別,當那些手勢發生時能夠用它來觸發各類操做。collection 視圖有兩種類型的操做是你可能想要實現的,它們是:app
You want to trigger changes to the collection view’s layout informationless
You want to manipulate cells and views directly.ide
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.oop
你應該老是給collection 視圖自己添加(attach)手勢識別,而不是給某個特定的cell 或視圖。UICollectionView 類是UIScrollView 類的一個子類(descendant). 把手勢識別添加到collection 視圖不太可能會妨礙其它必須被跟蹤(tracked)的各類手勢。另外, 由於collection 視圖已經訪問了你的數據源和佈局對象,你還能夠訪問了你須要相應地(appropriately)操做cells 和視圖的全部的信息。佈局
1、 使用手勢識別來修改佈局信息ui
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:this
手勢識別給動態修改佈局參數提供一條簡單的方法。 好比, 你可能使用一個捏合(pinch)手勢識別來改變一個自定義佈局上數據項之間的距離。配置這樣一個手勢識別相對比較簡單:spa
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 方法,它激活新的距離值並保存它以便在之後的佈局過程當中使用。操做方法而後無效化佈局並強制佈局根據新值更新數據項的位置。
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.
2、用默認手勢識別工做
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)。
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]; |
3、操做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.