说起摇一摇屏幕截图这个功能的实现,我们可以把它分为两部分来看:
一、摇一摇功能的实现;
二、屏幕截图。
首先来实现摇一摇功能:这个网上方法很多,就不多说了直接上代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
[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(@"摇动结束"); } }
|
其次实现屏幕截图功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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();
|
最后效果如下图:
demo地址:https://github.com/xiaobai0134/shakeScreen.git