1. 程式人生 > >理解 CALayer ContentsCenter 屬性

理解 CALayer ContentsCenter 屬性

property @property load nts spec asp ios 分享圖片 atomic


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

效果:
技術分享圖片

理解 CALayer ContentsCenter 屬性