1,从父视图删除所有子视图
- [xxxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
2,图片旋转控制:
- #pragma mark ----- 更新按钮动画
- - (void)rotate360DegreeWithImageViews:(UIImageView *)myViews{
- CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
- rotationAnimation.duration = 1.0;
- rotationAnimation.cumulative = YES;rotate360DegreeWithImageViews
- rotationAnimation.repeatCount = 100000;
- [myViews.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
- }
- [myViews.layer removeAllAnimations]; // 停止
-
[[UIapplication sharedApplication].keyWindow addSubview:waitingview];
4,给UINavigationBar添加背景色或者添加自定义的图片
设置背景色
1 UIColor *itemcolor = [UIColor colorWithRed:100.0f/255.0f green:176.0f/255.0f blue:0.0f/255.0f alpha:0.3f];
2 self.navigationController.navigationBar.tintColor = itemcolor;
设置自定义图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@" “]];
5,让屏幕一直保持唤醒状态不黑屏
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
6,图片模糊化处理
+(UIImage *)scale:(UIImage *)image toSize:(CGSize)size 3 { 4 UIGraphicsBeginImageContext(size); 5 [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 6 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 7 UIGraphicsEndImageContext(); 8 return scaledImage; 9 }
7,字符串大小写转换
1 NSString *str = @"hellow word";
2 // 全部转换为大写 3 NSString *str1 = [str uppercaseStringWithLocale:[NSLocale currentLocale]]; 6 // 单词首字母大写 7 NSString *str2 = [str1 capitalizedStringWithLocale:[NSLocale currentLocale]]; 10 // 转换为原型 11 NSString *str3= [str3 lowercaseStringWithLocale:[NSLocale currentLocale]];
8,隐藏状态栏
1,设置
2,代码设置
[[UIApplication sharedApplication]setStatusBarHidden:YES];//隐藏状态栏
9,根据字体和文字数量动态计算UILabel长度和大小
1 NSString *theText = @"hellow";
2 UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:14.0f]; 3 CGSize theStringSize = [theText sizeWithFont:font constrainedToSize:theLabel.frame.size 4 lineBreakMode:theLabel.lineBreakMode]; 5 theLable.frame= CGRectMake(theLable.frame.origin.x, 6 theLable.frame.origin.y,theStringSize.width,theStringSize.height); 7 theLable.text= theText;
10,颜色转变成图片
- (UIImage *)createImageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
11,app评分跳转
-(void)goToAppStore
{
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",项目id];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
13.获取当前系统语言环境
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defs objectForKey:@"AppleLanguages"];
NSString *preferredLang = [languages objectAtIndex:0];
14.计算字符串的高度NSString *str = @"chuanzhang";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *dicAtt = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,paragraphStyle.copy,NSParagraphStyleAttributeName, nil];
NSAttributedString *attribute = [[NSAttributedString alloc]initWithString:str attributes:dicAtt];
CGRect frame = [attribute boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
15.强行关闭app的方法
// 私有API
[[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
// C语言方法
exit(0);
16.如何快速的查看一段代码的执行时间
#define TICK NSDate *startTime = [NSDate date]
#define TOCK NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])
// 在想要查看执行时间的代码的地方进行这么处理
TICK
//do your work here
TOCK
17.tableViewCell上的button,点击获取所在row
UITableViewCell *cell = (UITableViewCell *)[[btn superview] superview];
NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
18.设置粘贴内容
[UIPasteboard generalPasteboard].string = @"string";
// 获取粘贴内容
NSString *string = [UIPasteboard generalPasteboard].string;
19.iPhone为了节省电力所以有一个自动休眠机制,如果想让我们的APP不自动进入休眠只需要设置 UIApplication的idleTimerDisabled 属性为 YES 即 可。(切勿滥用)
示例:
[UIApplication sharedApplication].idleTimerDisabled = YES;
20.UIApplicationUserDidTakeScreenshotNotification
通知,当用户截屏时触发
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenCapture) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
- (void)screenCapture{
// doSomething
}