iOS 3D touch 开发

来源:互联网 发布:centos minimal无法scp 编辑:程序博客网 时间:2024/05/17 07:29

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;

    

    returnYES;

}

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.imagedrawInRect: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
原创粉丝点击