手机振动,摇一摇,截屏

添加AudioToolbox.framework  //然后在头文件中声明 #import//声音提示
#import 
// 声音 振动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振动
AudioServicesPlaySystemSound(1007);//声音

//注册通知  iOS7提供一个崭新的推送方法:UIApplicationUserDidTakeScreenshotNotification。只要像往常一样订阅即可知道什么时候截图了。
    //    注意:UIApplicationUserDidTakeScreenshotNotification 将会在截图完成之后显示。现在在截图截取之前无法得到通知。
    //    希望苹果会在iOS8当中增加 UIApplicationUserWillTakeScreenshotNotification。(只有did, 没有will显然不是苹果的风格...)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    //人为截屏, 模拟用户截屏行为, 获取所截图片
    
    UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
    self.button  = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [keyWindow addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];



-(void)buttonAction
{
    self.button .hidden = YES;
    _show = NO;
}

show(属性) 初始NO 截屏后YES 截屏操作后NO 用来防止连续多次截屏
#pragma mark - UIApplicationUserDidTakeScreenshotNotification - 检测到截屏
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    NSLog(@"检测到截屏");
    if (!_show) {
        
        _show = YES;
        
        //人为截屏, 模拟用户截屏行为, 获取所截图片
        UIImage *image_ = [self imageWithScreenshot];
//        UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
        
        self.button .hidden = NO;
        [self.button setImage:image_ forState:(UIControlStateNormal)];
        
    }
}

/**
 *  返回截取到的图片
 *
 *  @return UIImage *
 */
- (UIImage *)imageWithScreenshot
{
    NSData *imageData = [self dataWithScreenshotInPNGFormat];
    return [UIImage imageWithData:imageData];
}

/**
 *  截取当前屏幕
 *
 *  @return NSData *
 */
- (NSData *)dataWithScreenshotInPNGFormat
{
    CGSize imageSize = CGSizeZero;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
   if (UIInterfaceOrientationIsPortrait(orientation)){
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    } 
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]){
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if (orientation == UIInterfaceOrientationLandscapeLeft){
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        }else if (orientation == UIInterfaceOrientationLandscapeRight){
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]){
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        }else{
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return UIImagePNGRepresentation(image);
}




//摇一摇的时候 写在(viewDidLoad){[self becomeFirstResponder];}中  这句话很重要  这是把当前VC设置为第一响应者
#pragma mark - 摇一摇       
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //检测到摇动
}

-
(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
    //摇动取消
}

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
    //摇动结束
    if(event.subtype == UIEventSubtypeMotionShake) {
        //somethinghappens
    }
}

你可能感兴趣的:(手机振动,摇一摇,截屏)