iOS10個實用小技巧

在開發過程中我們總會遇到各種各樣的小問題,有些小問題並不是十分容易解決。在此我就總結一下,我在開發中遇到的各種小問題,以及我的解決方法。比較普遍的我就不再提了,這裏主要講一些你可能不知道的(當然,也有可能你都知道,大神就不必往下看了)

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

//記得寫在didAppear裏面
- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

  當然你也可以使用一個第三方庫,寫的相當棒。他對系統的側滑返回手勢進行拓展,不用從邊緣滑動,只要右滑即可返回。最重要的是,他只需要加入項目中即可,不需要一行代碼即可實現。附上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) {

    }];
}

如需轉載請註明出處,請尊重作者的勞動成果。



文/_南山憶(簡書作者)
原文鏈接:http://www.jianshu.com/p/a3156826c27c
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章