ios常用知识点总结

1、在ios中打开链接地址的方法:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"url"]]];

2、禁止手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"当前的手势:%@",gestureRecognizer);
    NSLog(@"当前的处理view:%@",touch.view);
    if ([touch.view isKindOfClass:[UIButton class]]){
        return NO;
    }
    return YES;
}

3、是否是子类

isMemberOfClass

4、代码模拟用户点击
模拟UI的事件sendActionsForControlEvents,比如模拟用户点击事件

[myBtn sendActionsForControlEvents:UIControlEventTouchUpInside];

再比如模拟segmentedControltitleChange事件

[self.segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

5、全局设置navigationbar背景

UINavigationBar *navigationBar = [UINavigationBar appearance];
[navigationBar setBackgroundImage:[UIImage HJ_imageWithColor:UIColorFromRGB(CCMainThemeColorNumber())]
                        forBarMetrics:UIBarMetricsDefault];

当执行这句话的时候,会造成controllerview的坐标不是以屏幕左上为原点,而是以navigationbar下面为原点。

如图:

ios常用知识点总结_第1张图片
Paste_Image.png

6、IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet

NSString *characterDecodedStr = @"[I LOVE YOU]";

NSString *abc = [characterDecodedStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",。?.,?\\r\\n\\t[]"]];

这个方法只能去除首字符和末字符的特殊字符,中间不能出去。
stringByTrimmingCharactersInSet removes characters from the beginning and end of your string, not from any place in it

7、判断字符串是否包含某字符

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        return [self containsString:string];
    } else {
        return ([self rangeOfString:string].location != NSNotFound);
    }

8、时间戳timeIntervalSince1970
一、转化时间戳方法:

NSString *timeSp = [NSString stringWithFormat:@"%d", (long)      [localeDate timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //时间戳的值

二、把获取的时间转化为当前时间

NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式 
NSTimeZone *zone = [NSTimeZone systemTimeZone]; 
NSInteger interval = [zone secondsFromGMTForDate:datenow]; 
NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval]; 
NSLog(@"%@", localeDate);  

三、时间戳转换为时间的方法

NSDate *confirmTimesp = [NSDate dateWithTimeIntervalSince1970:136789745666];
NSLog(@"136789745666 = %@", confirmTimesp);

9、解决UIScrollView 产生64像素offset偏移问题

遇到含导航栏的ViewController,其第一个子试图是UIScrollView会自动产生64像素偏移。 找了哈资料发现可以通过设置viewcontrollerself.automaticallyAdjustsScrollViewInsets = false
解决了。
另外一个办法就去Storyboard上将ViewControllerUnder Top Bar
勾去掉也可以解决
automaticallyAdjustsScrollViewInsets:是否根据按所在界面的navigationbartabbar的高度,自动调整scrollviewinset

10、NSDictionary 显示出来的allkeys排序不正确.
解决方式,先进行allkeys排序,然后根据allkeys排序取值

NSArray *keys = [[dic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2 options:NSNumericSearch];
}];
for (NSString *key in keys) {
        if ([[dic objectForKey:key] isKindOfClass:[NSArray class]]) {
                [self.settingData addObject:[dic objectForKey:key]];
            }
        }

NSStringCompareOptions介绍
NSCaseInsensitiveSearch
A case-insensitive search.不区分大小写比较
NSLiteralSearch
Exact character-by-character equivalence.区分大小写比较
NSBackwardsSearch
Search from end of source string.字符串末尾开始搜索
NSAnchoredSearch
Search is limited to start (or end, if NSBackwardsSearch
) of source string.搜索限制范围的字符串
NSNumericSearch
Numbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt< Name25.txt
按照字符串里的数字为依据,算出顺序
NSDiacriticInsensitiveSearch
Search ignores diacritic marks.忽略 "-" 符号的比较
NSWidthInsensitiveSearch
Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.忽略字符串的长度,比较出结果
NSForcedOrderingSearch
Comparisons are forced to return either NSOrderedAscending or NSOrderedDescending
if the strings are equivalent but not strictly equal.忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
NSRegularExpressionSearch
The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except NSCaseInsensitiveSearch
and NSAnchoredSearch
. You can use this option only with the rangeOfString:...
methods and stringByReplacingOccurrencesOfString:withString:options:range:
.只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch

11、UIScrolleViewbounces属性
bounces //默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止

12、如何巧妙隐藏一行 UITableViewCell
有些时候, 我们想动态的隐藏某一行的UITableView里面某一行的Cell,一般我们会用到下面代码去实现第三行Cell隐藏.

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row == 3 ? 0 : 40;}

但是很不幸的是, 我们有时候虽然把高度设置 0, 但是有时候Cell里面的Label的文字还是显示, 还和其他Cell重叠.

ios常用知识点总结_第2张图片
Paste_Image.png

解决方法
有很多方法去解决这个问题, 只需要设置 UITableViewDelegate里面添加下面代码, 或者在你的继承 UITableViewCell的子类里面把这个属性设置 YES.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.clipsToBounds = YES;}

clipsToBounds属性
取值:BOOL(YES/NO)
作用:决定了子视图的显示范围。具体的说,就是当取值为YES时,剪裁超出父视图范围的子视图部分;当取值为NO时,不剪裁子视图。默认值为NO

13、NSArraycontainsObject方法,比较是什么

- (BOOL)containsObject:(id)anObject

苹果给的文档里说的是,当判断anObject是否在当前的NSArray中的时候,是通过调用isEqual:这个方法来判断的,即对于NSArray中的每个对象都会调用一次isEqual:anObject,如果返回YES自然就是说这个数组包含anObject。那这个时候问题可能就来了,比如说这个问题。究其原因的话还在于isEqual:这个方法。

ios常用知识点总结_第3张图片
Paste_Image.png

所以isEqual:本质是在比较两个对象的hash。苹果也说了:

This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.

所以在使用constainobject的时候,可以子model中充血isequal

推荐一篇文章http://nshipster.com/equality/

14.ios沙盒目录
获取目录路径的方法:

// 获取沙盒主目录路径
NSString *homeDir = NSHomeDirectory();
// 获取Documents目录路径
NSString *docDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// 获取Library的目录路径
NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
// 获取Caches目录路径
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 获取tmp目录路径
NSString *tmpDir = NSTemporaryDirectory();

获取应用程序程序包中资源文件路径的方法:

NSLog(@"%@",[[NSBundle mainBundle] bundlePath]);
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

摘自
http://www.jianshu.com/p/dd3f120eb249

15.获取自定义bundle

NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"My.bundle"];
// 或者这种
//NSString * path = [[NSBundle mainBundle] pathForResource:@"YPhotoBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *img_path = [bundle pathForResource:imgName ofType:@"png"];

16.方法注释快捷键
现在苹果提供了方法描述的快捷键option + command + /

17.iOS取绝对值- Abs Fabs Fabsf用法
int abs(int i); // 处理int类型的取绝对值
double fabs(double i); //处理double类型的取绝对值
float fabsf(float i); /处理float类型的取绝对值

18.iOS中的round、ceil、floor函数略解

round 如果参数是小数,则求本身的四舍五入.
ceil 如果参数是小数,则求最小的整数但不小于本身.
floor 如果参数是小数,则求最大的整数但不大于本身.

Example:如何值是3.4的话,则
-- round 3.000000
-- ceil 4.000000
-- floor 3.00000

持续更新。。。。

你可能感兴趣的:(ios常用知识点总结)