1.字符串GB2312编码
-
字符串GB2312编码
NSString *arrr = @"测试"; NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSData *adata = [arrr dataUsingEncoding:gbkEncoding];
2.UIView独占响应事件
-
一个布尔值,用来标示一个view独占触摸事件. 当把一个view中的exclusiveTouch设置成YES时,会致使这个window屏蔽掉其他的view触发响应事件.默认值是NO.
_button1.exclusiveTouch = YES;
3.设置导航栏
- 设置隐藏导航栏
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:YES]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } - 设置导航栏为白色:
- 在plist中设置
View controller-based status bar appearance1、plist View controller-based status bar appearance 设置为 NO - 在appdelegate 代码设置
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
- 在plist中设置
4.OC中block作为函数参数传递
-
使用枚举
typedef void (^OnFoolCallback)(NSString *name); -
定义方法
- (void)productFool:(OnFoolCallback)callback;typedef void (^OnFoolCallback)(NSString *name); - (void)productFool:(OnFoolCallback)callback; [fb productFool:^(NSString *name) { NSLog(@"name = %@", name); }];
4.collectionView的section刷新的时候有动画会闪一下解决
- 解决办法:停止动画:
[UIView setAnimationsEnabled:NO]; [self.homeCollectionView performBatchUpdates:^{ [self.homeCollectionView reloadSections:[NSIndexSet indexSetWithIndex:index]]; } completion:^(BOOL finished) { [UIView setAnimationsEnabled:YES]; }];
5.控制数据越界崩溃
-
数组越界控制
+(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SEL safeSel=@selector(safeObjectAtIndex:); SEL unsafeSel=@selector(objectAtIndex:); Class myClass = NSClassFromString(@"__NSArrayI"); Method safeMethod=class_getInstanceMethod (myClass, safeSel); Method unsafeMethod=class_getInstanceMethod (myClass, unsafeSel); Method_exchangeImplementations(unsafeMethod, safeMethod); }); } -(id)safeObjectAtIndex:(NSUInteger)index{ if (index>(self.count-1)) { NSAssert(NO, @"beyond the boundary"); return nil; } else{ return [self safeObjectAtIndex:index]; } }
6.Pt与Px 相互转换关系
- Pt与Px 相互转换关系。
Pt=(Px/96.0)*72.0 Pixel Point
7.设置声音静音:soundname 推送通知禁止声音
-
设置声音静音:soundname ,震动也是一个souond:
kSystemSoundID_Vibrate -
后台的代码设置推送内容
PushNotificationPayload payLoad = PushNotificationPayload.fromJSON(message); payLoad.addAlert("iphone推送测试 www.baidu.com"); // 消息内容 payLoad.addBadge(count); // iphone应用图标上小红圈上的数值 payLoad.addSound("default"); // 铃音 默认 -
2:程序内代码
#import <AudioToolbox/AudioToolbox.h> AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AudioServicesPlaySystemSound(1007);其中1007是系统声音的编号,其他的可用编号: //音效文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"]; //组装并播放音效 SystemSoundID soundID; NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID); AudioServicesPlaySystemSound(soundID); //声音停止 AudioServicesDisposeSystemSoundID(soundID);
8.编辑键盘问题->键盘下缩
- 有键盘,点击保存,或者编辑等时候点击键盘下缩,出现如下bug
“[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction” - 解决办法:
[self layoutSubviews];
9.UI View相对位置
-
获取点击事件的点相对于屏幕的位置
- 如果点击事件是如下这个方法(将UIEvent当做参数传回来)
-(void)expandButtonClicked:(id)sender withEvent:(UIEvent*)event 那么点击的位置相对于屏幕的坐标就是: UITouch* touch = [[event touchesForView:btn] anyObject]; CGPoint rootViewLocation = [touch locationInView:[FtAppDelegate shareAppDelegate].rootViewCtrl.view]; - 如果想知道一个view相对于屏幕或者另外一个view 的坐标,那么可以通过如下的方法得到:
UIWindow * window=[[[UIApplication sharedApplication] delegate] window]; CGRect rect=[bView convertRect: bView.bounds toView:window];
- 如果点击事件是如下这个方法(将UIEvent当做参数传回来)
10.UI View 移除子视图
- 移除所有子视图,无需循环只需要一句代码
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
11.虚拟机安装之后隔一段时间不能实用了修复 VirtualBox
- 打开虚拟机,报如下错误
shjyytxnj Implementation of the USB 2.0 controller not found! Because the USB 2.0 controller state is part of the saved VM state, the VM cannot be started. To fix this problem, either install the 'Oracle VM VirtualBox Extension Pack' or disable USB 2.0 support in the VM settings. Note! This error could also mean that an incompatible version of the 'Oracle VM VirtualBox Extension Pack' is installed (VERR_NOT_FOUND). - 安装了不兼容版本的“Oracle VM VirtualBox Extension Pack”
重新下载 最新版的 (1)VirtualBox 5.1.12 platform packages.\ (2)VirtualBox 5.1.12 Oracle VM VirtualBox Extension Pack https://www.virtualbox.org/wiki/Downloads 找到相匹配的版本和插件 重新札幌
12.设置textField 的placeholder的颜色
- 设置textField 的placeholder的颜色
[self.searchtextField setValue:RGB(156, 86, 49) forKeyPath:@"_placeholderLabel.textColor"]; [self.searchtextField setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
13.navbarhiddern ,消失之后的显示隐藏的缺一部分bug,
- Hidden动画问题
@property (nonatomic, assign) BOOL closeNavBarAnimating; - (void)viewDidLoad{ [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chageBool:) name:@"closeBarAnimationing" object:nil]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if (self.closeNavBarAnimating) { animated = NO; } [self.navigationController setNavigationBarHidden:YES animated:animated]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:NO animated:animated]; self.closeNavBarAnimating = NO; } -(void)chageBool:(id)sedner{ self.closeNavBarAnimating = YES; }
14.APP启动GIF动画
- UIImageView和UIImage都不支持GIF动画,使用UIWebView 加载GIF启动动画
@interface ViewController () @property (weak, nonatomic) IBOutlet UIWebView *webViewBG; @property (weak, nonatomic) IBOutlet UIButton *regBtn; @property (weak, nonatomic) IBOutlet UIButton *loginBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化UIWebView对象,添加GIF文件 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bg" ofType:@"gif"]; NSData *gif =[NSData dataWithContentsOfFile:filePath]; [self.webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil]; self.webViewBG.userInteractionEnabled = NO; //创建一个灰色的蒙版,提升效果(可选) UIView *filter = [[UIView alloc] initWithFrame:self.view.bounds]; filter.backgroundColor = [UIColor blackColor]; filter.alpha = 0.5; [self.view addSubview:filter]; //修改登录按钮和注册按钮的显示层级 [self.view bringSubviewToFront:self.loginBtn]; [self.view bringSubviewToFront:self.regBtn]; }
15.QQ聊天气泡样式界面
- 定义枚举区别是自己还是别人
typedef enum{ WPMessageTypeMe=0, WPMessageTypeOther=1 }WPMessageType; - 计算一段文字所占据的宽和高
CGSize textMaxSize=CGSizeMake(200, MAXFLOAT); NSDictionary *attr1=@{NSFontAttributeName:[UIFont systemFontOfSize:14]}; CGSize textSize=[message.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr1 context:nil].size; - 把一张小的图片做背景保持四周不变,拉伸中间,则用如下属性。UIEdgeInsets是拉伸的区域,一般取最中间的一个点
UIImage *meBgNor=[UIImage imageNamed:@"chat_send_nor"]; UIEdgeInsets edge1=UIEdgeInsetsMake(28, 32, 28, 32); meBgNor=[meBgNor resizableImageWithCapInsets:edge1 resizingMode:UIImageResizingModeStretch]; [self.textView setBackgroundImage:meBgNor forState:UIControlStateNormal]; - 监听键盘的弹出隐藏
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil]; -(void)changeFrame:(NSNotification *)note{ self.view.superview.backgroundColor=[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.9]; CGFloat duration=[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGFloat keyboardY=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; CGFloat screenH=[UIScreen mainScreen].bounds.size.height; [UIView animateWithDuration:duration animations:^{ self.view.transform=CGAffineTransformMakeTranslation(0, keyboardY-screenH); }]; } - 输入框不是键盘的inputView没有键盘的时候也是悬浮在视图上,弹出键盘的时候控制view的frame就行,底部对齐
- lable自适应
label.adjustsFontSizeToFitWidth = YES;
16.使用CAShapeLayer实现一个音量大小动态改变的控件 类似微信的语音
- 思路,外界轮廓是有圆角的图像类似椭圆,layer是个矩形,maskToBounds 为yes,
- _dynamicView 表示外部轮廓的View。 _indicateLayer 表示内容动态显示的Layer
-(void)refreshUIWithVoicePower : (NSInteger)voicePower{ CGFloat height = (voicePower)*(CGRectGetHeight(_dynamicView.frame)/TOTAL_NUM); [_indicateLayer removeFromSuperlayer]; _indicateLayer = nil; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, CGRectGetHeight(_dynamicView.frame)-height, CGRectGetWidth(_dynamicView.frame), height) cornerRadius:0]; _indicateLayer = [CAShapeLayer layer]; _indicateLayer.path = path.CGPath; _indicateLayer.fillColor = [UIColor whiteColor].CGColor; [_dynamicView.layer addSublayer:_indicateLayer]; }
17.比较UIimageView的image是哪张图片
- 实现手势引导页根据图片名字的不通切换下一张,最后一张的时候移除UIimageView
- 根据imageData判断是哪一个图片,也可以为imageView增加tag
NSData *data = UIImagePNGRepresentation(self.guidImageView.image); NSData *data1 = UIImagePNGRepresentation([UIImage imageNamed:@"home_bj_yd1"]); NSData *data2 = UIImagePNGRepresentation([UIImage imageNamed:@"home_bj_yd2"]); NSData *data3 = UIImagePNGRepresentation([UIImage imageNamed:@"home_bj_yd3"]); if ( [data isEqual:data1]) { self.guidImageView.image=[UIImage imageNamed:@"home_bj_yd2"]; }
18.APP开发注意的手机尺寸
- 启动页 尺寸
- 引导页尺寸
- AppStore商店的介绍图尺寸
NSLog(@"name: %@", [[UIDevice currentDevice] name]); NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]); NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]); NSLog(@"model: %@", [[UIDevice currentDevice] model]); NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);
19.点击顶部通知消息报错:
- 在点击事件中,如果我们不写completionHandler()这个方法,可能会报一下的错误,希望大家注意下~
Warning: UNUserNotificationCenter delegate received call to - userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called. 2017-01-16 15:00:36.045772 JYYinYongBao[1006:326459] Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
20.Xcode 文件夹 文件夹拖拽注意点
- 黄色的文件夹,打包的时候,不会建立目录,主要保存程序文件
- 素材不允许重名
- 蓝色的文件夹,打包的时候,会建立目录,可以分目录的存储素材文件
- 素材可以重名
- 游戏的场景,background.png 【草地/雪地/高山/坟墓】
- 手机应用的皮肤,/白天/夜间模式
- 切记:不能把程序文件放在蓝色文件夹内,程序会暴漏`
- Bundle:
- 通常在第三方框架素材
- 可以按照黄色文件夹的方式拖拽,同时保留目录结构
- 可以避免文件重名