iOS 3D touch 开发

1.在AppDelegate.m中添加方法,在6s设备上实现了弹出小窗口的效果,点击默认打开应用程序,但是不会执行相关方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    

    UIApplicationShortcutItem *itme1 = [[UIApplicationShortcutItemalloc]initWithType:@"能行不66"localizedTitle:@"能行不"];

    UIApplicationShortcutItem *itme2 = [[UIApplicationShortcutItemalloc]initWithType:@"我了擦66"localizedTitle:@"我了擦"];

    UIApplicationShortcutItem *itme3 = [[UIApplicationShortcutItemalloc]initWithType:@"王八蛋66"localizedTitle:@"王八蛋"];

    

    NSArray *array = @[itme1,itme2,itme3];

    

    [UIApplicationsharedApplication].shortcutItems = array;

    

    return YES;

}

2.在AppDelegate.m中添加如下回掉方法,可以判断点击按钮

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

   

    if ([shortcutItem.localizedTitleisEqualToString:@"王八蛋"]) {

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"王八蛋"message:@" 是王慧"delegate:selfcancelButtonTitle:@"好的"otherButtonTitles:nil,nil];

        

        [alert show];

    }

    

    NSLog(@"%@",shortcutItem.localizedTitle);

    

}

自此你已经能够通过对桌面上应用图标进行按压手势的了,会弹出一个小窗口,显示 shortcutItems



下面这段代码写在ViewController中,实现的是一个简单的画板功能,它可以根据你按压的力度自动调节画笔的宽度


@interface ViewController ()

{

    CGPoint touchPoint;  //手指的位置

    UIImageView *canDraw;  //画布图片

}

@end


通过touchesBegan 和 touchesMoved 的方法实现画布的功能

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

    touchPoint = [touch locationInView:canDraw];

}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

    CGPoint currentPoint = [touch locationInView:canDraw];

    

    UIGraphicsBeginImageContext(canDraw.frame.size);

    [canDraw.image drawInRect:CGRectMake(0.0, 0.0, canDraw.frame.size.width, canDraw.frame.size.height)];

    

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    

    float lineWidthc = 10.0;


//这个就是力度啦~~~~

    if ([touch respondsToSelector:@selector(force)]) {

        NSLog(@"force:%f", touch.force);

        lineWidthc = lineWidthc * touch.force;

    }

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidthc);

    

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), touchPoint.x, touchPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    canDraw.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    touchPoint = currentPoint;

    

}



本文是整理XXX的demo写的,十分感谢XXX的demo,不过不好意思忘了当时在哪下载的,也忘了您的大名了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值