Core Animation實戰二(寄宿圖)

ZY_FlyWay發表於2017-10-30

CALayer直接設定寄宿圖

//
//  LayerContentViewController.m
//  LayerStudyDemo
//
//  Created by apple on 2017/9/19.
//  Copyright © 2017年 ZY. All rights reserved.
//

#import "LayerContentViewController.h"

@interface LayerContentViewController ()

@end

@implementation LayerContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self contentImageDemo];
}

//寄宿圖
-(void)contentImageDemo{
    
    UIImage * image =  [UIImage imageNamed:@"timg.png"];
    
    CALayer   *  imageLayer = [CALayer layer];
    imageLayer.frame = CGRectMake(50, 200, 300, 300);
   
    //contents 圖層的寄宿圖 是一個CGImageRef型別,它是一個指向CGImage結構的指標
    imageLayer.contents = (__bridge id _Nullable)(image.CGImage);
   
    //contentsGravity的目的是為了決定內容在圖層的邊界中怎麼對齊,我們將使用kCAGravityResizeAspect,它的效果等同於UIViewContentModeScaleAspectFit, 同時它還能在圖層中等比例拉伸以適應圖層的邊界。
    imageLayer.contentsGravity = kCAGravityResizeAspect;
   
   //contentsScale屬性定義了寄宿圖的畫素尺寸和檢視大小的比例,預設情況下它是一個值為1.0的浮點數。
   //contentsScale的目的並不是那麼明顯。它並不是總會對螢幕上的寄宿圖有影響。如果你嘗試對我們的例子設定不同的值,你就會發現根本沒任何影響。因為contents由於設定了contentsGravity屬性,所以它已經被拉伸以適應圖層的邊界。
    imageLayer.contentsScale= 5.f;
   
   //我們裁切圖片的時候肯定用過這個屬性,是否繪製超出吧邊界的檢視
    imageLayer.masksToBounds = YES;
   
   //這個不用介紹了吧
    imageLayer.cornerRadius = 5.0f;
   
   //這個屬性需要介紹下,我們以前見過美工給圖在一張圖上,我們應該怎麼用呢。用這個屬性我們就可以切出我們要的那一部分了。
    imageLayer.contentsRect = CGRectMake(0, 0, 0.5, 0.5);
   
   //contentsCenter其實是一個CGRect,它定義了一個固定的邊框和一個在圖層上可拉伸的區域。 改變contentsCenter的值並不會影響到寄宿圖的顯示,除非這個圖層的大小改變了,你才看得到效果。
    imageLayer.contentsCenter = CGRectMake(0, 0, 0.1, 0.1);
    [self.view.layer addSublayer:imageLayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


Core Graphics繪製寄宿圖

     CALayer有一個可選的delegate屬性,實現了CALayerDelegate協議,當CALayer需要一個內容特定的資訊時,就會從協議中請求。CALayerDelegate是一個非正式協議,其實就是說沒有CALayerDelegate @protocol可以讓你在類裡面引用啦。你只需要呼叫你想呼叫的方法,CALayer會幫你做剩下的。(delegate屬性被宣告為id型別,所有的代理方法都是可選的)。

    當需要被重繪時,CALayer會請求它的代理給他一個寄宿圖來顯示。它通過呼叫下面這個方法做到的:

(void)displayLayer:(CALayerCALayer *)layer;

    趁著這個機會,如果代理想直接設定contents屬性的話,它就可以這麼做,不然沒有別的方法可以呼叫了。如果代理不實現-displayLayer:方法,CALayer就會轉而嘗試呼叫下面這個方法:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;

    在呼叫這個方法之前,CALayer建立了一個合適尺寸的空寄宿圖(尺寸由boundscontentsScale決定)和一個Core Graphics的繪製上下文環境,為繪製寄宿圖做準備,他作為ctx引數傳入。

//
//  CoustomDrawViewController.m
//  LayerStudyDemo
//
//  Created by apple on 2017/9/22.
//  Copyright © 2017年 ZY. All rights reserved.
//

#import "CoustomDrawViewController.h"

@interface CoustomDrawViewController ()<CALayerDelegate>

@end

@implementation CoustomDrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.layer.delegate = self;
    [self.view.layer display];
    
}

////layer 繪圖
//-(void)customDrawingDemo{
//
//    CALayer * customLayer = [CALayer layer];
//    customLayer.frame = CGRectMake((SCREEN_WIDTH-100)/2, (SCREEN_HEIGHT-100-64)/2, 100.0f, 100.0f);
//    customLayer.backgroundColor = [UIColor yellowColor].CGColor;
//    customLayer.delegate = self;
//    customLayer.contentsScale = [UIScreen mainScreen].scale; //add layer to our view
//    [self.view.layer addSublayer:customLayer];
//    [customLayer display];
//}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextSetLineWidth(ctx, 10.0f);
    CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
    CGContextStrokeEllipseInRect(ctx, layer.bounds);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end




相關文章