3D Touch2

上一篇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)到不同的界面.
如圖:

點(diǎn)擊不同item進(jì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下載地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末昔榴,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子仿畸,更是在濱河造成了極大的恐慌鞋诗,老刑警劉巖融涣,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件侧漓,死亡現(xiàn)場(chǎng)離奇詭異咒劲,居然都是意外死亡继薛,警方通過(guò)查閱死者的電腦和手機(jī)盖腿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)爽待,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事堕伪∫咀” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵欠雌,是天一觀的道長(zhǎng)蹄梢。 經(jīng)常有香客問(wèn)我,道長(zhǎng)富俄,這世上最難降的妖魔是什么禁炒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮霍比,結(jié)果婚禮上幕袱,老公的妹妹穿的比我還像新娘。我一直安慰自己悠瞬,他們只是感情好们豌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著浅妆,像睡著了一般望迎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凌外,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天辩尊,我揣著相機(jī)與錄音,去河邊找鬼康辑。 笑死摄欲,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的疮薇。 我是一名探鬼主播胸墙,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼按咒!你這毒婦竟也來(lái)了迟隅?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤胖齐,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后嗽冒,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體呀伙,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年添坊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剿另。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖雨女,靈堂內(nèi)的尸體忽然破棺而出谚攒,到底是詐尸還是另有隱情,我是刑警寧澤氛堕,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布馏臭,位于F島的核電站,受9級(jí)特大地震影響讼稚,放射性物質(zhì)發(fā)生泄漏括儒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一锐想、第九天 我趴在偏房一處隱蔽的房頂上張望帮寻。 院中可真熱鬧,春花似錦赠摇、人聲如沸固逗。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)烫罩。三九已至,卻和暖如春耘戚,著一層夾襖步出監(jiān)牢的瞬間嗡髓,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工收津, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留饿这,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓撞秋,卻偏偏與公主長(zhǎng)得像长捧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吻贿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容