前段時間花了點時間去研究了3DTouch這一塊,咱們就來聊一下具帮,看看怎么在app中集成3DTouch中peek pop博肋。 雖說iOS9出了這個功能感覺不太實用,但是作為一個開發(fā)者蜂厅,我覺得還是有必要去了解一下匪凡,弄好了還能讓自己的app裝一回逼。那下面簡單介紹一下peek和pop分別是什么掘猿。
peek:當你用力按下屏幕按到一定程度時锹雏,系統會彈出一個預覽視圖,這個過程就稱之為peek术奖。
pop:再用力按下去就會展開到預覽視圖的控制器,這過程就是pop轻绞。
廢話不多說采记,直接上代碼
1、在viewDidLoad方法中注冊預覽代理
// 注意這是ios9的API,需要做一下版本控制政勃,否則運行到9以下的會崩唧龄。
#ifdef __IPHONE_9_0
if(KC_IS_IOS9) {
// 判斷設備是否支持3dTouch
if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[selfregisterForPreviewingWithDelegate:self sourceView:self.view];
}
}
#endif
2、注冊完成后控制器需要遵守<UIViewControllerPreviewingDelegate>此協議奸远,那我們來看看協議內定義的方法既棺。
NS_CLASS_AVAILABLE_IOS(9_0)@protocolUIViewControllerPreviewingDelegate
// If you return nil, a preview presentation will not be performed
// peek過程會調用此方法,返回需要預覽的控制器(即需要跳轉到下一級的控制器)
- (nullableUIViewController*)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)locationNS_AVAILABLE_IOS(9_0);
// pop過程會調用此方法懒叛,執(zhí)行跳轉
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController*)viewControllerToCommitNS_AVAILABLE_IOS(9_0);
@end
協議中兩個方法是require的丸冕,所以控制器必須要實現這兩個方法,那該在這兩個方法干些什么呢薛窥?我們來實現協議中的方法吧胖烛。
#pragma mark -UIViewControllerPreviewingDelegate
// Peek
- (UIViewController*)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
/*
context: 執(zhí)行peek的上下文對象。
point: 按壓位置再souceView上的點诅迷,可以理解為手指在屏幕上的按壓點佩番。
*/
// 1、獲取sourceView 即注冊時傳入的sourceView罢杉,一般為控制器的view
UIView*sourceView = [context sourceView];
// 然后判斷按壓點是否在某個控件的frame內趟畏,假設我們有某個自定義控件為self.locationView
/*
這里有個涉及到坐標系轉換的細節(jié)問題
如果self.locationView不是sourceView的直接子控件,那么我們需要把point轉換到self.locationView的父控件的坐標系中滩租,代碼如下:
point = [self.locationView.superView convertPoint:point fromView:sourceView];
若缺少這段代碼赋秀,你會發(fā)現按壓位置錯亂的bug利朵。
*/
// 2、如果self.locationView.frame不包含這個點就直接return,不做任何操作
if(!CGRectContainsPoint(self.locationView.frame, point)) return nil;
// 能來到這里沃琅,即觸摸點在self.locationView上哗咆,那么
// 3、設置sourceRect益眉,這個souceRect就是當你按壓時候浮起來的那個矩形區(qū)域
CGRect sourceRect = self.locationView.frame;
/*
如果self.locationView不是sourceView的直接子控件,這里同時也涉及到坐標系轉換
sourceRect = [self convertRect:self.locationView.frame toView:sourceView];
*/
[context setSourceRect:sourceRect];
// 4晌柬、然后創(chuàng)建需要跳轉的目標控制器返回就OK了
MyLocationViewController*locationVC = [MyLocationViewController new];
// 目標控制器有參數也需要傳入參數
locationVC.locationModel=self.locationModel;
return locationVC;
}
// pop
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController*)viewControllerToCommit {
// 這里十分簡單,只需要直接show一下就OK了
[self showViewController:viewController ToCommitsender:self];
}
3郭脂、peek,pop這么簡單就實現了年碘,下面我們再來看看怎么在tableView中使用
- (UIViewController*)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
// 1、獲取sourceView
UIView*sourceView = [context sourceView];
/**通過坐標點獲取 indexPath */
/*
同樣如果sourceView != self.tableView的話展鸡,也需要轉換坐標系
point = [self.tableView convertPoint:point fromView:sourceView];
*/
NSIndexPath*indexPath = [self.tableView indexPathForRowAtPoint:point];
// 如果indexPath為nil屿衅,則直接返回nil
if(!indexPath) return nil;
/**獲得當前cell */
HCQBusinessCell*cell = [self.tableView cellForRowAtIndexPath:indexPath];
// 設置sourceRect
CGRect sourceRect = cell.frame;
/*
這里同時也涉及到坐標系轉換
sourceRect = [self.tableView convertRect:cell.frame toView:sourceView];
*/
[context setSourceRect:sourceRect];
HCQMechantDetailViewController*detailVC = [HCQMechantDetailViewController new];
detailVC.mechantModel= cell.mechantModel;
returndetailVC;
}
4、至此莹弊,在tableView上Peek,pop也集成完畢涤久,至于collectionView也是類似,我就不多說了忍弛。然而我在網上看到一些文章响迂,他們是把注冊放到cellForRow方法中,類似這樣子:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
HCQBusinessCell*cell = [tableViewdequeueReusableCellWithIdentifier:HCQBusinessCellReuseID];
cell.mechantModel=self.mechantModels[indexPath.row];
// 3DTouch peek..pop
if(KC_IS_IOS9) {
if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
}
returncell;
}
我只想說:大家千萬不要這樣搞细疚,代理只需要注冊一次蔗彤,重復注冊就會有問題了。有興趣的可以自行嘗試疯兼,但我真不建議你這樣做然遏。好了,先說這么多吧吧彪,快點為你的app集成3DTouch待侵,開展你的裝B之旅吧~~