- //创建动画
- letanim=CABasicAnimation(keyPath:"transform.rotation")//设置相关属性anim.toValue= 2 *M_PI
- anim.repeatCount=MAXFLOAT
- anim.duration = 15
- //完成之后不移除,testView被释放,动画随着一起删除anim.isRemovedOnCompletion= falsetestView(anim, forKey: nil)
中重写
- UINavigationController
方法, 不必每次跳转都调用
- pushViewController
- hidesBottomBarWhenPushed
- overridefunc pushViewController(_ viewController: UIViewController, animated: Bool) {//隐藏tabbar
- ifchildViewControllers.count >0{
- viewController.hidesBottomBarWhenPushed =true}super.pushViewController(viewController, animated: animated)
- }
为控件添加约束
- NSLayoutConstraint
- //设置通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算
- centerButton.translatesAutoresizingMaskIntoConstraints= false
- //依次添加X,Y, W,H
- view.addConstraint(NSLayoutConstraint(item: centerButton, attribute:.centerX, relatedBy:.equal, toItem: self.view, attribute:.centerX, multiplier:1.0, constant:0))
- view.addConstraint(NSLayoutConstraint(item: centerButton, attribute:.centerY, relatedBy:.equal, toItem: self.view, attribute:.centerY, multiplier:1.0, constant: -60))
- view.addConstraint(NSLayoutConstraint(item: centerButton, attribute:.width, relatedBy:.equal, toItem: nil, attribute:.notAnAttribute, multiplier:1.0, constant:50))
- view.addConstraint(NSLayoutConstraint(item: centerButton, attribute:.height, relatedBy:.equal, toItem: nil, attribute:.notAnAttribute, multiplier:1.0, constant:50))
添加约束
- 参数1
的
- 参数1(一般为视图)
属性
- 参数2(坐标或宽高)
- 参数3(大于等于小鱼)
的
- 参数4(参照视图)
属性 乘以
- 参数5(坐标或宽高)
加上 参数 7
- 参数6
, 参数 5 传入
- nil
- .notAnAttribute
- view.addConstraint(NSLayoutConstraint(item: 参数1,attribute: 参数2, relatedBy: 参数3, toItem: 参数4,attribute: 参数5, multiplier: 参数6,constant: 参数7))
- extension UIColor{ classfunc colorWithHex(hexValue:UInt32)->UIColor{
- let r =(hexValue& 0xff0000)>> 16
- let g =(hexValue& 0x00ff00)>> 8
- let b = hexValue & 0x0000ff
- returnUIColor(red:CGFloat(r)/ 255.0,green:CGFloat(g)/ 255.0,blue:CGFloat(b)/ 255.0,alpha: 1.0)}
- }
- //示例调用:
- view.backGroundColor =UIColor.colorWithHex(hexValue: 0xff0000)
的写法
- &error
- varerror: NSError?
- context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,error: &error)
的滑动范围
- TableView
- - (void)scrollViewDidScroll:(UIScrollView*)scrollView
- {//一般获取最后控件的最大Y坐标,labelExplain是最后一个cell下追加的控件
- self.tableView.contentSize= CGSizeMake(0,CGRectGetMaxY(self.labelExplain.frame) +10);
- }
- clipsToBounds(UIView)是指视图上的子视图,如果超出父视图的部分就截取掉masksToBounds(CALayer)却是指视图的图层上的子图层,如果超出父图层的部分就截取掉
- 模拟器的位置:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs
- 文档安装位置:/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
- 插件保存路径:
- ~/Library/ApplicationSupport/Developer/Shared/Xcode/Plug-ins
- 自定义代码段的保存路径:
- ~/Library/Developer/Xcode/UserData/CodeSnippets///如果找不到CodeSnippets文件夹,可以自己新建一个CodeSnippets文件夹。描述文件路径
- ~/Library/MobileDevice/Provisioning Profiles
- //富文本转html字符串
- - (NSString * ) attriToStrWithAttributeString: (NSAttributedString * ) attributeString {
- NSDictionary * tempDic = @ {
- NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
- NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt: NSUTF8StringEncoding]
- };
- NSData * htmlData = [attributeString dataFromRange: NSMakeRange(0, attributeString.length) documentAttributes: tempDic error: nil];
- return [[NSString alloc] initWithData: htmlData encoding: NSUTF8StringEncoding];
- }
- //html字符串转富文本
- - (NSAttributedString * ) strToAttriWithString: (NSString * ) htmlString {
- return [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding: NSUnicodeStringEncoding] options: @ {
- NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
- }
- documentAttributes: nil error: nil];
- }
的文件, 比如:
- .h
- Demo-Bridge.h
中找到
- Build Setteings
添加路径
- Objective_C Bridging Header
- $(SRCROOT)/Demo/Demo-Bridge.h
头文件, 例如:
- Objective_C
- #import "UIView+webCache.h"
- - (void)layoutSubviews
- {
- [superlayoutSubviews];// 设置UITableViewCellEditControl样式
- for(UIControl *control inself.subviews) {if([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {for(UIView*view in control.subviews) {if([view isKindOfClass: [UIImageViewclass]]) {UIImageView*img = (UIImageView*)view;if(self.selected) {//选择状态图片img.image= [UIImageimageNamed:@"image1"];
- }else{//未选中状态图片img.image= [UIImageimageNamed:@"image2"];
- }
- }
- }
- }
- }
- }
唤醒全局搜索并进入输入状态
- command + shift + F
为
- Find
(这里也可以采用正则进行查找搜索
- Replace
)
- Regular Expression
即可
- Replace All
判断应用程序是否是安装完首次次启动
- NSUserDefaults
- if(![[NSUserDefaultsstandardUserDefaults] valueForKey:@"FirstStart"]) {
- [[NSUserDefaultsstandardUserDefaults] setValue:@"firstStart"forKey:@"FirstStart"];//第一次启动,可以设置欢迎页或者设置默认语言}else{//非第一次启动}
- // 可以把下列代码放在AppDelegate的@UIApplicationMain的上方func DebugLog<T>(messsage : T, file :String = #file, funcName :String = #function, lineNum : Int= #line) {#ifDEBUGletfileName=(file as NSString).lastPathComponent
- print("\(fileName):(\(lineNum))-\(messsage)")#endif}//使用方法DebugLog(messsage:"test")//输出类名 + 代码行数 + 输出信息ViewController.swift:(37)-test
工程文件 –> 显示包内容 –> 用文本打开
- Show in Finder
–> 搜索
- project.pbxproj
–> 将值改为
- developmentRegion
- zh-Hans
为分类添加属性
- runtime
的
- UITableView
和
- section
属性,就是定义在
- row
的分类里的
- NSIndexPath
- //写一个UIView的分类命名:UIView+Category
- UIView+Category.h
- //增加的属性
- @property(nonatomic,strong)NSObject*propertyTest;UIView+Category.m
- //加入运行时头文件
- #import<objc/runtime.h>
- @implementation UIView(Category)
- //获取关联的对象- (NSObject*)propertyTest {returnobjc_getAssociatedObject(self,@selector(propertyTest));
- }//给对象添加关联对象- (void)setPropertyTest:(NSObject*)value {
- objc_setAssociatedObject(self,@selector(propertyTest), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- 此后,就可以正常访问了该属性了
中的懒加载, 通常是写
- OC
方法, 例如:
- get
- - (DataModel *)model
- {if(!_model) {
- _model = [[DataModel alloc] init];
- _model.title = @"标题";
- }return_model;
- }
中有专门的懒加载修饰符
- swift
, 实现如下:
- lazy
- privatelazyvarmodel: DataModel = {letmodel = DataModel()
- model.title ="标题"
- returnmodel
- }()
的重写
- shouldAutorotate
- override open
- var shouldAutorotate: Bool {
- return false / true
- }
- //或者
- open override
- var shouldAutorotate: Bool {
- get {
- return false / true
- }
- }
中, 重写
- View
; 如果是在
- layoutSubviews
中重写
- ViewController
,
- viewWillLayoutSubviews
代码如下:
- Swift
- //视图控制器中
- overridefunc viewWillLayoutSubviews() {super.viewWillLayoutSubviews()
- UIApplication.shared.isStatusBarHidden =false}//视图中
- overridefunc layoutSubviews() {super.layoutSubviews()
- UIApplication.shared.isStatusBarHidden =false}
,
- ==
,
- isEqual
- isEqualToString
: 比较的是内存地址
- ==
: 是
- isEqual
的方法, 首先都会判断指针是否相等 , 相等直接返回
- NSObject
, 不相等再判断是否是同类对象或非空, 空或非同类对象直接返回
- YES
, 而后依次判断对象对应的属性是否相等, 若均相等, 返回
- NO
- YES
: 是
- isEqualToString
的方法, 从继承关系角度来说是
- NSString
的衍生方法, 在都是字符串的前提下, 判断字符串的内容是否相等, 如果知道了两个对象都是字符串,
- isEqual
比
- isEqualToString
要快
- isEqual
项目显示语言
- GitHub
- 打开Terminal 进入到仓库文件夹$:cd /Users/MacName/Desktop/Demo
- 创建一个`.gitattributes`的文件$:touch.gitattributes打开文件$:open.gitattributes写入如下代码,比如设置语言为Swift
- *.hlinguist-language=swift
- *.mlinguist-language=swift
- 重新push项目到GitHub, 完成修改
- 显示隐藏文件:$:defaults writecom.apple.finderAppleShowAllFiles -bool true
- 关闭显示隐藏文件:
- defaults writecom.apple.finderAppleShowAllFiles -bool false
- ***执行命令后需要打开强制退出界面(快捷键option+command+esc),重启Finder
布局后获取
- Masonry
值
- Frame
- [self layoutIfNeeded];
同时打开两个
- Xcode
模拟器 (做通信
- Simulator
方便调试)
- APP
- $:cd /Applications/Xcode.app/Contents/Developer/Applications/
- $:open -n Simulator.app/
, 键入以下代码
- xim.sh
- #!/bin/sh
- cd/Applications/Xcode.app/Contents/Developer/Applications/
- open -n Simulator.app/
- sudosh sim.sh
后, 换一个与当前模拟器设备不同的机型
- OK
中选择刚选取的机型
- Xcode
包即可同时打开调试
- run
道理也是一样的, 进到
- APP
应用目录,
- APP
即可
- open -n appName.app/
检测滑动到底部和顶部 (可用于聊天界面取历史消息)
- TableView
- - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
- {if(scrollView.contentOffset.y== scrollView.contentSize.height- scrollView.frame.size.height) {NSLog(@"滑到底部加载更多");
- }if(scrollView.contentOffset.y==0) {NSLog(@"滑到顶部更新");
- }
- }// 另外点击状态栏会调用- (void)scrollViewDidScrollToTop:(UIScrollView*)scrollView
- {NSLog(@"点击状态栏调用");
- }
,
- __weak
,
- __unsafe_unretained
- __block
- __weak在释放指针后能够同时将指针置为nil__unsafe_unretained 只留下一个无效的也指针
- __block 打破循环 copy副本 内部修改
来源: http://blog.csdn.net/feng2qing/article/details/54427436