上一篇3D touch中有很多不足之处,今天我再补充一些.
用过3dtouch的朋友都知道,按压图标后会出来几个item,点击任意一个item后会进入到不同的页面,还有进入程序后第一次重压会有个上拉的动作,并会出现几个选项.今天就来实现这2个功能.
在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:@"第二页" localizedTitle:@"第二页"];
NSArray *shortItems = @[item1,item2];
[[UIApplication sharedApplication]setShortcutItems:shortItems];
return YES;
}
这里没什么问题,很简单,就是按压图标后出现2个item,重点是点击item后实现push到具体的界面.请看下面:
-(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:@"第二页"])
{
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];
}
}
这个方法是专门为3DTouch所用的,参数shortcutItem就是具体点击的那个item,通过判断它的localizedTitle来跳转到不同的界面.
如图:
下面是重压后上拉动作.
首先,我们在Viewcontroller里面创建一个tableView,并让他拥有简单的3DTouch效果,代码如下:
#import "ViewController.h"
#import "SearchView.h"
#import "SecondViewController.h"
@interface ViewController ()
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *cellArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"第一页";
_tableView = [[UITableView alloc]initWithFrame:self.view.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
_cellArray = @[@"第一页",@"第二页",@"搜索"];
[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)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)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
现在的效果是这样:
怎么实现上拉效果呢.是这样的,比如第二页有个上拉效果,那么我们在具体的页面写个方法就可以了,也就是在SecondViewcontroller.m里面写这个方法:
-(NSArray> *)previewActionItems
{
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"进去" 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;
}
这样,第一次重压第二页出现绿色小框后再上拉会出现"进去"和"取消"两个选项.同理,在searchView里面也是这样,第一页没有上拉,所以当第一次重压第一页后上面没有上拉的箭头.大家看看效果图吧:
好了,代码就这些,其实也并不难. github下载地址