代码改变世界

核心动画2

2015-10-16 05:23  真实16  阅读(104)  评论(0编辑  收藏  举报

//

//  ViewController.m

//  UI-NO-36-1 核心动画-2

//

//  Created by 容伟 on 15/9/15.

//  Copyright (c) 2015年 容伟. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController (){

    

    CALayer *myLayer;

    CGFloat width;

    CALayer *imageLayer;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self addLayer];

    [self addImageLayer];

 

 

}

 

- (void)addLayer {

    width = 100;

    

    myLayer = [[CALayer alloc] init];

    myLayer.backgroundColor = [UIColor whiteColor].CGColor;

    myLayer.shadowOpacity = 1.0;   //  阴影 默认的透明度是 0

    myLayer.shadowColor = [UIColor yellowColor].CGColor;

    myLayer.bounds = CGRectMake(0, 0, width, width);

    myLayer.position = self.view.center;

    myLayer.borderWidth = 2;

    myLayer.borderColor = [UIColor blackColor].CGColor;

    myLayer.cornerRadius = width/2;

    myLayer.shadowOffset = CGSizeMake(-1, -1);

//    myLayer.contents = (__bridge id)([UIImage imageNamed:@"12.jpg"].CGImage);

//    myLayer.masksToBounds = YES;

    

    

    [self.view.layer addSublayer:myLayer];

}

 

- (void)addImageLayer {

    imageLayer = [[CALayer alloc] init];

    imageLayer.bounds = CGRectMake(0, 0, width, width);

    imageLayer.position = self.view.center;

    imageLayer.cornerRadius = myLayer.cornerRadius;

    imageLayer.contents = (__bridge id)([UIImage imageNamed:@"12.jpg"].CGImage);

        imageLayer.masksToBounds = YES;

    imageLayer.borderWidth = 2;

    imageLayer.borderColor = [UIColor whiteColor].CGColor;

    [self.view.layer addSublayer:imageLayer];

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    [self changeStateWith:[touch locationInView:self.view]];

}

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    [self changeStateWith:[touch locationInView:self.view]];

}

 

- (void)changeStateWith:(CGPoint)position {

    myLayer.position = position;

    width = width == 100 ? 300:100;

    myLayer.bounds = CGRectMake(0, 0, width, width);

    myLayer.cornerRadius = width/2;

    

    imageLayer.position = position;

    imageLayer.bounds = CGRectMake(0, 0, width, width);

    imageLayer.cornerRadius = myLayer.cornerRadius;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end