iOS常用技巧之UI篇

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

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

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的透明問(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)題都在可以使用以下方法:

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

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


Paste_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的偏移量來(lái)設(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:[UIFontboldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];
 
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFontboldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)}forState:UIControlStateNormal];
 
    [[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)畫(huà)來(lái)隱藏或顯示navigationBar:
在返回后將要出現(xiàn)的頁(yè)面實(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)用。但是在開(kāi)發(fā)過(guò)程中在自定義了返回按鈕,或者某些webView,tableView等頁(yè)面,側(cè)滑返回手勢(shì)失效,這時(shí)候就需要我們來(lái)進(jìn)行設(shè)置一下了,可以在基類里面協(xié)商如下代碼:


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

問(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ù),寫(xiě)的相當(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、導(dǎo)航控制器返回時(shí)陰影出現(xiàn)

11.gif

這個(gè)問(wèn)題其實(shí)也很常見(jiàn),可以看到,在Push的時(shí)候,有顏色層級(jí)錯(cuò)開(kāi),非常影響體驗(yàn),解決方式也比較簡(jiǎn)單,只需要設(shè)置Window的顏色就可以了

    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];

7、給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:[UIImageimageNamed:@"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的高度,使其可見(jiàn)

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

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

DetailViewController *detailVC = [[DetailViewController alloc]init];
    //UIModalTransitionStyleFlipHorizontal 翻轉(zhuǎn)
    //UIModalTransitionStyleCoverVertical 底部滑出
    //UIModalTransitionStyleCrossDissolve 漸顯
    //UIModalTransitionStylePartialCurl 翻頁(yè)
    detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentViewController:detailVC animated:YES completion:nil];

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

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

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

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

- (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放回原來(lái)的位置
    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 *)cellforItemAtIndexPath:(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) {
 
    }];
}

11、給UIView設(shè)置圖片(性能優(yōu)化)

//注意:這種方式讀取圖片會(huì)占內(nèi)存
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è)置顯示的圖片范圍
//不過(guò)此處略有不同,這里的四個(gè)值均為0-1之間;對(duì)應(yīng)的依然是寫(xiě)x,y,widt,height
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,629評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,258評(píng)論 7 249
  • 食材原材料 土豆2個(gè) 番茄2個(gè) 洋蔥1個(gè) 上海紅腸1根 卷心菜1顆 番茄醬(起司) 烹飪時(shí)間 4個(gè)小時(shí) 半鍋水(2...
    御宅胖貓閱讀 426評(píng)論 0 0
  • 想著,盼望著,今年的雪該來(lái)了吧。好吧,這么一點(diǎn)點(diǎn),還是雨夾雪,完全沒(méi)有期望中樣子的一星半點(diǎn)??蓺鉁亟K究還是降了,小...
    kwork1988閱讀 290評(píng)論 0 0

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