iOS常用技巧

来源:互联网 发布:热血江湖sf源码 编辑:程序博客网 时间:2024/06/04 20:05

ios中常用的小技巧(总有你不知道的和你会用到的)

一、父视图设置的透明度影响到子视图的透明度问题

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UIView *darkView = [[UIView alloc] init];  
  2. //[darkView setAlpha:0];//会影响到子时图  
  3. [darkView setUserInteractionEnabled:NO];  
  4. [darkView setFrame:(CGRect){00, SCREEN_SIZE}];  
  5. //[darkView setBackgroundColor:LCColor(46, 49, 50)];  
  6. darkView.backgroundColor = [[UIColor colorWithRed:46/255.0f green:49/255.0f blue:50/255.0f alpha:1.0f] colorWithAlphaComponent:LC_DEFAULT_BACKGROUND_OPACITY];//这样父视图透明不影响子时图  


二、控件的局部圆角问题

你是不是也遇到过这样的问题,一个button或者label,只要右边的两个角圆角,或者只要一个圆角。该怎么办呢。这就需要图层蒙版来帮助我们了。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CGRectrect=CGRectMake(0,0,100,50);  
  2. CGSizeradio=CGSizeMake(5,5);//圆角尺寸  
  3. UIRectCornercorner=UIRectCornerTopLeft|UIRectCornerTopRight;//这只圆角位置  
  4. UIBezierPath*path=[UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];  
  5. CAShapeLayer*masklayer=[[CAShapeLayeralloc]init];//创建shapelayer  
  6. masklayer.frame=button.bounds;  
  7. masklayer.path=path.CGPath;//设置路径  
  8. button.layer.mask=masklayer;  

举例为button,其它继承自UIView的控件都可以


三、navigationBar的透明问题

如果仅仅把navigationBar的alpha设为0的话,那就相当于把navigationBar给隐藏了,大家都知道,父视图的alpha设置为0的话,
那么子视图全都会透明的。那么相应的navigationBar的标题和左右两个按钮都会消失。这样显然达不到我们要求的效果。
(1)如果仅仅是想要navigationBar透明,按钮和标题都在可以使用以下方法:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="objc" style="color: rgb(46, 46, 46); font-size: 15px; line-height: 24px;">//给navigationBar设置一个空的背景图片即可实现透明,而且标题按钮都在  
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault];
细心的你会发现上面有一条线?

首先我们看一下苹果官方给出的解释:

2E04731A-B0C8-4341-9B1D-D8FBB09B6484.png

现在让我这个英语半吊子来翻译一下,他的意思就是说如果你不调用这个方法设置一张背景图片的话,那我就给你默认一张,然后同时还有一张阴影图片被默认设置上去,好吧,这就是导航栏上1px黑线的由来,没错,就是这个苹果赠送的shadowImage。

相信看到这个解释即使不继续说下去有些同学也知道解决办法了吧,没错嘛,你就用上面说的那个方法,给设置一张背景图片,然后在设置一张shadowImage就可以了,就像这样:

这就需要我们做进一步处理,把线去掉,如下方法即可:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. self.navigationController.navigationBar.shadowImage = [UIImage new];  
  2. //其实这个线也是image控制的。设为空即可  
<code class="objectivec" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span style="color: rgb(101, 123, 131);">根据UI的设计,navigationbar需要跟界面一体化,但是下面这根黑线是比较烦的问题,可能界面一需要隐藏,界面二就要出现,也可能需要改变粗细之类的,又因为navigationbar会影响接下来的推栈,所以需要做一点小改动.方法</span><span style="color: rgb(42, 161, 152);">2</span><span style="color: rgb(101, 123, 131);">:直接隐藏:</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">//在页面出现的时候就将黑线隐藏起来</span><span style="color: rgb(101, 123, 131);"> -(</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">void</span><span style="color: rgb(101, 123, 131);">)viewWillAppear:(</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">BOOL</span><span style="color: rgb(101, 123, 131);">)animated{    [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationController</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationBar</span><span style="color: rgb(101, 123, 131);"> setBackgroundImage:[</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImage</span><span style="color: rgb(101, 123, 131);"> new] forBarMetrics:</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIBarMetricsDefault</span><span style="color: rgb(101, 123, 131);">];    [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationController</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationBar</span><span style="color: rgb(101, 123, 131);"> setShadowImage:[</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImage</span><span style="color: rgb(101, 123, 131);"> new]];}</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">//在页面消失的时候就让navigationbar还原样式</span><span style="color: rgb(101, 123, 131);">-(</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">void</span><span style="color: rgb(101, 123, 131);">)viewWillDisappear:(</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">BOOL</span><span style="color: rgb(101, 123, 131);">)animated{    [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationController</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationBar</span><span style="color: rgb(101, 123, 131);"> setBackgroundImage:</span><span class="hljs-literal" style="color: rgb(101, 123, 131);">nil</span><span style="color: rgb(101, 123, 131);"> forBarMetrics:</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIBarMetricsDefault</span><span style="color: rgb(101, 123, 131);">];    [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationController</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationBar</span><span style="color: rgb(101, 123, 131);"> setShadowImage:</span><span class="hljs-literal" style="color: rgb(101, 123, 131);">nil</span><span style="color: rgb(101, 123, 131);">];}这个方法唯一的不好就是会影响导航栏的translucent(透明)属性方法</span><span style="color: rgb(42, 161, 152);">3</span><span style="color: rgb(101, 123, 131);">:找出黑线,再做处理:</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">//通过一个方法来找到这个黑线(findHairlineImageViewUnder):</span><span style="color: rgb(101, 123, 131);"> - (</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImageView</span><span style="color: rgb(101, 123, 131);"> *)findHairlineImageViewUnder:(</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIView</span><span style="color: rgb(101, 123, 131);"> *)view {    </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">if</span><span style="color: rgb(101, 123, 131);"> ([view isKindOfClass:</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImageView</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.class</span><span style="color: rgb(101, 123, 131);">] && view</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.bounds</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.size</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.height</span><span style="color: rgb(101, 123, 131);"> <= </span><span class="hljs-number" style="color: rgb(42, 161, 152);">1.0</span><span style="color: rgb(101, 123, 131);">) {        </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span><span style="color: rgb(101, 123, 131);"> (</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImageView</span><span style="color: rgb(101, 123, 131);"> *)view;    }    </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">for</span><span style="color: rgb(101, 123, 131);"> (</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIView</span><span style="color: rgb(101, 123, 131);"> *subview </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">in</span><span style="color: rgb(101, 123, 131);"> view</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.subviews</span><span style="color: rgb(101, 123, 131);">) {        </span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImageView</span><span style="color: rgb(101, 123, 131);"> *imageView = [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span style="color: rgb(101, 123, 131);"> findHairlineImageViewUnder:subview];        </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">if</span><span style="color: rgb(101, 123, 131);"> (imageView) {            </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span><span style="color: rgb(101, 123, 131);"> imageView;        }    }    </span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span><span style="color: rgb(101, 123, 131);"> </span><span class="hljs-literal" style="color: rgb(101, 123, 131);">nil</span><span style="color: rgb(101, 123, 131);">;}</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">//再定义一个imageview来等同于这个黑线 </span><span style="color: rgb(101, 123, 131);"> </span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">UIImageView</span><span style="color: rgb(101, 123, 131);"> *navBarHairlineImageView; navBarHairlineImageView = [</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span style="color: rgb(101, 123, 131);"> findHairlineImageViewUnder:</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">self</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationController</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.navigationBar</span><span style="color: rgb(101, 123, 131);">];同样的在界面出现时候开启隐藏 -(</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">void</span><span style="color: rgb(101, 123, 131);">)viewWillAppear:(</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">BOOL</span><span style="color: rgb(101, 123, 131);">)animated{ navBarHairlineImageView</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.hidden</span><span style="color: rgb(101, 123, 131);"> = </span><span class="hljs-literal" style="color: rgb(101, 123, 131);">YES</span><span style="color: rgb(101, 123, 131);">;}</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">//在页面消失的时候就让出现</span><span style="color: rgb(101, 123, 131);"> -(</span><span class="hljs-keyword" style="color: rgb(133, 153, 0);">void</span><span style="color: rgb(101, 123, 131);">)viewWillAppear:(</span><span class="hljs-built_in" style="color: rgb(38, 139, 210);">BOOL</span><span style="color: rgb(101, 123, 131);">)animated{ navBarHairlineImageView</span><span class="hljs-variable" style="color: rgb(181, 137, 0);">.hidden</span><span style="color: rgb(101, 123, 131);"> = </span><span class="hljs-literal" style="color: rgb(101, 123, 131);">NO</span><span style="color: rgb(101, 123, 131);">;}如果想要做一些更好的处理,比如说改变粗细,颜色之类的也在界面出现的时候写就行了.推荐使用第二种方法,因为整个项目都在使用导航栏推栈,出栈,很可能因为改变了样式,导致后面的属性混乱起来.</span></code>

方法4.下一个解决办法,将UINavigationBar的clipsToBounds属性设成YES就好啦,从此黑线去无踪。

方法5.最后一个办法,就是循环遍历一下UINavigationBar的所有子视图,发现有UIImageView类型的视图就remove掉,或者设成隐藏状态(hidden)。虽然也能达到想要的效果,但是感觉这个方法太暴力了,不是很推荐。


(2)如果你想在透明的基础上实现根据下拉距离,由透明变得不透明的效果,那么上面那个就显得力不从心了,这就需要我们采用另外一种方法了
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手  
  2. [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果  

四、全局设置navigationBar标题样式和barItem的标题样式

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //UIColorWithHexRGB( )这个方法是自己定义的,这里只需要给个颜色就好了  
  2.  [[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)];  
  3.    
  4.  [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];  
  5.    
  6.  [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)} forState:UIControlStateNormal];  


五、navigationBar隐藏和显示的过度

相信在使用中肯定遇到过,一个页面隐藏navigationBar,另一个不隐藏。两个页面进行push和pop的时候,尤其是有侧滑手势返回的时候,不做处理就会造成滑动返回时,navigationBar位置是空的,直接显示一个黑色或者显示下面一层视图,很难看。这就需要我们加入过度动画来隐藏或显示navigationBar:
在返回后将要出现的页面实现viewWillAppear方法,需要隐藏就设为YES,需要显示就设为NO
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (void)viewWillAppear:(BOOL)animated{  
  2.     [super viewWillAppear:animated];  
  3.     [self.navigationController setNavigationBarHidden:NO animated:YES];  
  4. }  


六、给webView添加头视图

webView是一个复合视图,里面包含有一个scrollView,scrollView里面是一个UIWebBrowserView(负责显示WebView的内容)
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView  
  2. self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(00, kScreenWidth, kScreenWidth*2/3.0)];  
  3. [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];  
  4. [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView];  
  5. //把backHeadImageView插入到webView的scrollView下面  
  6. CGRect frame = self.webBrowserView.frame;  
  7. frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame);  
  8. self.webBrowserView.frame = frame;  
  9. //更改webBrowserView的frame向下移backHeadImageView的高度,使其可见  


七、模态跳转的动画设置

设置模态跳转的动画,系统提供了四种可供选择
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. DetailViewController *detailVC = [[DetailViewController alloc]init];  
  2. //UIModalTransitionStyleFlipHorizontal 翻转  
  3. //UIModalTransitionStyleCoverVertical 底部滑出  
  4. //UIModalTransitionStyleCrossDissolve 渐显  
  5. //UIModalTransitionStylePartialCurl 翻页  
  6. detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;  
  7. [self presentViewController:detailVC animated:YES completion:nil];  



八、图片处理:只拿图片的一部分

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UIImage *image = [UIImage imageNamed:filename];  
  2. CGImageRef imageRef = image.CGImage;  
  3. CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);  
  4. //这里的宽高是相对于图片的真实大小  
  5. //比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可  
  6. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);  
  7. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];  


九、给TableView或者CollectionView的cell添加简单的动画,像这样


只要在willDisplayCell方法中对将要显示的cell做动画即可:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.     NSArray *array =  tableView.indexPathsForVisibleRows;  
  3.     NSIndexPath *firstIndexPath = array[0];  
  4.    
  5.     //设置anchorPoint  
  6.     cell.layer.anchorPoint = CGPointMake(00.5);  
  7.     //为了防止cell视图移动,重新把cell放回原来的位置  
  8.     cell.layer.position = CGPointMake(0, cell.layer.position.y);  
  9.    
  10.     //设置cell 按照z轴旋转90度,注意是弧度  
  11.     if (firstIndexPath.row < indexPath.row) {  
  12.             cell.layer.transform = CATransform3DMakeRotation(M_PI_2001.0);  
  13.     }else{  
  14.         cell.layer.transform = CATransform3DMakeRotation(- M_PI_2001.0);  
  15.     }  
  16.    
  17.     cell.alpha = 0.0;  
  18.    
  19.     [UIView animateWithDuration:1 animations:^{  
  20.         cell.layer.transform = CATransform3DIdentity;  
  21.         cell.alpha = 1.0;  
  22.     }];  
  23. }  
  24. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{  
  25.    
  26.     if (indexPath.row % 2 != 0) {  
  27.         cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/20);  
  28.     }else{  
  29.         cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/20);  
  30.     }  
  31.     cell.alpha = 0.0;  
  32.     [UIView animateWithDuration:0.7 animations:^{  
  33.         cell.transform = CGAffineTransformIdentity;  
  34.         cell.alpha = 1.0;  
  35.     } completion:^(BOOL finished) {  
  36.    
  37.     }];  
  38. }  

十、点击屏幕隐藏键盘

这个需求应该是非常常见的,一个应用总有那么几个UITextField控件,当输入完成后,可以通过点击屏幕来隐藏键盘。具体实现如下:
(1)在ViewController的viewDidload方法中增加一个点击手势,
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UITapGestureRecognizer *tap =   
  2. [[UITapGestureRecognizer alloc]  
  3. initWithTarget:self action:@selector(dismissKeyboard)];  
  4.   
  5. [self.view addGestureRecognizer:tap];  
(2)实现dissmissKeyboard方法 在方法内隐藏键盘
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [textField resignFirstResponder];//textField是需要监听的UITextField对象  

十一、隐藏UITabbar

这个需求在列表页转场至详细页面中会遇到。例如在tableView的 didSelectRowAtIndexPath方法中可以使用,实现方法如下:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. detailViewController *dc = [[detailViewController alloc]init];  
  2. dc.hidesBottomBarWhenPushed = YES;  
  3. [self.navigationController pushViewController:dc animated:YES];  

十二、iOS中字符串过滤掉非法字符

这里简单说两种方法,第一种就是替代法:使用stringByReplacingOccurrencesOfString这个方法将字符串里的非法字符逐个替代
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*1*/ tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];  
  2. /*2 */tempString = [tempString stringByReplacingOccurrencesOfString:@"#" withString:@""];  
  3. /*3*/ tempString = [tempString stringByReplacingOccurrencesOfString:@"*" withString:@""];  
  4. /*4*/ tempString = [tempString stringByReplacingOccurrencesOfString:@"+" withString:@""];  
  5. /*5*/ tempString = [tempString stringByReplacingOccurrencesOfString:@"-" withString:@""];  

这样做比较烦的一个问题就是,如果要过滤掉的非法字符有很多的话,就得写多行这种替代代码。

另外一种方法比较巧妙,先将字符串按非法字符集进行截断最后再拼接起来。代码看起来很简直,直接了断。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*1*/ NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"[]{}(#%-*+=_)\\|~(<>$%^&*)_+ "];  
  2. /*2*/ tempString = [[tempString componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString: @""];  

doNotWant这个字符集里想写几个就写几个。

另外不能用stringByTrimmingCharactersInSet这个方法进行过滤,它能做到的仅仅是把字符串两端的非法字符过滤,但是包含在字符串里非法字符则无能为力。














1. 获取磁盘总空间大小




2. 获取磁盘可用空间大小




3. 获取指定路径下某个文件的大小




4. 获取文件夹下所有文件的大小




5. 获取字符串(或汉字)首字母




6. 将字符串数组按照元素首字母顺序进行排序分组




使用如下:




输出结果如下:




7. 获取当前时间




8. 计算上次日期距离现在多久, 如 xx 小时前、xx 分钟前等




使用如下:




输出结果如下:




9. 判断手机号码格式是否正确




10. 判断邮箱格式是否正确




11. 将十六进制颜色转换为 UIColor 对象




12. 对图片进行滤镜处理



13. 对图片进行模糊处理



14. 调整图片饱和度、亮度、对比度



15. 创建一张实时模糊效果 View (毛玻璃效果)



16. 全屏截图



17. 截取一张 view 生成图片



18. 截取view中某个区域生成一张图片



19. 压缩图片到指定尺寸大小



20. 压缩图片到指定文件大小



21. 获取设备 IP 地址

需要先引入下头文件:



代码:



22. 判断字符串中是否含有空格



23. 判断字符串中是否含有某个字符串



24. 判断字符串中是否含有中文



25. 判断字符串是否全部为数字



26. 绘制虚线




1 0
原创粉丝点击