iOS开发之一些实用小知识点总结

2025-05-29 0 78

话不多说,直接进主题

一、防止UIButton,cell等重复点击

主要是快速点击button或者cell,所对应的action或者逻辑会走多次,例如:点击button或者cell调用拨打电话的方法,会弹出拨打电话框好多次;这个对用户不太友好;问了下哥们儿,他给了个宏,目前算是解决这个问题;代码如下:

?

1

2

3

4

5

6

7

8
// 防止多次调用

#define kPreventRepeatClickTime(_seconds_) \\

static BOOL shouldPrevent; \\

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前调用即可:

?

1
kPreventRepeatClickTime(0.5);

二、获取当前视图最顶层的ViewController

获取当前视图最顶层的ViewController

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
+ (UIViewController *)currentViewController {

UIWindow * window = [[UIApplication sharedApplication] keyWindow];

if (window.windowLevel != UIWindowLevelNormal){

NSArray *windows = [[UIApplication sharedApplication] windows];

for(UIWindow * tmpWin in windows){

if (tmpWin.windowLevel == UIWindowLevelNormal){

window = tmpWin;

break;

}

}

}

UIViewController *currentVC = window.rootViewController;

while (currentVC.presentedViewController) {

currentVC = currentVC.presentedViewController;

}

if ([currentVC isKindOfClass:[UITabBarController class]]) {

currentVC = [(UITabBarController *)currentVC selectedViewController];

}

if ([currentVC isKindOfClass:[UINavigationController class]]) {

currentVC = [(UINavigationController *)currentVC topViewController];

}

return currentVC;

}

三、代码截图相关

截取指定的View:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
/// 截屏

- (void)actionForScreenShotWith:(UIView *)aimView savePhoto:(BOOL)savePhoto {

if (!aimView) return;

UIGraphicsBeginImageContextWithOptions(aimView.bounds.size, NO, 0.0f);

[aimView.layer renderInContext: UIGraphicsGetCurrentContext()];

UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

if (savePhoto) {

/// 保存到本地相册

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

}

}

保存图片的回调处理

?

1

2

3

4

5

6

7
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{

if (error) {

NSLog(@"保存失败,请重试");

} else {

NSLog(@"保存成功");

}

}

总结

以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。原文链接:http://my.csdn.net/syg90178aw

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS开发之一些实用小知识点总结 https://www.kuaiidc.com/90236.html

相关文章

猜你喜欢
发表评论
暂无评论