iOS 核心動畫Core Animation

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>//這個包也可以不用導入

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

{

    UIView *view2;

}

//@property (nonatomic,strong)UIView *containerView;

@end


@implementation ViewController

-(CALayer *)faceWithTransform:(CATransform3D)transform{

    CALayer *face=[CALayer layer];

    face.frame=CGRectMake(-50,-50, 100, 100);

    

    CGFloat red = (rand()/(double)INT_MAX);

    CGFloat green=(rand()/(double)INT_MAX);

    CGFloat blue=(rand()/(double)INT_MAX);

    face.backgroundColor=[UIColor colorWithRed:red green:green blue:blue alpha:1].CGColor;

    face.transform=transform;

    return face;

    

}

-(CALayer *)cubeWithTransform:(CATransform3D)transform{

    CATransformLayer *cube =[CATransformLayer layer];

    //添加立方體的一個面

    CATransform3D ct = CATransform3DMakeTranslation(0, 0, 50);//參數1:x軸偏移位置,往下爲正。參數2:y軸偏移位置,往右爲正。參數3:z軸偏移位置,往外爲正數。

    

    [cube addSublayer:[self faceWithTransform:ct]];

    //add cube face 2

    ct =  CATransform3DMakeTranslation(50, 0, 0);

    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);

    [cube addSublayer:[self faceWithTransform:ct]];

    //add cube face 3

    ct = CATransform3DMakeTranslation(0, -50, 0);

    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);

    [cube addSublayer:[self faceWithTransform:ct]];

    //add cube face 4

    ct = CATransform3DMakeTranslation(0, 50, 0);

    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);

    [cube addSublayer:[self faceWithTransform:ct]];

    //add cube face 5

    ct = CATransform3DMakeTranslation(-50, 0, 0);

    ct = CATransform3DRotate(ct, -M_PI_2, 0, 1, 0);

    [cube addSublayer:[self faceWithTransform:ct]];

    //add cube face 6

    ct= CATransform3DMakeTranslation(0, 0, -50);

    ct=CATransform3DRotate(ct, M_PI, 0, 1, 0);

    [cube addSublayer:[self faceWithTransform:ct]];

    

    CGSize  containerSize=view2.bounds.size;

    cube.position=CGPointMake(containerSize.width/2.0, containerSize.height/2.0);

    cube.transform = transform;

    return  cube;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //CAShapeLayer屬性是CGPathRef類型,但是我們用UIBezierPath幫助類創建了圖層路徑,這樣我們就不用考慮釋放CGPath了。

    UIBezierPath *path=[[UIBezierPath alloc]init];

//    [path moveToPoint:CGPointMake(175, 100)];

    [path addArcWithCenter:CGPointMake(150, 100) radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES ];

    [path moveToPoint:CGPointMake(150, 125)];

    [path addLineToPoint:CGPointMake(150, 175)];

    [path addLineToPoint:CGPointMake(125, 225)];//左腳

    [path moveToPoint:CGPointMake(150, 175)];

    [path addLineToPoint:CGPointMake(175, 225)];//右腳

    [path moveToPoint:CGPointMake(100, 150)];

    [path addLineToPoint:CGPointMake(200, 150)];//胳膊


    //create shape layer

    CAShapeLayer *shapeLayer=[CAShapeLayer layer];

    shapeLayer.strokeColor=[UIColor redColor].CGColor;

    shapeLayer.fillColor=[UIColor clearColor].CGColor;

    shapeLayer.lineWidth=5;

    shapeLayer.lineJoin=kCALineJoinRound;

    shapeLayer.lineCap=kCALineCapRound;

    shapeLayer.path=path.CGPath;

    [self.view.layer addSublayer:shapeLayer];

    

    CGRect rect = CGRectMake(50, 200, 100, 100);

    CGSize radii=CGSizeMake(20, 20);

    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerTopLeft |UIRectCornerBottomRight;

    UIBezierPath *path2=[UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];

     CAShapeLayer *shapeLayer2=[CAShapeLayer layer];

    shapeLayer2.borderWidth=2;

    shapeLayer2.strokeColor=[UIColor redColor].CGColor;

    shapeLayer2.fillColor=[UIColor clearColor].CGColor;

    shapeLayer2.path=path2.CGPath;

    [self.view.layer addSublayer:shapeLayer2];

    

    

    //create a text layer

    UIView *view=[[UIView alloc]init];

    view.backgroundColor=[UIColor yellowColor];

    view.frame=CGRectMake(10, 400, 100, 100);

    [self.view addSubview:view];

    CATextLayer *textlayer=[CATextLayer layer];

    textlayer.frame=view.bounds;

    [view.layer addSublayer:textlayer];

    

    textlayer.foregroundColor=[UIColor blackColor].CGColor;

    textlayer.alignmentMode=kCAAlignmentJustified;

    textlayer.wrapped=YES;

    textlayer.contentsScale=[UIScreen mainScreen].scale;

    

    UIFont *font=[UIFont systemFontOfSize:15];

    

    CFStringRef fontName=(__bridge CFStringRef)(font.fontName);

    CGFontRef fontRef = CGFontCreateWithFontName(fontName);

    textlayer.font=fontRef;

    textlayer.fontSize=font.pointSize;

    CGFontRelease(fontRef);

    

    NSString *str=@"asjfh是發生地啊快減肥大法肯定會噶好快回歸哈哈個很好jjjhg";

    textlayer.string = str;

    

    

    //CATransformLayer

    //CATransformLayer不同於普通的CALayer,因爲它不能顯示它自己的內容。只有當存在了一個能作用於子圖層的變換它才真正存在。CATransformLayer並不平面化它的子圖層,所以它能夠用於構造一個層級的3D結構。

    

    view2=[[UIView alloc]init];

    view2.frame=CGRectMake(150, 300, 200, 200);

    view2.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:view2];

    

    CATransform3D pt = CATransform3DIdentity;

    pt.m34 = -1.0/500.0;//m34= -1/D,  默認值是0,所謂的D,是eye(觀察者)到投射面的距離。

    view2.layer.sublayerTransform=pt;

    

    CATransform3D c1t = CATransform3DIdentity;

    c1t=CATransform3DTranslate(c1t, -150, 0, 0);

    CALayer *cube1=[self cubeWithTransform:c1t];

    [view2.layer addSublayer:cube1];

    

    CATransform3D c2t = CATransform3DIdentity;

    c2t = CATransform3DTranslate(c2t, 30, 0, 0);//參數1: 用於本身實現疊加效果。參數2:用於x軸偏移位置,往下爲正數。參數3:用於z軸偏移位置,往外是正數,值越大這個圖層就越往外(接近屏幕)

    c2t = CATransform3DRotate(c2t, -M_PI_4, 1, 0, 0);//參數1:用於本身實現疊加效果。參數2:旋轉的弧度。參數3:x軸方向旋轉。值範圍-1-1之間。參數4:y軸方向旋轉。參數5:z軸方向旋轉.

    c2t = CATransform3DRotate(c2t, -M_PI_4, 0, 1, 0);//效果可疊加

    CALayer *cube2 = [self cubeWithTransform:c2t];

    [view2.layer addSublayer:cube2];

    

    //CAGradientLayer

    //CAGradientLayer是用來生成兩種或更多顏色平滑漸變的。CAGradientLayer的真正好處在於繪製使用了硬件加速。

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame=view2.bounds;

    [view2.layer addSublayer:gradientLayer];

    gradientLayer.colors=@[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor blueColor].CGColor];

    gradientLayer.startPoint=CGPointMake(0, 0);

    gradientLayer.endPoint=CGPointMake(1, 1);

    

    //多重漸變

    //locations數組並不是強制要求的,但是如果你給它賦值了就一定要確保locations的數組大小和colors數組大小相同。

    CAGradientLayer *gradientLayer2=[CAGradientLayer layer];

    gradientLayer2.frame = self.view.bounds;

    [self.view.layer addSublayer:gradientLayer2];

    gradientLayer2.colors=@[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor];

    gradientLayer2.locations=@[@0.0,@0.25,@0.5];//表示紅色從0.0%(該處爲純紅)開始漸變直到25%(在該處顏色爲純綠),然後從%25到%50進行漸變(50%該處顏色爲純黃)

    gradientLayer2.startPoint=CGPointMake(0, 0);

    gradientLayer2.endPoint=CGPointMake(1, 1);

    

    

    //AVPlayerLayer是用來在iOS上播放視頻的。

    UIView *view3=[[UIView alloc]init];

    view3.frame=CGRectMake(0, 0, 200, 200);

    view3.backgroundColor=[UIColor redColor];

    [self.view addSubview:view3];

    NSURL *url=[[NSBundle mainBundle]URLForResource:@"video" withExtension:@"mp4"];

    AVPlayer *player = [AVPlayer playerWithURL:url];

    AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];

    playerLayer.frame=view3.bounds;

    [view3.layer addSublayer:playerLayer];

    CATransform3D transform=CATransform3DIdentity;

    transform.m34 = -1.0/500.0;

    transform =CATransform3DRotate(transform, M_PI_4, 1, 1,0);

    playerLayer.transform=transform;

    playerLayer.masksToBounds =YES;

    playerLayer.cornerRadius=20.0;

    playerLayer.borderColor=[UIColor blueColor].CGColor;

    playerLayer.borderWidth=5.0;

    

    

    [player play];

    

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章