1.搖一搖功能的實(shí)現(xiàn):
在viewDidLoad方法中調(diào)用:
[[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];
在viewWillAppear方法中調(diào)用:
[self becomeFirstResponder];
為了防止意外發(fā)生撩炊,需要在viewWillDisappear方法中調(diào)用:
[self resignFirstResponder];
實(shí)現(xiàn)和搖一搖相關(guān)的三個(gè)方法:
#pragma mark - ShakeToEdit
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//手機(jī)震動(dòng)
dispatch_queue_t queue = dispatch_queue_create("shake", NULL);
dispatch_async(queue, ^{
[self shake];
});
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"cancel");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
UIImage *image = [self screenshot];
if(image){
[ShowView showWithImage:image AndSaveClick:^{
[self saveImageToPhotos:image];
}];
}
}
2.屏幕截圖功能的實(shí)現(xiàn):
#pragma mark - ScreenShot
-(UIImage *)screenshot{
UIView *view = self.view;
// float scale = [[UIScreenmainScreen] scale];//得到設(shè)備的分辨率
// scale = 1; 的時(shí)候是代表當(dāng)前設(shè)備是320*480的分辨率(就是iphone4之前的設(shè)備)
// scale = 2; 的時(shí)候是代表分辨率為640*960的分辨率
//繪圖
//第一個(gè)參數(shù)表示區(qū)域大小。第二個(gè)參數(shù)表示是否是非透明的真椿。如果需要顯示半透明效果谭跨,需要傳NO干厚,否則傳YES。第三個(gè)參數(shù)就是屏幕密度了螃宙,關(guān)鍵就是第三個(gè)參數(shù)蛮瞄。
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [UIScreen mainScreen].scale);
//渲染
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//生產(chǎn)圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}