美文网首页
iOS小知识集合

iOS小知识集合

作者: muice | 来源:发表于2021-11-10 15:07 被阅读0次
    1. 局部圆角
        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;
    
    1. 给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:[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的高度,使其可见
    
    1. 设置模态跳转动画
        DetailViewController *detailVC = [[DetailViewController alloc]init];
        //UIModalTransitionStyleFlipHorizontal 翻转
        //UIModalTransitionStyleCoverVertical 底部滑出
        //UIModalTransitionStyleCrossDissolve 渐显
        //UIModalTransitionStylePartialCurl 翻页
        detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentViewController:detailVC animated:YES completion:nil];
    
    1. 给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);//啥意思?先看书吧。contentsRect
        _layerView.contentsGravity = kCAGravityResize;
    
    1. 给TableView或者CollectionView的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;
        }];
    }
    
    1. “别挤我”和“紧抱我”
      我们都知道,UIButton和UILabel,在布局时并不需要给它们设置宽高约束,因为他们可以根据自身内容决定大小(intrinsic size)。但是当两个label的intrinsic size加起来比superview还要大或者小的时候,那么label就可能会受到压缩或者拉伸。也就引出了“Content Compression Resistance Priority(别挤我)”和“Content Hugging Priority(紧抱我)”,即通过设置优先级来决定拉伸或者压缩哪个label。
      比如:两个label的size加起来比superview小,那么label1“Content Compression Resistance Priority”值比label2“Content Compression Resistance Priority”值大,那么就会压缩label2。

    相关文章

      网友评论

          本文标题:iOS小知识集合

          本文链接:https://www.haomeiwen.com/subject/iqvvnttx.html