標籤:
本文轉載至 http://www.jianshu.com/p/a3156826c27c 在開發過程中我們總會遇到各種各樣的小問題,有些小問題並不是十分容易解決。在此我就總結一下,我在開發中遇到的各種小問題,以及我的解決方案。比較普遍的我就不再提了,這裡主要講一些你可能不知道的(當然,也有可能你都知道,大神就不必往下看了)1、控制項的局部圓角問題
你是不是也遇到過這樣的問題,一個button或者label,只要右邊的兩個角圓角,或者只要一個圓角。該怎麼辦呢。這就需要圖層蒙版來協助我們了
CGRect rect = CGRectMake(0, 0, 100, 50); CGSize radio = CGSizeMake(5, 5);//圓角尺寸 UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這隻圓角位置 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio]; CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//建立shapelayer masklayer.frame = button.bounds; masklayer.path = path.CGPath;//設定路徑 button.layer.mask = masklayer;
舉例為button,其它繼承自UIView的控制項都可以
2、navigationBar的透明問題
如果僅僅把navigationBar的alpha設為0的話,那就相當於把navigationBar給隱藏了,大家都知道,父視圖的alpha設定為0的話,那麼子視圖全都會透明的。那麼相應的navigationBar的標題和左右兩個按鈕都會消失。這樣顯然達不到我們要求的效果。
(1)如果僅僅是想要navigationBar透明,按鈕和標題都在可以使用以下方法:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];//給navigationBar設定一個空的背景圖片即可實現透明,而且標題按鈕都在
細心的你會發現上面有一條線如:
IMG_0025.PNG
這就需要我們做進一步處理,把線去掉,如下方法即可:
self.navigationController.navigationBar.shadowImage = [UIImage new];//其實這個線也是image控制的。設為空白即可
(2)如果你想在透明的基礎上實現根據下拉距離,由透明變得不透明的效果,那麼上面那個就顯得力不從心了,這就需要我們採用另外一種方法了
//navigationBar是一個複合視圖,它是有許多個控制群組成的,那麼我們就可以從他的內部入手[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//這裡可以根據scrollView的位移量來設定alpha就實現了漸層透明的效果
3、全域設定navigationBar標題的樣式和barItem的標題樣式
//UIColorWithHexRGB( )這個方法是自己定義的,這裡只需要給個顏色就好了 [[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}]; [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)} forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz
4、navigationBar隱藏顯示的過度
相信在使用中肯定遇到過,一個頁面隱藏navigationBar,另一個不隱藏。兩個頁面進行push和pop的時候,尤其是有側滑手勢返回的時候,不做處理就會造成滑動返回時,navigationBar位置是空的,直接顯示一個黑色或者顯示下面一層視圖,很難看。這就需要我們加入過度動畫來隱藏或顯示navigationBar:
在返回後將要出現的頁面實現viewWillAppear方法,需要隱藏就設為YES,需要顯示就設為NO
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES];}
5、側滑手勢返回
iOS的側滑返回手勢有著很好的操作體驗,不支援側滑返回的應用絕對不是好應用。但是在開發過程中在自訂了返回按鈕,或者某些webView,tableView等頁面,側滑返回手勢失效,這時候就需要我們來進行設定一下了,可以在基類裡面協商如下代碼:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {//需要遵循一下手勢的代理 self.navigationController.interactivePopGestureRecognizer.delegate = self; self.navigationController.interactivePopGestureRecognizer.enabled = YES; }
問題:當返回navigationController的最頂層的Controller的時候。再次側滑,這個時候你在點擊一個push頁面的操作,你會發現卡那了,半天才會有反應。
這是由於,在最頂層Controller手勢依然有效,但是滑動後,並找不到返回的頁面。造成軟體卡頓,假死所以就要在rootViewController中讓此手勢失效。把下面的設為NO
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
當然你也可以使用一個第三方庫,寫的相當棒。他對系統的側滑返回手勢進行拓展,不用從邊緣滑動,只要右滑即可返回。最重要的是,他只需要加入項目中即可,不需要一行代碼即可實現。附上github 網址
6、給webView添加頭視圖
webView是一個複合視圖,裡麵包含有一個scrollView,scrollView裡面是一個UIWebBrowserView(負責顯示WebView的內容)
UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth*2/3.0)]; [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]]; [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView]; //把backHeadImageView插入到webView的scrollView下面 CGRect frame = self.webBrowserView.frame; frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame); self.webBrowserView.frame = frame; //更改webBrowserView的frame向下移backHeadImageView的高度,使其可見
7、模態跳轉的動畫設定
設定模態跳轉的動畫,系統提供了四種可供選擇
DetailViewController *detailVC = [[DetailViewController alloc]init]; //UIModalTransitionStyleFlipHorizontal 翻轉 //UIModalTransitionStyleCoverVertical 底部滑出 //UIModalTransitionStyleCrossDissolve 漸顯 //UIModalTransitionStylePartialCurl 翻頁 detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl; [self presentViewController:detailVC animated:YES completion:nil];
8、圖片處理只拿到圖片的一部分
UIImage *image = [UIImage imageNamed:filename];CGImageRef imageRef = image.CGImage;CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);//這裡的寬高是相對於圖片的真實大小//比如你的圖片是400x400的那麼(0,0,400,400)就是圖片的全尺寸,想取哪一部分就設定相應座標即可CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
9、給UIView設定圖片
UIImage *image = [UIImage imageNamed:@"playing"]; _layerView.layer.contents = (__bridge id)image.CGImage;_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);//同樣可以設定顯示的圖片範圍//不過此處略有不同,這裡的四個值均為0-1之間;對應的依然是寫x,y,widt,height
10、給TableView或者CollectionView的cell添加簡單動畫,像這樣
888.gif
555.gif
只要在willDisplayCell方法中對將要顯示的cell做動畫即可:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ NSArray *array = tableView.indexPathsForVisibleRows; NSIndexPath *firstIndexPath = array[0]; //設定anchorPoint cell.layer.anchorPoint = CGPointMake(0, 0.5); //為了防止cell視圖移動,重新把cell放回原來的位置 cell.layer.position = CGPointMake(0, cell.layer.position.y); //設定cell 按照z軸旋轉90度,注意是弧度 if (firstIndexPath.row < indexPath.row) { cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0); }else{ cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0); } cell.alpha = 0.0; [UIView animateWithDuration:1 animations:^{ cell.layer.transform = CATransform3DIdentity; cell.alpha = 1.0; }];}- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row % 2 != 0) { cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0); }else{ cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0); } cell.alpha = 0.0; [UIView animateWithDuration:0.7 animations:^{ cell.transform = CGAffineTransformIdentity; cell.alpha = 1.0; } completion:^(BOOL finished) { }];}
iOS10個實用小技巧(總有你不知道的和你會用到的)