2016-07-27 11:00:09 shenjie12345678 阅读数 2271

好久没有写过文章了,今天就给大家带来一篇比较简单的开发教程吧!目的是为了让大伙能够避开那些不必要的坑,快速高效的实现功能。今天呢来和大家讲一下iOS9的新特性3DTouch开发(iOS10都已经出了Beta版,这篇教程真是来的有点晚……)

3DTouch的功能就是用户可以在HomeScreen上通过用力按压屏幕,使屏幕弹出快捷菜单,就好像是我们在Windows上右键鼠标一样,效果图如下:
这里写图片描述


实现3DTouch有两种方式,一种是通过配置info.Plist来实现的静态方式,另一种是通过代码控制的动态方式。

静态方式实现3DTouch

实现info.Plist中的如下配置

<array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeShare</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>分享</string>
            <key>UIApplicationShortcutItemType</key>
            <string>3dtouch.share</string>
        </dict>
    </array>

解释一下:
1.UIApplicationShortcutItemIconType 3DTouch图标的类型,例如分享,下载,播放,搜索等等
它的值可以选如下这些:

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,
    UIApplicationShortcutIconTypePlay,
    UIApplicationShortcutIconTypePause,
    UIApplicationShortcutIconTypeAdd,
    UIApplicationShortcutIconTypeLocation,
    UIApplicationShortcutIconTypeSearch,
    UIApplicationShortcutIconTypeShare,
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;

2.UIApplicationShortcutItemTitle 3DTouch显示的标题,这个值不能没有
3.UIApplicationShortcutItemType 3DTouch的标识符 ,这个值不能没有,我们通过这个标识符来判断触发的是哪个按钮。
4.除了这几个必要的值以为,我们还可以往里面传入例如:
UIApplicationShortcutItemSubtitle 设置标签的副标题
UIApplicationShortcutItemIconFile 设置标签的Icon文件
UIApplicationShortcutItemUserInfo:字典信息,如传值使用

info.Plist设置完了以后,我们还要加入捕获操作的的响应代码,我们在AppDelegate.m中加入如下代码:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

在逻辑中使用shortcutItem.type来判断是哪个按钮触发的消息。

通过info.Plist得到的效果图如下:

这里写图片描述


2.动态方式实现3DTouch

在AppDelegate.m中加入如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"+++++++didFinishLaunchingWithOptions+++++++");
    //3D Touch按压程序图标的快捷项
    //快捷菜单的图标
    UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
    UIApplicationShortcutIcon *icon2=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutIcon *icon3=[UIApplicationShortcutIcon iconWithTemplateImageName:@"icon.png"];

    //快捷菜单
    UIApplicationShortcutItem *item1=[[UIApplicationShortcutItem alloc]initWithType:@"test1"
                                                                     localizedTitle:@"测试1"
                                                                  localizedSubtitle:nil
                                                                               icon:icon1
                                                                           userInfo:nil];
    UIApplicationShortcutItem *item2=[[UIApplicationShortcutItem alloc]initWithType:@"test2"
                                                                     localizedTitle:@"测试2"
                                                                  localizedSubtitle:@"这是测试2"
                                                                               icon:icon2
                                                                           userInfo:nil];
    UIApplicationShortcutItem *item3=[[UIApplicationShortcutItem alloc]initWithType:@"测试3"
                                                                     localizedTitle:@"搜索"
                                                                  localizedSubtitle:nil
                                                                               icon:icon3
                                                                           userInfo:nil];
    //设置app的快捷菜单
    [[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3]];

    return YES;
}

在上述代码中,我动态的创建了3个3DTouch的选项,当我们通过标签进入app时,就会在appdelegate中调用这样一个回调,我们可以获取shortcutItem的信息进行相关逻辑操作:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

    NSString *title = nil;
    if([shortcutItem.type isEqualToString:@"test1"]){
        title=@"测试1";
    }else if([shortcutItem.type isEqualToString:@"test2"]){
        title=@"测试2";
    }else if([shortcutItem.type isEqualToString:@"test3"]){
        title=@"测试3";
    }

    //这里就弹个框子意思一下
    //由于UIAlertView在iOS 9被废弃,因此选用UIAlertController
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示"
                                                                          message:[NSString stringWithFormat:@"你点击了“%@”",title]
                                                                   preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action=[UIAlertAction actionWithTitle:@"知道了"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction  *action) {
                                                    [alertController dismissViewControllerAnimated:YES completion:nil];
                                                }];
    [alertController addAction:action];
    [self.window.rootViewController presentViewController:alertController
                                                 animated:YES
                                               completion:nil];
}

这样我们的动态创建方式也完成了。


总结

因为3DTouch的预览功能不太好演示,所以在此就不在做操作了,大家可以通过头文件来熟悉一下它的一些特性,另外3DTouch在UIView中也增加了预览的功能,下篇文章,我们再来讲下关于3DTouch的Pop和Peek功能。

2018-01-03 02:40:19 weixin_34055910 阅读数 9

6s和6s plus之后特有效果,对着应用图标用力按会触发3DTouch .

###第一步 : 3DTouch 设备支持检测:

检测当前的设备是否支持3DTouch

//  在iOS9中有一个新的枚举
 typedef NS_ENUM(NSInteger, UIForceTouchCapability) {  
            UIForceTouchCapabilityUnknown        = 0,  // 未知的支持属性
            UIForceTouchCapabilityUnavailable    = 1,  // 不支持
            UIForceTouchCapabilityAvailable      = 2 // 支持
  };
复制代码

一般我们都在每个ViewController的生命周期中这样做:

定义一个是否设备支持的BOOL值属性

 @property (nonatomic , assign) BOOL support3DTouch; 
复制代码

在生命周期函数中检测支持与否

  - (void)viewWillAppear:(BOOL)animated {  
     [super viewWillAppear:animated];  
      //检测当前是否支持3DTouch  
      self.support3DTouch = [self support3DTouch];  
  }
复制代码

在生命周期外检测支持与否(因为有可能出了生命周期函数而发生了变化)

  - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0) {     
      self.support3DTouch = [self support3DTouch];  
  } 
复制代码

检测是否支持3DTouch的方法

  - (BOOL)support3DTouch  
  {  
      // 如果开启了3D touch  
      if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)  
      {  
          return YES;  
      }  
          return NO;  
      }  
  }  
复制代码

第二步 : 配置快捷视图列表

创建快捷视图列表有两种方法: 1,一种是编辑info.plist文件中的UIApplicationShortcutItems, 通过可视化的界面添加键值对直接配置info.plist

2,另一种是使用代码在工程中加入items
在工程的 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    [self.window makeKeyAndVisible];
    
    //  代码创建快捷视图列表的方法,
     [self create3DTouchShotItems];

    return YES;
}

- (void)create3DTouchShotItems {
    //创建快捷item的icon UIApplicationShortcutItemIconFile
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon3"];
    
    //创建快捷item的userinfo UIApplicationShortcutItemUserInfo
    NSDictionary *info1 = @{@"url":@"url1"};
    NSDictionary *info2 = @{@"url":@"url2"};
    NSDictionary *info3 = @{@"url":@"url3"};
    
    //创建ShortcutItem
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_1" localizedTitle:@"扫一扫" localizedSubtitle:@"" icon:icon1 userInfo:info1];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_2" localizedTitle:@"smile" localizedSubtitle:@"微笑面对生活" icon:icon2 userInfo:info2];
    UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_3" localizedTitle:@"购物" localizedSubtitle:@"Shopping" icon:icon3 userInfo:info3];
    
    NSArray *items = @[item1, item2, item3];
    [UIApplication sharedApplication].shortcutItems = items;
}
复制代码

第三步 : 给列表视图中的cell注册 3DTouch 事件

1,首先,在首页当前控制器里遵守UIViewControllerPreviewingDelegate协议 UIViewControllerPreviewingDelegate 2,在注册前先判断是否设备支持(也就是第一步)

3,注册: [self registerForPreviewingWithDelegate:self sourceView:cell];

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLTableViewCell"];
    if (cell == nil) {
        cell = [ZLTableViewCell cellWithTableView:tableView];
    }
    cell.dataFrame = self.dataSource[indexPath.row];
    //给cell注册代理,使其支持3DTouch手势
    if (self.support3DTouch) {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
    
    return cell;
}
复制代码

###第四步: 完成UIViewControllerPreviewingDelegate 协议回调,实现Peek Pop 在首页当前控制器里,

#pragma mark - 3DTouch  UIViewControllerPreviewingDelegate
复制代码

Peek 实现代码:

// 此方法是轻按控件时,跳出peek的代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    //防止重复加入
    if ([self.presentedViewController isKindOfClass:[ZLPeekViewController class]])
    {
        return nil;
    }
    else
    {
        ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
        ZLCellData * cellData = cell.dataFrame.cellData;
        ZLPeekViewController *peekViewController = [[ZLPeekViewController alloc] init];
        peekViewController.cellData = cellData;
        peekViewController.delegate = self;
        return peekViewController;
    }
}
复制代码

Pop 代码

//此方法是重按peek时,跳入pop的代理方法
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
    commitViewController:(UIViewController *)viewControllerToCommit {
    
    ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
    ZLCellData * cellData = cell.dataFrame.cellData;
    ZLPopViewController *popViewController = [[ZLPopViewController alloc] init];
    popViewController.cellData = cellData;
    // 以prentViewController的形式展现
    [self showViewController:popViewController sender:self];
    
    // 以push的形势展现
//    [self.navigationController pushViewController:popViewController animated:YES];
}
复制代码

###第五步 : 在Peek状态下向上滑动出现的按钮配置方法

在 ZLPeekViewController.m 里, 实现 - (NSArray> *)previewActionItems 回调方法

#pragma mark - Preview Actions

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    // 生成UIPreviewAction
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"事件 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 selected");
        [self.delegate pushToPopViewControllerWithCellData:self.cellData];
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"事件 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 2 selected");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"事件 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 3 selected");
    }];
    
    UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"按钮 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 1 selected");
    }];
    
    UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"按钮 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 2 selected");
    }];
    
    UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"按钮 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 3 selected");
    }];
    
    NSArray *actions = @[action1, action2, action3];
    NSArray *taps = @[tap1, tap2, tap3];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"一组事件" style:UIPreviewActionStyleDefault actions:actions];
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"一组按钮" style:UIPreviewActionStyleDefault actions:taps];
    NSArray *group = @[group1,group2];
    
    //当然你也可以返回三个单独的action对象的数组,而不是group,具体效果,可以自己试一下
    
    return group;
}
复制代码

现在可以测试喽, 看下效果吧, 如果需要demo可以去我的 GitHub 上下载~

2018-02-20 12:05:19 findhappy117 阅读数 122

概述

3DTouch是一种立体触控技术,被苹果称为新一代多点触控技术.

详细

6s和6s plus之后特有效果,对着应用图标用力按会触发3DTouch .

一、程序实现

第一步 : 3DTouch 设备支持检测:

检测当前的设备是否支持3DTouch

//  在iOS9中有一个新的枚举
 typedef NS_ENUM(NSInteger, UIForceTouchCapability) {  
            UIForceTouchCapabilityUnknown        = 0,  // 未知的支持属性
            UIForceTouchCapabilityUnavailable    = 1,  // 不支持
            UIForceTouchCapabilityAvailable      = 2 // 支持
  };

一般我们都在每个ViewController的生命周期中这样做:

定义一个是否设备支持的BOOL值属性

@property (nonatomic , assign) BOOL support3DTouch;

在生命周期函数中检测支持与否

  - (void)viewWillAppear:(BOOL)animated {  
     [super viewWillAppear:animated];  
      //检测当前是否支持3DTouch  
      self.support3DTouch = [self support3DTouch];  
  }

在生命周期外检测支持与否(因为有可能出了生命周期函数而发生了变化)

  - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0) {     
      self.support3DTouch = [self support3DTouch];  
  }

检测是否支持3DTouch的方法

 - (BOOL)support3DTouch  
  {  
      // 如果开启了3D touch  
      if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)  
      {  
          return YES;  
      }  
          return NO;  
      }  
  }


第二步 : 配置快捷视图列表

创建快捷视图列表有两种方法:

1,一种是编辑info.plist文件中的UIApplicationShortcutItems,

通过可视化的界面添加键值对直接配置info.plist

576025-2d106da92a5e28d5.png

2,另一种是使用代码在工程中加入items

在工程的 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    [self.window makeKeyAndVisible];
    
    //  代码创建快捷视图列表的方法,
     [self create3DTouchShotItems];
    return YES;
}

代码创建快捷视图列表的方法:

- (void)create3DTouchShotItems {
    //创建快捷item的icon UIApplicationShortcutItemIconFile
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon3"];
    
    //创建快捷item的userinfo UIApplicationShortcutItemUserInfo
    NSDictionary *info1 = @{@"url":@"url1"};
    NSDictionary *info2 = @{@"url":@"url2"};
    NSDictionary *info3 = @{@"url":@"url3"};
    
    //创建ShortcutItem
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_1" localizedTitle:@"扫一扫" localizedSubtitle:@"" icon:icon1 userInfo:info1];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_2" localizedTitle:@"smile" localizedSubtitle:@"微笑面对生活" icon:icon2 userInfo:info2];
    UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_3" localizedTitle:@"购物" localizedSubtitle:@"Shopping" icon:icon3 userInfo:info3];
    
    NSArray *items = @[item1, item2, item3];
    [UIApplication sharedApplication].shortcutItems = items;
}


第三步 : 给列表视图中的cell注册 3DTouch 事件

  • 1,首先,在首页当前控制器里遵守UIViewControllerPreviewingDelegate协议

  • UIViewControllerPreviewingDelegate

  • 2,在注册前先判断是否设备支持(也就是第一步)

  • 3,注册: [self registerForPreviewingWithDelegate:self sourceView:cell];

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLTableViewCell"];
    if (cell == nil) {
        cell = [ZLTableViewCell cellWithTableView:tableView];
    }
    cell.dataFrame = self.dataSource[indexPath.row];
    //给cell注册代理,使其支持3DTouch手势
    if (self.support3DTouch) {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
    
    return cell;
}


第四步: 完成UIViewControllerPreviewingDelegate 协议回调,实现Peek Pop

在首页当前控制器里,

#pragma mark - 3DTouch  UIViewControllerPreviewingDelegate

Peek 实现代码:

此方法是轻按控件时,跳出peek的代理方法

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    //防止重复加入
    if ([self.presentedViewController isKindOfClass:[ZLPeekViewController class]])
    {
        return nil;
    }
    else
    {
        ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
        ZLCellData * cellData = cell.dataFrame.cellData;
        ZLPeekViewController *peekViewController = [[ZLPeekViewController alloc] init];
        peekViewController.cellData = cellData;
        peekViewController.delegate = self;
        return peekViewController;
    }
}

Pop 代码:

此方法是重按peek时,跳入pop的代理方法

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
    commitViewController:(UIViewController *)viewControllerToCommit {
    
    ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
    ZLCellData * cellData = cell.dataFrame.cellData;
    ZLPopViewController *popViewController = [[ZLPopViewController alloc] init];
    popViewController.cellData = cellData;
    // 以prentViewController的形式展现
    [self showViewController:popViewController sender:self];
    
    // 以push的形势展现
//    [self.navigationController pushViewController:popViewController animated:YES];
}


第五步 : 在Peek状态下向上滑动出现的按钮配置方法

在 ZLPeekViewController.m 里, 实现 - (NSArray> *)previewActionItems 回调方法

#pragma mark - Preview Actions
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    // 生成UIPreviewAction
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"事件 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 selected");
        [self.delegate pushToPopViewControllerWithCellData:self.cellData];
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"事件 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 2 selected");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"事件 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 3 selected");
    }];
    
    UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"按钮 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 1 selected");
    }];
    
    UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"按钮 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 2 selected");
    }];
    
    UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"按钮 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 3 selected");
    }];
    
    NSArray *actions = @[action1, action2, action3];
    NSArray *taps = @[tap1, tap2, tap3];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"一组事件" style:UIPreviewActionStyleDefault actions:actions];
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"一组按钮" style:UIPreviewActionStyleDefault actions:taps];
    NSArray *group = @[group1,group2];
    
    //当然你也可以返回三个单独的action对象的数组,而不是group,具体效果,可以自己试一下
    
    return group;
}

二、运行效果与文件截图

1、运行效果截图:

576025-b98a06c5b91e59ee.png

2、文件截图:

2.jpg

三、其他补充

界面性问题可以根据自己项目需求调整即可, 具体可参考代码, 项目能够直接运行!

注:本文著作权归作者,由demo大师(http://www.demodashi.com)宣传,拒绝转载,转载需要作者授权


2016-07-20 15:57:12 yj229201093 阅读数 1086

一、前言
因最近入手一个6sp, 看着3DTouch 挺好玩的,就想自己实现下,
3D Touch 听上去好高端,很难开发的样子,但原来那么简单,往往看着复杂高端的的东西其实简单。因为很多都是现成可用的。
好了 上代码

二、配置、设置、实现
1.静态配置 & 动态 配置
1.1 静态配置 首先打开plist文件
1.2 在plist添加 UIApplicationShortcutItems 添加item
这里写图片描述

注释:
UIApplicationShortcutItemType(必填项) 这个键值设置一个快捷通道类型的字符串
UIApplicationShortcutItemTitle (必填项)  这个键值设置标签的标题
UIApplicationShortcutItemSubtitle (可选项)  设置标签的副标题
UIApplicationShortcutItemIconType (可选项)  设置标签Icon类型

2.动态配置
在appDelegate.m文件中 设置, 我喜欢在代码中设置,方便
在appDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      //  创建ShortcutItem
      [self createShortcutItem]; // 设置 3D touch 快捷选项

    UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
    //如果是从快捷选项标签启动app,则根据不同标识执行不同操作,然后返回NO,防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

    if (shortcutItem) {
        if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.pay"]) {
            //  进入发工资界面
            GFBLog(@"进入发工资界面");
            XZHPaySalaryProjectVC *paySalaryVC = [[XZHPaySalaryProjectVC alloc] initWithNibName:@"XZHPaySalaryProjectVC" bundle:nil];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:paySalaryVC];
            self.window.rootViewController = nav;

        } else if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.myQRCode"]) {
            // 进入我的二维码界面
            GFBLog(@"我的二维码界面");
        } else if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.RichScan"]){
            // 进入扫一扫
            GFBLog(@"进入扫一扫");
            XZHScanCodeViewController *scanCodeVC = [[XZHScanCodeViewController alloc] init];
            scanCodeVC.pushScanCodeType = MainPush;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scanCodeVC];
            self.window.rootViewController = nav;
        }
        return NO;
    }
      return YES
   }
/**

 *  3D touch 快捷选项

 */

- (void) createShortcutItem {
    // 创建系统风格的icon
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"HomePage_Scan"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"red-envelope"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"blackberry-qr-code-variant"];

    //创建快捷选项
    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.gonghuipay.gongfubao.RichScan" localizedTitle:@"扫一扫" localizedSubtitle:@"" icon:icon1 userInfo:nil];
    UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"com.gonghuipay.gongfubao.pay" localizedTitle:@"发工资" localizedSubtitle:@"" icon:icon2 userInfo:nil];
    UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem alloc]initWithType:@"com.gonghuipay.gongfubao.myQRCode" localizedTitle:@"我的二维码" localizedSubtitle:@"" icon:icon3 userInfo:nil];


    //添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[item, item1, item2];
}
  1. 设置相应事件
// 3D Touch 响应事件
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
  completionHandler:(void(^)(BOOL succeeded))completionHandler{

    //判断先前我们设置的唯一标识
    if (shortcutItem) {
        if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.pay"]) {
            //  进入发工资界面
            GFBLog(@"进入发工资界面");
            XZHPaySalaryProjectVC *paySalaryVC = [[XZHPaySalaryProjectVC alloc] initWithNibName:@"XZHPaySalaryProjectVC" bundle:nil];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:paySalaryVC];
            self.window.rootViewController = nav;

        } else if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.myQRCode"]) {
            // 进入我的二维码界面
            GFBLog(@"我的二维码界面");
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LCMinePage" bundle:nil];
            LCQRCodeInformationViewController *codeInformationVC = [storyboard instantiateViewControllerWithIdentifier:@"LCQRCodeInformationViewController"];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:codeInformationVC];
            self.window.rootViewController = nav;

        } else if ([shortcutItem.type isEqualToString:@"com.gonghuipay.gongfubao.RichScan"]){
            // 进入扫一扫
            GFBLog(@"进入扫一扫");
            XZHScanCodeViewController *scanCodeVC = [[XZHScanCodeViewController alloc] init];
            scanCodeVC.pushScanCodeType = MainPush;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scanCodeVC];
            self.window.rootViewController = nav;

        }
    }

    if (completionHandler) {
        completionHandler(YES);
    }
}

以上就完成了,可以测试

三、效果
这里写图片描述
简单吧····

四、总结
但这边有一个问题,就是第一次刚安装应用程序的时候, shortitemsmenu 启动不起来,需要启动下app 才可以。不懂原因,知道的可以告知下。

因公司项目测试的,无demo,不懂的可以发邮一起探讨 229201093@qq.com

参考链接:http://www.cnblogs.com/zhanglinfeng/p/5133939.html

最近在写Swift项目,后期有时间写一个Swift版本

2016-10-19 14:18:44 chenzheiu1210 阅读数 606

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    /**
     *  @author 李凯利, 16-10-19 11:10:30
     *
     *  UIApplicationShortcutIcon :设置每个item项目的图标 可以是系统的也可以是自定义图标,此类提供两个初始化方法来供我们选择:iconWithType:方法设置系统图标
     iconWithTemplatelmageName:方法设置自定义图标
     
     UIApplicationShortcutItem类中的属性: 
     type:标识每一个Item项目,通过标识我们可以区分点击相应每一项该触发哪些操作(在3Dtouchu的代理方法中就是靠type 属性来区分点击的那一项)
        localizedTitle:每一个item 的标题
     localizedSubtitle:每个item的副标题 可有可无
     icon:每个item对应的图标
     userInfo:属性的信息字典,用于传值
     */
    //栏目一
    UIApplicationShortcutIcon * icon1 =[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];
    UIApplicationShortcutItem* item1 =[[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"入口1" localizedSubtitle:@"入口1副标题" icon:icon1 userInfo:nil];
    //栏目二
    UIApplicationShortcutIcon * icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
    UIApplicationShortcutItem * item2 =[[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"入口二" localizedSubtitle:@"入口二副标题" icon:icon2 userInfo:nil];
    //栏目3
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePause];
    UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc] initWithType:@"item3" localizedTitle:@"入口3" localizedSubtitle:@"入口3副标题" icon:icon3 userInfo:nil];
    
    //栏目4
    UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"item4" localizedTitle:@"入口4" localizedSubtitle:@"入口4副标题" icon:icon4 userInfo:nil];
    //设置shortcutItems 需要注意的是shortcutItems 数组最多只能加入四个item,超过四个只会显示前面的四个
    application.shortcutItems = @[item1,item2,item3,item4];
    
    return YES;
}
#pragma mark ----3DTouchu的代理方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    //根据item对应的type标识处理对应的点击操作
    NSString * itemType = shortcutItem.type;
    if ([@"item1" isEqualToString:itemType]) {
        NSLog(@"item1");
    }
    else if ([@"item2" isEqualToString:itemType]){
        NSLog(@"item2");
    }
    else if ([@"item3" isEqualToString:itemType]){
        NSLog(@"item3");
    }
    else if ([@"item4" isEqualToString:itemType]){
        NSLog(@"item4");
    }
    
}

iOS 3DTouch 引用

阅读数 214

Unity之使用IOS 3DTouch

博文 来自: qq_26999509

IOS 3DTouch

阅读数 284

没有更多推荐了,返回首页