iOS 10个实用小技巧(总有你不知道的和你会用到的)

移动开发 iOS
在开发过程中我们总会遇到各种各样的小问题,有些小问题并不是十分容易解决。在此我就总结一下,我在开发中遇到的各种小问题,以及我的解决方法。比较普遍的我就不再提了,这里主要讲一些你可能不知道的。

[[184792]]

在开发过程中我们总会遇到各种各样的小问题,有些小问题并不是十分容易解决。在此我就总结一下,我在开发中遇到的各种小问题,以及我的解决方法。比较普遍的我就不再提了,这里主要讲一些你可能不知道的(当然,也有可能你都知道,大神就不必往下看了)

1、控件的局部圆角问题

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

  1. CGRect rect = CGRectMake(0, 0, 100, 50); 
  2.  
  3.     CGSize radio = CGSizeMake(5, 5);//圆角尺寸 
  4.  
  5.     UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//这只圆角位置 
  6.  
  7.     UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio]; 
  8.  
  9.     CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer 
  10.  
  11.     masklayer.frame = button.bounds; 
  12.  
  13.     masklayer.path = path.CGPath;//设置路径 
  14.  
  15.     button.layer.mask = masklayer;  

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

2、navigationBar的透明问题

如果仅仅把navigationBar的alpha设为0的话,那就相当于把navigationBar给隐藏了,大家都知道,父视图的alpha设置为0的话,那么子视图全都会透明的。那么相应的navigationBar的标题和左右两个按钮都会消失。这样显然达不到我们要求的效果。

(1)如果仅仅是想要navigationBar透明,按钮和标题都在可以使用以下方法:

  1. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] 
  2.  
  3. forBarMetrics:UIBarMetricsDefault];//给navigationBar设置一个空的背景图片即可实现透明,而且标题按钮都在  

细心的你会发现上面有一条线如下图: 

 

 

 

这就需要我们做进一步处理,把线去掉,如下方法即可:

  1. self.navigationController.navigationBar.shadowImage = [UIImage new]; 
  2.  
  3. //其实这个线也是image控制的。设为空即可  

(2)如果你想在透明的基础上实现根据下拉距离,由透明变得不透明的效果,那么上面那个就显得力不从心了,这就需要我们采用另外一种方法了

  1. //navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手 
  2.  
  3. [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果  

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

  1. //UIColorWithHexRGB( )这个方法是自己定义的,这里只需要给个颜色就好了 
  2.  
  3. [[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)]; 
  4.  
  5.  
  6.     [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];  
  7.   
  8.  
  9.     [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFontboldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)}forState:UIControlStateNormal]; 
  10.  
  11.   
  12.   [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz 

 4、navigationBar隐藏显示的过度

相信在使用中肯定遇到过,一个页面隐藏navigationBar,另一个不隐藏。两个页面进行push和pop的时候,尤其是有侧滑手势返回的时候,不做处理就会造成滑动返回时,navigationBar位置是空的,直接显示一个黑色或者显示下面一层视图,很难看。这就需要我们加入过度动画来隐藏或显示navigationBar:

在返回后将要出现的页面实现viewWillAppear方法,需要隐藏就设为YES,需要显示就设为NO

  1. - (void)viewWillAppear:(BOOL)animated{ 
  2.  
  3.     [super viewWillAppear:animated]; 
  4.  
  5.     [self.navigationController setNavigationBarHidden:NO animated:YES]; 
  6.  
  7.  

5、侧滑手势返回

iOS的侧滑返回手势有着很好的操作体验,不支持侧滑返回的应用绝对不是好应用。但是在开发过程中在自定义了返回按钮,或者某些webView,tableView等页面,侧滑返回手势失效,这时候就需要我们来进行设置一下了,可以在基类里面协商如下代码:

  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 
  2.  
  3. //需要遵循一下手势的代理        self.navigationController.interactivePopGestureRecognizer.delegate = self; 
  4.  
  5.         self.navigationController.interactivePopGestureRecognizer.enabled = YES; 
  6.  
  7.     }  

问题:当返回navigationController的最顶层的Controller的时候。再次侧滑,这个时候你在点击一个push页面的操作,你会发现卡那了,半天才会有反应。

这是由于,在最顶层Controller手势依然有效,但是滑动后,并找不到返回的页面。造成软件卡顿,假死所以就要在rootViewController中让此手势失效。把下面的设为NO

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

当然你也可以使用一个第三方库,写的相当棒。他对系统的侧滑返回手势进行拓展,不用从边缘滑动,只要右滑即可返回。最重要的是,他只需要加入项目中即可,不需要一行代码即可实现。附上github 网址

https://github.com/forkingdog/FDFullscreenPopGesture

6、给webView添加头视图

webView是一个复合视图,里面包含有一个scrollView,scrollView里面是一个UIWebBrowserView(负责显示WebView的内容)

  1. UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView 
  2.  
  3.     self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenWidth*2/3.0)]; 
  4.  
  5.     [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImageimageNamed:@"placeholderImage"]]; 
  6.  
  7.     [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView]; 
  8.  
  9.     //把backHeadImageView插入到webView的scrollView下面 
  10.  
  11.     CGRect frame = self.webBrowserView.frame; 
  12.  
  13.     frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame); 
  14.  
  15.     self.webBrowserView.frame = frame; 
  16.  
  17.     //更改webBrowserView的frame向下移backHeadImageView的高度,使其可见  

7、模态跳转的动画设置

设置模态跳转的动画,系统提供了四种可供选择

  1. DetailViewController *detailVC = [[DetailViewController alloc]init]; 
  2.  
  3.     //UIModalTransitionStyleFlipHorizontal 翻转 
  4.  
  5.     //UIModalTransitionStyleCoverVertical 底部滑出 
  6.  
  7.     //UIModalTransitionStyleCrossDissolve 渐显 
  8.  
  9.     //UIModalTransitionStylePartialCurl 翻页 
  10.  
  11.     detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
  12.  
  13.     [self presentViewController:detailVC animated:YES completion:nil];  

8、图片处理只拿到图片的一部分

  1. UIImage *image = [UIImage imageNamed:filename]; 
  2.  
  3. CGImageRef imageRef = image.CGImage; 
  4.  
  5. CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height); 
  6.  
  7. //这里的宽高是相对于图片的真实大小 
  8.  
  9. //比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可 
  10.  
  11. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); 
  12.  
  13. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];  

9、给UIView设置图片

  1. UIImage *image = [UIImage imageNamed:@"playing"]; 
  2.  
  3.     _layerView.layer.contents = (__bridge id)image.CGImage; 
  4.  
  5. _layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5); 
  6.  
  7. //同样可以设置显示的图片范围 
  8.  
  9. //不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height  

10、给TableView或者CollectionView的cell添加简单动画

只要在willDisplayCell方法中对将要显示的cell做动画即可:

  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{ 
  2.  
  3.     NSArray *array =  tableView.indexPathsForVisibleRows; 
  4.  
  5.     NSIndexPath *firstIndexPath = array[0]; 
  6.  
  7.   
  8.  
  9.     //设置anchorPoint 
  10.  
  11.     cell.layer.anchorPoint = CGPointMake(0, 0.5); 
  12.  
  13.     //为了防止cell视图移动,重新把cell放回原来的位置 
  14.  
  15.     cell.layer.position = CGPointMake(0, cell.layer.position.y); 
  16.  
  17.   
  18.  
  19.     //设置cell 按照z轴旋转90度,注意是弧度 
  20.  
  21.     if (firstIndexPath.row < indexPath.row) { 
  22.  
  23.             cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0); 
  24.  
  25.     }else
  26.  
  27.         cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0); 
  28.  
  29.     } 
  30.  
  31.   
  32.  
  33.     cell.alpha = 0.0; 
  34.  
  35.   
  36.  
  37.     [UIView animateWithDuration:1 animations:^{ 
  38.  
  39.         cell.layer.transform = CATransform3DIdentity; 
  40.  
  41.         cell.alpha = 1.0; 
  42.  
  43.     }]; 
  44.  
  45.  
  46. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cellforItemAtIndexPath:(NSIndexPath *)indexPath{ 
  47.  
  48.   
  49.  
  50.     if (indexPath.row % 2 != 0) { 
  51.  
  52.         cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0); 
  53.  
  54.     }else
  55.  
  56.         cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0); 
  57.  
  58.     } 
  59.  
  60.     cell.alpha = 0.0; 
  61.  
  62.     [UIView animateWithDuration:0.7 animations:^{ 
  63.  
  64.         cell.transform = CGAffineTransformIdentity; 
  65.  
  66.         cell.alpha = 1.0; 
  67.  
  68.     } completion:^(BOOL finished) { 
  69.  
  70.   
  71.  
  72.     }]; 
  73.  
  74.  
责任编辑:庞桂玉 来源: iOS大全
相关推荐

2009-04-14 21:38:05

LinuxUbuntu技巧

2020-07-11 09:45:33

Python编程语言开发

2023-12-21 14:40:09

Python编程语言

2022-09-20 11:58:27

NpmNode.js

2017-07-21 09:48:45

SQL索引查询

2021-03-18 14:02:56

iOS苹果细节

2019-11-29 16:49:42

HTML语言开发

2023-07-07 14:47:46

JavaScript技巧

2020-08-11 11:20:49

Linux命令使用技巧

2017-02-23 19:42:55

AS Android代码

2024-03-04 00:00:00

Kubernetes技巧API

2022-12-07 08:16:50

Vue 3技巧数组

2022-04-30 19:22:35

Python编程语言

2021-05-12 10:48:02

苹果技巧功能

2019-06-04 08:38:24

2021-01-05 11:22:58

Python字符串代码

2015-08-13 09:03:14

调试技巧

2017-03-02 14:05:42

AndroidAndroid Stu调试技巧

2020-01-29 19:40:36

Python美好,一直在身边Line

2018-05-08 15:30:46

程序员代码框架
点赞
收藏

51CTO技术栈公众号