iOS 常用到的知识点(三)

iOS 常用到的知识点(一)
iOS 常用到的知识点(二)
iOS 常用到的知识点(三)

1. navigationBar 的透明问题

想要 navigationBar 透明,按钮和标题都在可以使用以下方法:

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

处理去掉navigationBar下面的横线去

self.navigationController.navigationBar.shadowImage = [UIImage new];

2. 全局设置 navigationBar 标题的样式和 barItem 的标题样式

[[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]; 

3. 侧滑手势返回

在开发过程中在自定义了返回按钮,或者某些 webView、tableView 等页面,侧滑返回手势失效,这时候就需要我们来进行设置一下了,可以在基类里面添加如下代码:

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

// 记得写在 viewDidAppear 里面
- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

使用一个全屏滑动返回手势第三方库:FDFullscreenPopGesture
只需要加入项目中即可,不需要一行代码即可实现。
[FDFullscreenPopGesture](https://github.com/forkingdog/FDFullscreenPopGesture

4.给 webView 添加头视图

webView 是一个复合视图,里面包含有一个 scrollView,scrollView 里面是一个 UIWebBrowserView(负责显示 WebView 的内容)

// 拿到 webView 的 webBrowserView 
UIView *webBrowserView = self.webView.scrollView.subviews[0];

self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth*2/3.0)];
[_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];

// 把 backHeadImageView 插入到 webView 的 scrollView 下面
[self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView];

CGRect frame = self.webBrowserView.frame;
frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame);

// 更改 webBrowserView 的 frame 向下移 backHeadImageView 的高度,使其可见
self.webBrowserView.frame = frame;

5. 模态跳转的动画设置

设置模态跳转的动画,系统提供了四种可供选择。

DetailViewController *detailVC = [[DetailViewController alloc] init];

// UIModalTransitionStyleFlipHorizontal 翻转
// UIModalTransitionStyleCoverVertical 底部滑出
// UIModalTransitionStyleCrossDissolve 渐显
//UIModalTransitionStylePartialCurl 翻页
detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:detailVC animated:YES completion:nil];

6 图片处理只拿到图片的一部分

UIImage *image = [UIImage imageNamed:filename];
CGImageRef imageRef = image.CGImage;

// 这里的宽高是相对于图片的真实大小
// 比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);

CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];

7. 给 UIView 设置图片

UIImage *image = [UIImage imageNamed:@"playing"];
_layerView.layer.contents = (__bridge id)image.CGImage;

//同样可以设置显示的图片范围
//不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,width,height
_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);

8. 给 TableView 或者 CollectionView 的 cell 添加简单动画

只要在 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) {

    }];
}

摘自知识小集10 个 iOS 开发实用小技巧

猜你喜欢

转载自blog.csdn.net/weixin_34090643/article/details/87040323