添加AudioToolbox.framework //然后在头文件中声明 #import//声音提示
#import <AudioToolbox/AudioToolbox.h>
// 声音 振动
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
}
}
手机振动,摇一摇,截屏
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 场景由于各种产品需求的变动,我们在项目里一般情况下会用系统的UIAlertController,当然有很多特别的需...
- 写在前面 公司近期让做一个录制屏幕类的App,我研究了iOS9新增的Replaykit框架,使用起来确实挺简单的,...