作為3D Touch
的一部分,peek and pop
(預(yù)覽和彈出)個(gè)人覺(jué)得還是很有意思的,今天就說(shuō)說(shuō)如何去實(shí)現(xiàn)這一小功能恋昼。
一、遵守協(xié)議
@interface ViewController ()<UIViewControllerPreviewingDelegate>
@end
二赶促、注冊(cè)代理液肌,并傳入響應(yīng)3D Touch的視圖,并實(shí)現(xiàn)三個(gè)通知中心(分別為刪除鸥滨、置頂嗦哆、未讀功能)。
注意:注冊(cè)代理應(yīng)該放在viewDidLoad
里面婿滓,而看了很多文章都把registerForPreviewingWithDelegate
方法放在tableView
的cellForRowAtIndexPath
代理方法中老速,這樣寫(xiě)非常不好,因?yàn)樽?cè)只需要執(zhí)行一次凸主。
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteActionClick:) name:MessageDeleteNoti object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stickActionClick:) name:MessageStickNoti object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noReadActionClick:) name:MessageNoReadNoti object:nil];
if (@available(iOS 9.0, *)) {
if ([self check3DTouchAvailable]) { // 判斷3d touch是否可用
[self registerForPreviewingWithDelegate:(id)self sourceView:self.tableView];
}
}
}
- (BOOL)check3DTouchAvailable {
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
return YES;
}
return NO;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MessageDeleteNoti object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MessageStickNoti object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MessageNoReadNoti object:nil];
}
三橘券、實(shí)現(xiàn)UIViewControllerPreviewingDelegate協(xié)議
可以看看官方API
,只有需實(shí)現(xiàn)以下兩個(gè)方法
NS_CLASS_AVAILABLE_IOS(9_0) @protocol UIViewControllerPreviewingDelegate <NSObject>
// If you return nil, a preview presentation will not be performed
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0);
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0);
@end
3.1 當(dāng)系統(tǒng)察覺(jué)到足夠的壓力時(shí),會(huì)調(diào)用previewingContext:commitViewController:
代理方法:
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
// 將配置好的控制器推入導(dǎo)航棧
[self.navigationController pushViewController:viewControllerToCommit animated:YES];
}
3.2 當(dāng)用戶重壓區(qū)域的出現(xiàn)預(yù)覽界面旁舰,其他區(qū)域則會(huì)變模糊,在此方法中我們可以把需要的數(shù)據(jù)傳入到下一個(gè)界面锋华。
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if (!indexPath) {
return nil;
}
//防止重復(fù)加入
if ([self.presentedViewController isKindOfClass:[nextViewController class]]){
return nil;
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
listCell *cell = (listCell *)[self.tableView cellForRowAtIndexPath:indexPath];
CGRect rect = cell.frame;
previewingContext.sourceRect = rect;
NIMRecentSession *recentSession = self.conversationArr[indexPath.row];
if (recentSession.unreadCount > 0) {
[recentSession setValue:@"0" forKey:@"unreadCount"];
}
[self sort];
[self testDataIsNull];
nextViewController *vc = [[nextViewController alloc] initWithSession:recentSession.session];
vc.indexPath = indexPath;
vc.isTop = [recentSession isTop];
vc.isOfficeNotiAccount = recentSession.isOfficeNotiAccount;
return vc;
}
}
四、實(shí)現(xiàn)通知方法
- (void)deleteActionClick:(NSNotification *)noti {
NSIndexPath *indexPath = noti.userInfo[@"index"];
NIMRecentSession *recentSession = self.conversationArr[indexPath.row];
[self onDeleteRecentAtIndexPath:recentSession atIndexPath:indexPath];
}
- (void)stickActionClick:(NSNotification *)noti {
NSIndexPath *indexPath = noti.userInfo[@"index"];
NSInteger isTop = [noti.userInfo[@"isTop"] integerValue];
NIMRecentSession *recentSession = self.conversationArr[indexPath.row];
if (isTop) { // 點(diǎn)擊了取消置頂
[recentSession setTopFlag:0];
[self sort];
[self testDataIsNull];
}else { // 點(diǎn)擊了置頂
[recentSession setTopFlag:1];
[self sort];
[self testDataIsNull];
}
}
- (void)noReadActionClick:(NSNotification *)noti {
NSIndexPath *indexPath = noti.userInfo[@"index"];
NIMRecentSession *recentSession = self.conversationArr[indexPath.row];
[recentSession setValue:@"1" forKey:@"unreadCount"];
[self testDataIsNull];
}
五鬓梅、添加Peek
狀態(tài)下供置,上劃時(shí)的快速操作谨湘,即在nextViewController
重寫(xiě)方法绽快,通過(guò)發(fā)送通知告訴上一個(gè)界面需要執(zhí)行的操作。
#pragma mark - UIPreviewActionItem
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *noReadAction = [UIPreviewAction actionWithTitle:@"標(biāo)為未讀" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"標(biāo)為未讀");
[[NSNotificationCenter defaultCenter] postNotificationName:MessageNoReadNoti object:nil userInfo:@{@"index":self.indexPath}];
}];
UIPreviewAction *stickAction = [UIPreviewAction actionWithTitle:@"置頂" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"置頂");
[[NSNotificationCenter defaultCenter] postNotificationName:MessageStickNoti object:nil userInfo:@{@"index":self.indexPath,@"isTop":@(self.isTop)}];
}];
UIPreviewAction *cancelStickAction = [UIPreviewAction actionWithTitle:@"取消置頂" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"取消置頂");
[[NSNotificationCenter defaultCenter] postNotificationName:MessageStickNoti object:nil userInfo:@{@"index":self.indexPath,@"isTop":@(self.isTop)}];
}];
UIPreviewAction *deleteAction = [UIPreviewAction actionWithTitle:@"刪除" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"刪除");
[[NSNotificationCenter defaultCenter] postNotificationName:MessageDeleteNoti object:nil userInfo:@{@"index":self.indexPath}];
}];
if (self.isOfficeNotiAccount) {
return @[noReadAction, deleteAction];
}
return self.isTop ? @[noReadAction, cancelStickAction, deleteAction] : @[noReadAction, stickAction, deleteAction];
}