说起摇一摇屏幕截图这个功能的实现,我们可以把它分为两部分来看:
一、摇一摇功能的实现;
二、屏幕截图。
首先来实现摇一摇功能:这个网上方法很多,就不多说了直接上代码。
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
//在这里设置当前vc 为第一响应者。
[self becomeFirstResponder];
// 开始摇一摇
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"开始摇一摇");
}
//取消摇一摇
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"取消摇一摇");
}
//结束摇一摇
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.subtype == UIEventSubtypeMotionShake)
{
NSLog(@"摇动结束");
//这里实现屏幕截图
}
}
其次实现屏幕截图功能:
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(self.view.window.bounds.size);
}
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
最后效果如下图: