3D Touch

来源:互联网 发布:gta5精简优化版 编辑:程序博客网 时间:2024/06/15 19:51
//1,桌面重压app图标/**需要在info.plist中进行配置    添加:UIApplicationShortcutItems   (数组,元素是字典,多个title就多个item);    字典中有5个key,    必要的(required):    UIApplicationShortcutItemType:唯一标识的字符串,用来在代码中进行判断点击的哪个title    UIApplicationShortcutItemTitle:标题    可选的(optional)    UIApplicationShortcutItemSubtitle:子标题,显示在title下面,如果指定了subtitle,title只会显示一行,显示不下的以...的形式显示    UIApplicationShortcutItemIconFile:图片名字(自定义)    UIApplicationShortcutItemIconType:枚举,系统自带的图片,包括:UIApplicationShortcutIconTypeCompose之类的,可以点进去看的,或者参考http://blog.csdn.net/baidu_32469997/article/details/51192500    UIApplicationShortcutItemUserInfo:字典类型,具体用处不清楚*///在APPDelegate中- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {//这里根据UIApplicationShortcutItemType来判断要做的动作    if ([shortcutItem.type isEqualToString:@"share"]) {        UIButton *btn = [[UIButton alloc]initWithFrame:[UIScreen mainScreen].bounds];        [btn setImage:[UIImage imageNamed:@"shareLogo"] forState:UIControlStateNormal];        [[[UIApplication sharedApplication] keyWindow] addSubview:btn];        [btn setBackgroundColor:[UIColor whiteColor]];        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    }    else if ([shortcutItem.type isEqualToString:@"dianij"]) {        NSLog(@"share");    }}
//例:    <key>UIApplicationShortcutItems</key>    <!--数组,可以设置多个选项-->    <array>        <dict>            <!--图片,暂时是纯黑的,可以空心-->            <key>UIApplicationShortcutItemIconFile</key>            <string>wt_unity_refreshing</string>            <!--标题-->            <key>UIApplicationShortcutItemTitle</key>            <string>标题</string>            <!--唯一标识的字符串,用来在代码中进行判断-->            <key>UIApplicationShortcutItemType</key>            <string>dianji</string>        </dict>        <dict>            <!--唯一标识的字符串,用来在代码中进行判断-->            <key>UIApplicationShortcutItemType</key>            <string>share</string>            <!--标题-->            <key>UIApplicationShortcutItemTitle</key>            <string>选择</string>            <!--图片-->            <key>UIApplicationShortcutItemIconFile</key>            <string>wt_unity_shareLogo</string>        </dict>    </array>
//二.在viewController中使用3D Touch//1.控制器注册成为3DTouch响应[self registerForPreviewingWithDelegate:self sourceView:_tableView];//2.实现 UIViewControllerPreviewingDelegate 协议- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {    //location是相对于previewingContext.sourceView的view中的位置    UITableView *table = (UITableView *)previewingContext.sourceView;    //这是目前我能想到的一个确定点击哪个cell的方法,如果cell高度不固定暂时还不知道怎么弄 ,    UITableViewCell *cell =[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:(NSInteger)location.y/cellHeight]];    SecondViewController *secondVC = [[SecondViewController alloc]init];    [secondVC.titleLable setText:cell.textLabel.text];    //要有上滑手势还需要在secondViewController中重写 - (NSArray<id<UIPreviewActionItem>> *)previewActionItems 方法    return secondVC;}- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {    [self showViewController:viewControllerToCommit sender:self];}//3.SecondViewController中重写 - (NSArray<id<UIPreviewActionItem>> *)previewActionItems方法- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {//三个type可以自己都试一下看看效果    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"title1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"title1");    }];    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"title2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"selected");    }];    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"title3" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"销毁");//UIPreviewActionStyleDestructive应该是默认字体颜色是红色    }];    return @[action1, action2, action3];}
0 0
原创粉丝点击