iOS开发之一些实用小知识点总结
话不多说,直接进主题
一、防止UIButton,cell等重复点击
主要是快速点击button或者cell,所对应的action或者逻辑会走多次,例如:点击button或者cell调用拨打电话的方法,会弹出拨打电话框好多次;这个对用户不太友好;问了下哥们儿,他给了个宏,目前算是解决这个问题;代码如下:
//防止多次调用
#definekPreventRepeatClickTime(_seconds_)\
staticBOOLshouldPrevent;\
if(shouldPrevent)return;\
shouldPrevent=YES;\
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)((_seconds_)*NSEC_PER_SEC)),dispatch_get_main_queue(),^{\
shouldPrevent=NO;\
});\
总的思路是设置一个bool变量,记录一下,延时更改下变量的值;使用:在所需要的button或者cell的action前调用即可:
kPreventRepeatClickTime(0.5);
二、获取当前视图最顶层的ViewController
获取当前视图最顶层的ViewController
+(UIViewController*)currentViewController{
UIWindow*window=[[UIApplicationsharedApplication]keyWindow];
if(window.windowLevel!=UIWindowLevelNormal){
NSArray*windows=[[UIApplicationsharedApplication]windows];
for(UIWindow*tmpWininwindows){
if(tmpWin.windowLevel==UIWindowLevelNormal){
window=tmpWin;
break;
}
}
}
UIViewController*currentVC=window.rootViewController;
while(currentVC.presentedViewController){
currentVC=currentVC.presentedViewController;
}
if([currentVCisKindOfClass:[UITabBarControllerclass]]){
currentVC=[(UITabBarController*)currentVCselectedViewController];
}
if([currentVCisKindOfClass:[UINavigationControllerclass]]){
currentVC=[(UINavigationController*)currentVCtopViewController];
}
returncurrentVC;
}
三、代码截图相关
截取指定的View:
///截屏
-(void)actionForScreenShotWith:(UIView*)aimViewsavePhoto:(BOOL)savePhoto{
if(!aimView)return;
UIGraphicsBeginImageContextWithOptions(aimView.bounds.size,NO,0.0f);
[aimView.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(savePhoto){
///保存到本地相册
UIImageWriteToSavedPhotosAlbum(viewImage,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
}
}
保存图片的回调处理
-(void)image:(UIImage*)imagedidFinishSavingWithError:(NSError*)errorcontextInfo:(void*)contextInfo{
if(error){
NSLog(@"保存失败,请重试");
}else{
NSLog(@"保存成功");
}
}
总结
以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。