上一篇3D touch中有很多不足之處,今天我再補(bǔ)充一些.
用過(guò)3dtouch的朋友都知道,按壓圖標(biāo)后會(huì)出來(lái)幾個(gè)item,點(diǎn)擊任意一個(gè)item后會(huì)進(jìn)入到不同的頁(yè)面,還有進(jìn)入程序后第一次重壓會(huì)有個(gè)上拉的動(dòng)作,并會(huì)出現(xiàn)幾個(gè)選項(xiàng).今天就來(lái)實(shí)現(xiàn)這2個(gè)功能.
在AppDelegate里面:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
_window.rootViewController = nav;
[_window makeKeyAndVisible];
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"搜索" localizedTitle:@"搜索"];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"第二頁(yè)" localizedTitle:@"第二頁(yè)"];
NSArray *shortItems = @[item1,item2];
[[UIApplication sharedApplication]setShortcutItems:shortItems];
return YES;
}
這里沒(méi)什么問(wèn)題,很簡(jiǎn)單,就是按壓圖標(biāo)后出現(xiàn)2個(gè)item,重點(diǎn)是點(diǎn)擊item后實(shí)現(xiàn)push到具體的界面.請(qǐng)看下面:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
if ([shortcutItem.localizedTitle isEqual:@"搜索"]) {
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
_window.rootViewController = nav;
[_window makeKeyAndVisible];
SearchView *sv = [[SearchView alloc]init];
[sv.searchBar becomeFirstResponder];
[nav pushViewController:sv animated:YES];
}
else if([shortcutItem.localizedTitle isEqual:@"第二頁(yè)"])
{
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
_window.rootViewController = nav;
[_window makeKeyAndVisible];
SecondViewController *sv = [[SecondViewController alloc]init];
[nav pushViewController:sv animated:YES];
}
}
這個(gè)方法是專門(mén)為3DTouch所用的,參數(shù)shortcutItem就是具體點(diǎn)擊的那個(gè)item,通過(guò)判斷它的localizedTitle來(lái)跳轉(zhuǎn)到不同的界面.
如圖:
下面是重壓后上拉動(dòng)作.
首先,我們?cè)赩iewcontroller里面創(chuàng)建一個(gè)tableView,并讓他擁有簡(jiǎn)單的3DTouch效果,代碼如下:
#import "ViewController.h"
#import "SearchView.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *cellArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"第一頁(yè)";
_tableView = [[UITableView alloc]initWithFrame:self.view.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
_cellArray = @[@"第一頁(yè)",@"第二頁(yè)",@"搜索"];
[self.view addSubview:_tableView];
}
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _cellArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[self registerForPreviewingWithDelegate:self sourceView:cell];
cell.textLabel.text = _cellArray[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {
[self.navigationController pushViewController:[[SecondViewController alloc]init] animated:YES];
}
else if(indexPath.row == 2)
{
[self.navigationController pushViewController:[[SearchView alloc]init] animated:YES];
}
}
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
if (indexPath.row == 0) {
ViewController *vc = [[ViewController alloc]init];
vc.preferredContentSize = CGSizeMake(0.0f, 600.0f);
return vc;
}
else if (indexPath.row == 1)
{
SecondViewController *svc = [[SecondViewController alloc]init];
svc.preferredContentSize = CGSizeMake(0, 600);
return svc;
}
else
{
SearchView *searchVC = [[SearchView alloc]init];
searchVC.preferredContentSize = CGSizeMake(0, 600);
return searchVC;
}
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
現(xiàn)在的效果是這樣:
怎么實(shí)現(xiàn)上拉效果呢.是這樣的,比如第二頁(yè)有個(gè)上拉效果,那么我們?cè)诰唧w的頁(yè)面寫(xiě)個(gè)方法就可以了,也就是在SecondViewcontroller.m里面寫(xiě)這個(gè)方法:
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"進(jìn)去" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
SecondViewController *vc = [[SecondViewController alloc]init];
ViewController *viewcontroller = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewcontroller];
[UIApplication sharedApplication].keyWindow.rootViewController = nav;
[[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
[nav pushViewController:vc animated:YES];
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
NSArray *array = @[action1,action2];
return array;
}
這樣,第一次重壓第二頁(yè)出現(xiàn)綠色小框后再上拉會(huì)出現(xiàn)"進(jìn)去"和"取消"兩個(gè)選項(xiàng).同理,在searchView里面也是這樣,第一頁(yè)沒(méi)有上拉,所以當(dāng)?shù)谝淮沃貕旱谝豁?yè)后上面沒(méi)有上拉的箭頭.大家看看效果圖吧:
好了,代碼就這些,其實(shí)也并不難.github下載地址