(iOS9新特性——3Dtouchaa
http://blog.csdn.net/tx874828503/article/details/48786003
)
引言
在iphone6s问世之后,很多果粉都争先要体验3D Touch给用户带来的额外维度上的交互,这个设计之所以叫做3D Touch,其原理上是增加了一个压力的感触,通过区分轻按和重按来进行不同的用户交互。
UIApplicationShortcutItem 创建3DTouch标签的类
UIMutableApplicationShortcutItem 创建可变的3DTouch标签的类(//这个类继承于 UIApplicationShortcutItem,创建的标签可变 )
▪ UIApplicationShortcutIcon 创建标签中图片Icon的类(//这个类创建标签中的icon
▪@interface UIApplicationShortcutIcon : NSObject <NSCopying>
▪//创建系统风格的icon
▪+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
▪//创建自定义的图片icon
▪+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
▪@end
)
这里,将系统风格icon的枚举列举如下:(9.1后有更多)
▪ typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
▪ UIApplicationShortcutIconTypeCompose,//编辑的图标
▪ UIApplicationShortcutIconTypePlay,//播放图标
▪ UIApplicationShortcutIconTypePause,//暂停图标
▪ UIApplicationShortcutIconTypeAdd,//添加图标
▪ UIApplicationShortcutIconTypeLocation,//定位图标
▪ UIApplicationShortcutIconTypeSearch,//搜索图标
▪ UIApplicationShortcutIconTypeShare//分享图标
} NS_ENUM_AVAILABLE_IOS(9_0);
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
也需要进行一下判断,在launchOptions中有UIApplicationLaunchOptionsShortcutItemKey这样一个键,通过它,我们可以区别是否是从标签进入的app,如果是则处理结束逻辑后,返回NO,防止处理逻辑被反复回调。
几点注意:
1、快捷标签最多可以创建四个,包括静态的和动态的。
2、每个标签的题目和icon最多两行,多出的会用...省略
代码:
1.(动态)
//第二种
UIMutableApplicationShortcutItem *shortItem1 = [[UIMutableApplicationShortcutItem alloc] initWithType: @"one"localizedTitle: @"one" localizedSubtitle: @"one subtitle" icon: [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd] userInfo: nil];
NSArray *shortItems = [[NSArray alloc] initWithObjects: shortItem1, nil];
shortItem1.icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
[[UIApplication sharedApplication] setShortcutItems: shortItems];
//第一种
// UIApplicationShortcutItem *shortItem1 =[[UIApplicationShortcutItem alloc] initWithType:@"斋打开"localizedTitle:@"斋打开"];
// UIApplicationShortcutItem *shortItem2 =[[UIApplicationShortcutItem alloc] initWithType:@"弹框"localizedTitle:@"弹框"];
// UIApplicationShortcutItem *shortItem3 =[[UIApplicationShortcutItem alloc] initWithType: @"跳转"localizedTitle: @"gohome" localizedSubtitle: nil icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypePlay]userInfo: nil];
// UIApplicationShortcutItem *shortItem4 =[[UIApplicationShortcutItem alloc] initWithType: @"跳转"localizedTitle: @"something" localizedSubtitle: nil icon: [UIApplicationShortcutIconiconWithTemplateImageName: @"login_icon_user_in.png"] userInfo: nil];
//
// NSArray *shortItems = [[NSArray alloc]initWithObjects:shortItem1, shortItem2, shortItem3, shortItem4, nil];
//
// NSLog(@"%@", shortItems);
// [[UIApplication sharedApplication]setShortcutItems:shortItems];
//
//用于开发
// UIApplicationShortcutItem *item =[launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
// if (item)
// {
// NSLog(@"We've launched fromshortcut item: %@", item.localizedTitle);
// }
// else
// {
// NSLog(@"We've launchedproperly.");
// }
2. - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
if ([shortcutItem.localizedTitle isEqual: @"弹框"]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"OPPS!" message:@"我的CTO叫佛山强"delegate:self cancelButtonTitle:@"哦" otherButtonTitles:nil,nil];
[alert show];
return;
}else if ([shortcutItem.localizedTitle isEqual:@"gohome"])
{
if (![CommonUtil getObjectFromUD: @"UserInfo"]){
LoginViewController *login = [[LoginViewController alloc] init];
[self.navi pushViewController: login animated:NO];
}
}
}
效果图:
参考网址:
iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览http://blog.csdn.net/showhilllee/article/details/48376073
iOS 3D Touch (UIApplicationShortcutItem、UIViewControllerPreviewing、UIPreviewAction)
http://blog.csdn.net/liwenjie0912/article/details/49178873
接着这里要说的是 在 长按touchViewController 弹出 自定义的ViewContoller
▪首先 在ViewController.m 里面加入(这个就是要手指 长按并且要往下压的ViewController)- (void)viewDidLoad
▪{
▪ [super viewDidLoad];
▪ // Do any additionalsetup after loading the view, typically from a nib.
▪
▪ //首先要判断一下 压力感是否有效,跟着注册delegate
▪ [self check3DTouch];
▪}
▪
▪- (void)check3DTouch
▪{
▪
▪ // register for 3D Touch (if available)
▪ if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
▪ {
▪ [self registerForPreviewingWithDelegate:self sourceView:self.view];
▪ NSLog(@"3D Touch 可用!");
▪ }
▪ else
▪ {
▪ NSLog(@"3D Touch 无效");
▪ }
}
▪- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
▪{
▪ // check if we're not already displaying a preview controller
▪ //SecViewController 是要弹出悬浮展示的ViewController
▪ if ([self.presentedViewController isKindOfClass:[SecViewController class]]) {
▪ return nil;
▪ }
▪
▪ SecViewController *sec = [[SecViewController alloc] init];
▪
▪ return sec;
▪
}
未了方便的演示 我们在SecViewController 里面加上一个返回的手势
[objc] view plaincopy
▪UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissMe)];
▪ [self.view addGestureRecognizer:tap];
▪
▪ - (void)dismissMe{
▪ // dismiss this view controller
[self dismissViewControllerAnimated:YES completion:nil];
当弹出自定义的SecViewController之后 然后我们往上移动那么就会出现Action
这代码是写在SecViewController里面的
[objc] view plaincopy
▪ - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
▪
▪ // setup a list of preview actions
▪ UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"这里可以做你想要做的事情的Aciton" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
▪ NSLog(@"click");
▪ }];
▪
▪ // add them to an arrary
▪ //想要显示多个就定义多个 UIPreviewAction
▪ NSArray *actions = @[action1];
▪
▪ // and return them (return the array of actions instead to see all items ungrouped)
▪ return actions;
}
iOS9新特性——3Dtouchaa
http://blog.csdn.net/tx874828503/article/details/48786003
享趣客户端有例子
1. 签代理UIViewControllerPreviewingDelegate
2. 注册
- (void)viewDidLoad中
if (self.traitCollection.forceTouchCapability ==UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate: selfsourceView: self.view];
}else{
NSLog(@"3DTouch 无效");
}
3. 实现代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
// if([self.presentedViewController isKindOfClass: [SharedViewController class]]) {
//
// return nil;
// }
// SharedViewController你想要 弹出的ViewController
SharedViewController *share = [[SharedViewController alloc] init];
return share;
}
▪//这个方法是在什么时候出发呢?就是 给更加大的力度的时候进去 全屏状态
▪- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
▪
▪ // deep press: bring up the commit view controller (pop)
▪ [self showViewController:viewControllerToCommit sender:self];
}
4. 当弹出自定义的SharedViewController 之后 然后我们往上移动那么就会出现Action
这代码是写在SharedViewController 里面的
▪ - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
▪
▪ // setup a list of preview actions
▪ UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"这里可以做你想要做的事情的Aciton" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
▪ NSLog(@"click"); //点击回调方法
▪ }];
▪
▪ // add them to an arrary
▪ //想要显示多个就定义多个 UIPreviewAction
▪ NSArray *actions = @[action1];
▪
▪ // and return them (return the array of actions instead to see all items ungrouped)
▪ return actions;
}
效果图: