自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS 10個(gè)實(shí)用小技巧(總有你不知道的和你會(huì)用到的)

移動(dòng)開(kāi)發(fā) iOS
在開(kāi)發(fā)過(guò)程中我們總會(huì)遇到各種各樣的小問(wèn)題,有些小問(wèn)題并不是十分容易解決。在此我就總結(jié)一下,我在開(kāi)發(fā)中遇到的各種小問(wèn)題,以及我的解決方法。比較普遍的我就不再提了,這里主要講一些你可能不知道的。

[[184792]]

在開(kāi)發(fā)過(guò)程中我們總會(huì)遇到各種各樣的小問(wèn)題,有些小問(wèn)題并不是十分容易解決。在此我就總結(jié)一下,我在開(kāi)發(fā)中遇到的各種小問(wèn)題,以及我的解決方法。比較普遍的我就不再提了,這里主要講一些你可能不知道的(當(dāng)然,也有可能你都知道,大神就不必往下看了)

1、控件的局部圓角問(wèn)題

你是不是也遇到過(guò)這樣的問(wèn)題,一個(gè)button或者label,只要右邊的兩個(gè)角圓角,或者只要一個(gè)圓角。該怎么辦呢。這就需要圖層蒙版來(lái)幫助我們了

  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];//創(chuàng)建shapelayer 
  10.  
  11.     masklayer.frame = button.bounds; 
  12.  
  13.     masklayer.path = path.CGPath;//設(shè)置路徑 
  14.  
  15.     button.layer.mask = masklayer;  

舉例為button,其它繼承自UIView的控件都可以

2、navigationBar的透明問(wèn)題

如果僅僅把navigationBar的alpha設(shè)為0的話,那就相當(dāng)于把navigationBar給隱藏了,大家都知道,父視圖的alpha設(shè)置為0的話,那么子視圖全都會(huì)透明的。那么相應(yīng)的navigationBar的標(biāo)題和左右兩個(gè)按鈕都會(huì)消失。這樣顯然達(dá)不到我們要求的效果。

(1)如果僅僅是想要navigationBar透明,按鈕和標(biāo)題都在可以使用以下方法:

  1. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] 
  2.  
  3. forBarMetrics:UIBarMetricsDefault];//給navigationBar設(shè)置一個(gè)空的背景圖片即可實(shí)現(xiàn)透明,而且標(biāo)題按鈕都在  

細(xì)心的你會(huì)發(fā)現(xiàn)上面有一條線如下圖: 

 

 

 

這就需要我們做進(jìn)一步處理,把線去掉,如下方法即可:

  1. self.navigationController.navigationBar.shadowImage = [UIImage new]; 
  2.  
  3. //其實(shí)這個(gè)線也是image控制的。設(shè)為空即可  

(2)如果你想在透明的基礎(chǔ)上實(shí)現(xiàn)根據(jù)下拉距離,由透明變得不透明的效果,那么上面那個(gè)就顯得力不從心了,這就需要我們采用另外一種方法了

  1. //navigationBar是一個(gè)復(fù)合視圖,它是有許多個(gè)控件組成的,那么我們就可以從他的內(nèi)部入手 
  2.  
  3. [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//這里可以根據(jù)scrollView的偏移量來(lái)設(shè)置alpha就實(shí)現(xiàn)了漸變透明的效果  

3、全局設(shè)置navigationBar標(biāo)題的樣式和barItem的標(biāo)題樣式

  1. //UIColorWithHexRGB( )這個(gè)方法是自己定義的,這里只需要給個(gè)顏色就好了 
  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隱藏顯示的過(guò)度

相信在使用中肯定遇到過(guò),一個(gè)頁(yè)面隱藏navigationBar,另一個(gè)不隱藏。兩個(gè)頁(yè)面進(jìn)行push和pop的時(shí)候,尤其是有側(cè)滑手勢(shì)返回的時(shí)候,不做處理就會(huì)造成滑動(dòng)返回時(shí),navigationBar位置是空的,直接顯示一個(gè)黑色或者顯示下面一層視圖,很難看。這就需要我們加入過(guò)度動(dòng)畫來(lái)隱藏或顯示navigationBar:

在返回后將要出現(xiàn)的頁(yè)面實(shí)現(xiàn)viewWillAppear方法,需要隱藏就設(shè)為YES,需要顯示就設(shè)為NO

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

5、側(cè)滑手勢(shì)返回

iOS的側(cè)滑返回手勢(shì)有著很好的操作體驗(yàn),不支持側(cè)滑返回的應(yīng)用絕對(duì)不是好應(yīng)用。但是在開(kāi)發(fā)過(guò)程中在自定義了返回按鈕,或者某些webView,tableView等頁(yè)面,側(cè)滑返回手勢(shì)失效,這時(shí)候就需要我們來(lái)進(jìn)行設(shè)置一下了,可以在基類里面協(xié)商如下代碼:

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

問(wèn)題:當(dāng)返回navigationController的最頂層的Controller的時(shí)候。再次側(cè)滑,這個(gè)時(shí)候你在點(diǎn)擊一個(gè)push頁(yè)面的操作,你會(huì)發(fā)現(xiàn)卡那了,半天才會(huì)有反應(yīng)。

這是由于,在最頂層Controller手勢(shì)依然有效,但是滑動(dòng)后,并找不到返回的頁(yè)面。造成軟件卡頓,假死所以就要在rootViewController中讓此手勢(shì)失效。把下面的設(shè)為NO

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

當(dāng)然你也可以使用一個(gè)第三方庫(kù),寫的相當(dāng)棒。他對(duì)系統(tǒng)的側(cè)滑返回手勢(shì)進(jìn)行拓展,不用從邊緣滑動(dòng),只要右滑即可返回。最重要的是,他只需要加入項(xiàng)目中即可,不需要一行代碼即可實(shí)現(xiàn)。附上github 網(wǎng)址

https://github.com/forkingdog/FDFullscreenPopGesture

6、給webView添加頭視圖

webView是一個(gè)復(fù)合視圖,里面包含有一個(gè)scrollView,scrollView里面是一個(gè)UIWebBrowserView(負(fù)責(zé)顯示W(wǎng)ebView的內(nèi)容)

  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的高度,使其可見(jiàn)  

7、模態(tài)跳轉(zhuǎn)的動(dòng)畫設(shè)置

設(shè)置模態(tài)跳轉(zhuǎn)的動(dòng)畫,系統(tǒng)提供了四種可供選擇

  1. DetailViewController *detailVC = [[DetailViewController alloc]init]; 
  2.  
  3.     //UIModalTransitionStyleFlipHorizontal 翻轉(zhuǎn) 
  4.  
  5.     //UIModalTransitionStyleCoverVertical 底部滑出 
  6.  
  7.     //UIModalTransitionStyleCrossDissolve 漸顯 
  8.  
  9.     //UIModalTransitionStylePartialCurl 翻頁(yè) 
  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. //這里的寬高是相對(duì)于圖片的真實(shí)大小 
  8.  
  9. //比如你的圖片是400x400的那么(0,0,400,400)就是圖片的全尺寸,想取哪一部分就設(shè)置相應(yīng)坐標(biāo)即可 
  10.  
  11. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); 
  12.  
  13. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];  

9、給UIView設(shè)置圖片

  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. //同樣可以設(shè)置顯示的圖片范圍 
  8.  
  9. //不過(guò)此處略有不同,這里的四個(gè)值均為0-1之間;對(duì)應(yīng)的依然是寫x,y,widt,height  

10、給TableView或者CollectionView的cell添加簡(jiǎn)單動(dòng)畫

只要在willDisplayCell方法中對(duì)將要顯示的cell做動(dòng)畫即可:

  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.     //設(shè)置anchorPoint 
  10.  
  11.     cell.layer.anchorPoint = CGPointMake(0, 0.5); 
  12.  
  13.     //為了防止cell視圖移動(dòng),重新把cell放回原來(lái)的位置 
  14.  
  15.     cell.layer.position = CGPointMake(0, cell.layer.position.y); 
  16.  
  17.   
  18.  
  19.     //設(shè)置cell 按照z軸旋轉(zhuǎn)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.  
責(zé)任編輯:龐桂玉 來(lái)源: iOS大全
相關(guān)推薦

2009-04-14 21:38:05

LinuxUbuntu技巧

2020-07-11 09:45:33

Python編程語(yǔ)言開(kāi)發(fā)

2023-12-21 14:40:09

Python編程語(yǔ)言

2022-09-20 11:58:27

NpmNode.js

2017-07-21 09:48:45

SQL索引查詢

2019-11-29 16:49:42

HTML語(yǔ)言開(kāi)發(fā)

2021-03-18 14:02:56

iOS蘋果細(xì)節(jié)

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

2017-03-02 14:05:42

AndroidAndroid Stu調(diào)試技巧

2015-08-13 09:03:14

調(diào)試技巧

2020-01-29 19:40:36

Python美好,一直在身邊Line

2021-01-05 11:22:58

Python字符串代碼

2022-12-07 08:16:50

Vue 3技巧數(shù)組

2022-04-30 19:22:35

Python編程語(yǔ)言

2019-06-04 08:38:24

2021-05-12 10:48:02

蘋果技巧功能

2018-05-08 15:30:46

程序員代碼框架
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)