理解 CALayer ContentsCenter 属性


http://aaronzjp.cn/2016/12/01/iOS-CALayer/

这个属性和android 的 .9 文件类似,定义了图片的拉伸范围:例子中明显是四个角不拉伸,对于需要做背景,contentsCenter矩形范围为横竖拉伸的交叉区域

此处输入图片的描述

  • _layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
#import "CALayerContentsCenterViewController.h"
@interface CALayerContentsCenterViewController ()
@property (nonatomic,strong) UIView *layerView;
@end
@implementation CALayerContentsCenterViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  self.layerView = [[UIView alloc]init];
  _layerView.bounds = CGRectMake(0, 0, 300, 300);
  _layerView.center = self.view.center;
  [self.view addSubview:_layerView];
  
  UIImage *image = [UIImage imageNamed:@"Test.png"];
  _layerView.layer.contents = (__bridge id) image.CGImage;
  _layerView.layer.contentsGravity = kCAGravityResizeAspect;
  _layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
  
}
@end

效果:
此处输入图片的描述

posted @ 2018-07-20 20:25  buoge  阅读(436)  评论(0编辑  收藏  举报