手機(jī)振動(dòng),搖一搖,截屏

添加AudioToolbox.framework  //然后在頭文件中聲明 #import//聲音提示
#import <AudioToolbox/AudioToolbox.h>
// 聲音 振動(dòng)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振動(dòng)
AudioServicesPlaySystemSound(1007);//聲音

//注冊(cè)通知  iOS7提供一個(gè)嶄新的推送方法:UIApplicationUserDidTakeScreenshotNotification育叁。只要像往常一樣訂閱即可知道什么時(shí)候截圖了。
    //    注意:UIApplicationUserDidTakeScreenshotNotification 將會(huì)在截圖完成之后顯示。現(xiàn)在在截圖截取之前無(wú)法得到通知始锚。
    //    希望蘋(píng)果會(huì)在iOS8當(dāng)中增加 UIApplicationUserWillTakeScreenshotNotification沿侈。(只有did, 沒(méi)有will顯然不是蘋(píng)果的風(fēng)格...)
    [[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 用來(lái)防止連續(xù)多次截屏
#pragma mark - UIApplicationUserDidTakeScreenshotNotification - 檢測(cè)到截屏
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    NSLog(@"檢測(cè)到截屏");
    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];
}

/**
 *  截取當(dāng)前屏幕
 *
 *  @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);
}




//搖一搖的時(shí)候 寫(xiě)在(viewDidLoad){[self becomeFirstResponder];}中  這句話很重要  這是把當(dāng)前VC設(shè)置為第一響應(yīng)者
#pragma mark - 搖一搖       
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //檢測(cè)到搖動(dòng)
}

-
(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
    //搖動(dòng)取消
}

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
    //搖動(dòng)結(jié)束
    if(event.subtype == UIEventSubtypeMotionShake) {
        //somethinghappens
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末通惫,一起剝皮案震驚了整個(gè)濱河市竟宋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌叨吮,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞬矩,死亡現(xiàn)場(chǎng)離奇詭異茶鉴,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)景用,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén)涵叮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人伞插,你說(shuō)我怎么就攤上這事割粮。” “怎么了媚污?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵舀瓢,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我杠步,道長(zhǎng),這世上最難降的妖魔是什么榜轿? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任幽歼,我火速辦了婚禮,結(jié)果婚禮上谬盐,老公的妹妹穿的比我還像新娘甸私。我一直安慰自己,他們只是感情好飞傀,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布皇型。 她就那樣靜靜地躺著,像睡著了一般砸烦。 火紅的嫁衣襯著肌膚如雪弃鸦。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,156評(píng)論 1 308
  • 那天幢痘,我揣著相機(jī)與錄音唬格,去河邊找鬼。 笑死颜说,一個(gè)胖子當(dāng)著我的面吹牛购岗,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播门粪,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼喊积,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了玄妈?” 一聲冷哼從身側(cè)響起乾吻,我...
    開(kāi)封第一講書(shū)人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤髓梅,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后溶弟,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體女淑,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年辜御,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鸭你。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡擒权,死狀恐怖袱巨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情碳抄,我是刑警寧澤愉老,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站剖效,受9級(jí)特大地震影響嫉入,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜璧尸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一咒林、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧爷光,春花似錦垫竞、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至徐裸,卻和暖如春遣鼓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背重贺。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工譬正, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人檬姥。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓曾我,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親健民。 傳聞我的和親對(duì)象是個(gè)殘疾皇子抒巢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容