iOS開發(fā)技巧

1、控件的局部圓角問題

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

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];//創(chuàng)建shapelayer    masklayer.frame = button.bounds;
   masklayer.path = path.CGPath;//設(shè)置路徑    button.layer.mask = masklayer;
舉例為button,其它繼承自UIView的控件都可以

2、navigationBar的透明問題

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

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] 
forBarMetrics:UIBarMetricsDefault];//給navigationBar

設(shè)置一個(gè)空的背景圖片即可實(shí)現(xiàn)透明,而且標(biāo)題按鈕都在

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

這就需要我們做進(jìn)一步處理,把線去掉,如下方法即可:
self.navigationController.navigationBar.shadowImage = [UIImage new];//其實(shí)這個(gè)線也是image控制的。設(shè)為空即可

(2)如果你想在透明的基礎(chǔ)上實(shí)現(xiàn)根據(jù)下拉距離,由透明變得不透明的效果,那么上面那個(gè)就顯得力不從心了,這就需要我們采用另外一種方法了
//navigationBar是一個(gè)復(fù)合視圖,它是有許多個(gè)控件組成的,那么我們就可以從他的內(nèi)部入手

 [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//這里可以根據(jù)scrollView的偏移量來設(shè)置alpha就實(shí)現(xiàn)了漸變透明的效果

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

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

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

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

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

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

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

問題:當(dāng)返回navigationController的最頂層的Controller的時(shí)候。再次側(cè)滑,這個(gè)時(shí)候你在點(diǎn)擊一個(gè)push頁面的操作,你會(huì)發(fā)現(xiàn)卡那了,半天才會(huì)有反應(yīng)。
  這是由于,在最頂層Controller手勢(shì)依然有效,但是滑動(dòng)后,并找不到返回的頁面。造成軟件卡頓,假死所以就要在rootViewController中讓此手勢(shì)失效。把下面的設(shè)為NO

6、給webView添加頭視圖

webView是一個(gè)復(fù)合視圖,里面包含有一個(gè)scrollView,scrollView里面是一個(gè)UIWebBrowserView(負(fù)責(zé)顯示W(wǎng)ebView的內(nèi)容)
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、模態(tài)跳轉(zhuǎn)的動(dòng)畫設(shè)置

設(shè)置模態(tài)跳轉(zhuǎn)的動(dòng)畫,系統(tǒng)提供了四種可供選擇
DetailViewController *detailVC = [[DetailViewController alloc]init];
 //UIModalTransitionStyleFlipHorizontal 翻轉(zhuǎn) //UIModalTransitionStyleCoverVertical 底部滑出 //UIModalTransitionStyleCrossDissolve 漸顯 //UIModalTransitionStylePartialCurl 翻頁    detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentViewController:detailVC animated:YES completion:nil];

8、圖片處理只拿到圖片的一部分

UIImage *image = [UIImage imageNamed:filename];
//這里的寬高是相對(duì)于圖片的真實(shí)大小//比如你的圖片是400x400的那么(0,0,400,400)就是圖片的全尺寸,想取哪一部分就設(shè)置相應(yīng)坐標(biāo)即可
CGImageRef imageRef = image.CGImage;CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];

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

UIImage *image = [UIImage imageNamed:@"playing"];
    _layerView.layer.contents = (__bridge id)image.CGImage;
_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);//同樣可以設(shè)置顯示的圖片范圍//不過此處略有不同,這里的四個(gè)值均為0-1之間;對(duì)應(yīng)的依然是寫x,y,widt,height
10、給TableView或者CollectionView的cell添加簡(jiǎn)單動(dòng)畫,像這樣
image.png
image.png
只要在willDisplayCell方法中對(duì)將要顯示的cell做動(dòng)畫即可:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
 NSArray *array =  tableView.indexPathsForVisibleRows;
 NSIndexPath *firstIndexPath = array[0];
 
 //設(shè)置anchorPoint    cell.layer.anchorPoint = CGPointMake(0, 0.5);
 //為了防止cell視圖移動(dòng),重新把cell放回原來的位置    cell.layer.position = CGPointMake(0, cell.layer.position.y);
 
 //設(shè)置cell 按照z軸旋轉(zhuǎn)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) {
 
    }];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在這里總結(jié)一些iOS開發(fā)中的小技巧,能大大方便我們的開發(fā),持續(xù)更新。 UITableView的Group樣式下頂部...
    Fendouzhe閱讀 338評(píng)論 2 4
  • 通過自己開發(fā)以及借鑒的別人的經(jīng)驗(yàn),總結(jié)一下一些開發(fā)中經(jīng)常用到的技巧知識(shí)點(diǎn),也算是做個(gè)小筆記吧。 1、控件的局部圓角...
    男兒心閱讀 887評(píng)論 0 1
  • 1.NSString過濾特殊字符串定義一個(gè)特殊字符的集合NSCharacterSet set = [NSChara...
    奮拓達(dá)閱讀 942評(píng)論 0 0
  • iOS開發(fā)技巧 一、調(diào)用代碼使APP進(jìn)入后臺(tái),達(dá)到點(diǎn)擊Home鍵的效果。(私有API) suspend的英文意思有...
    WuZhuoXuan閱讀 4,949評(píng)論 2 21
  • 晚上下班回來經(jīng)常盯著手機(jī)屏幕發(fā)呆,就想寫點(diǎn)什么,打發(fā)下漫長(zhǎng)的夜晚,并記錄下工作中的點(diǎn)點(diǎn)滴滴。 1.tablevie...
    so_what閱讀 1,915評(píng)論 3 5

友情鏈接更多精彩內(nèi)容