1、代码方式调整屏幕亮度
// brightness属性值在0-1之间,0代表最小亮度,1代表最大亮度
[[UIScreen mainScreen] setBrightness:0.5];
2、解决openUrl延时问题
// 方法一
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:@{}
completionHandler:nil];
} else {
[application openURL:URL];
}
});
// 方法二
[self performSelector:@selector(redirectToURL:) withObject:url afterDelay:0.1];
- (void) redirectToURL
{
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:@{}
completionHandler:nil];
} else {
[application openURL:URL];
}
}
3、状态栏菊花
在状态栏增加网络请求的菊花,类似safari加载网页的时候状态栏菊花
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
4、让手机震动一下
#import
// #import
- (void)vibrate {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
5、生成随机数
有时候我们需要在程序中生成随机数,但是在Objective-c中并没有提供相应的函数,好在C中提供了rand()、srand()、random()、arc4random()几个函数。那么怎么使用呢?下面将简单介绍:
1.获取一个随机整数范围在:[0,100)包括0,不包括100
int x = arc4random() % 100;
2.获取一个随机数范围在:[500,1000),包括500,包括1000
int y = (arc4random() % 501) + 500;
3.获取一个随机整数,范围在[from,to),包括from,包括to
-(int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to – from + 1)));
}
6、点击Button 出现 拷贝
[self.btnContentbecomeFirstResponder];
UIMenuController*menu = [UIMenuControllersharedMenuController];
[menusetTargetRect:self.btnContent.frameinView:self.btnContent.superview];
[menu setMenuVisible:YESanimated:YES];
7、获取手机信息
NSDictionary*infoDictionary = [[NSBundlemainBundle]infoDictionary];
//当前应用名称
NSString*appCurName = [infoDictionaryobjectForKey:@"CFBundleDisplayName"];
//当前应用软件版本 比如:1.0.1
NSString*appCurVersion = [infoDictionaryobjectForKey:@"CFBundleShortVersionString"];
//当前应用版本号码 int类型 构建版本如:20151128
NSString*appCurVersionNum = [infoDictionaryobjectForKey:@"CFBundleVersion"];
//手机别名:用户定义的名称
NSString* userPhoneName = [[UIDevicecurrentDevice]name];
//设备名称
NSString* deviceName = [[UIDevicecurrentDevice]systemName];
//手机系统版本9.1
NSString* phoneVersion = [[UIDevicecurrentDevice]systemVersion];
//手机型号iPhone
NSString* phoneModel = [[UIDevicecurrentDevice]model];
//地方型号 (国际化区域名称)
NSString* localPhoneModel = [[UIDevicecurrentDevice]localizedModel];
8、禁止锁屏
默认情况下,当设备一段时间没有触控动作时,iOS会锁住屏幕。但有一些应用是不需要锁屏的,比如视频播放器。
[UIApplication sharedApplication].idleTimerDisabled = YES;
或
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
9、比较两个UIImage是否相等
- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqual:data2];
}
10、解决当UIScrollView上有UIButton的时候,触摸到button滑动不了的问题
// 子类化UIScrollView,并重写以下方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.delaysContentTouches = NO;
}
return self;
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
if ([view isKindOfClass:UIButton.class]) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}