//
// ViewController.m
// CATrancation
//
// Created by apple on 17/8/8.
// Copyright © 2017年 Wang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIView *changeView;
@property (nonatomic, strong) CALayer *layer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.changeView.hidden = YES;
CALayer *layer = [CALayer layer];
layer.position = CGPointMake(self.view.frame.size.width/2, 50);
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.backgroundColor = [UIColor lightGrayColor].CGColor;
[self.view.layer addSublayer:layer];
self.layer = layer;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//get the touch point
CGPoint point = [[touches anyObject] locationInView:self.view];
NSLog(@"钱self.layer = %@ self.layer.presentationlayer = %@",NSStringFromCGRect(self.layer.frame),NSStringFromCGRect(self.layer.presentationLayer.frame));
//check if we've tapped the moving layer
if ([self.layer.presentationLayer hitTest:point]) {
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
} else {
//otherwise (slowly) move the layer to new position
[CATransaction begin];
[CATransaction setAnimationDuration:4.0];
[CATransaction setCompletionBlock:^{
NSLog(@"内self.layer = %@ self.layer.presentationlayer = %@",NSStringFromCGRect(self.layer.frame),NSStringFromCGRect(self.layer.presentationLayer.frame));
}];
self.layer.position = point;
[CATransaction commit];
}
NSLog(@"后self.layer = %@ self.layer.presentationlayer = %@",NSStringFromCGRect(self.layer.frame),NSStringFromCGRect(self.layer.presentationLayer.frame));
}
- (IBAction)changeColor:(id)sender {
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.layer.actions = @{@"backgroundColor":transition};
self.layer.backgroundColor = [self randomColor].CGColor;
}
- (UIColor *)randomColor{
CGFloat R = arc4random_uniform(256.f)/255.f;
CGFloat G = arc4random_uniform(256.f)/255.f;
CGFloat B = arc4random_uniform(256.f)/255.f;
return [UIColor colorWithRed:R green:G blue:B alpha:1.f];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
动画过程的layer,添加点击效果
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 啊,开始填坑。。。主要是想说CALayer的模型层(modelLayer)和展示层(presentationLay...
- 1、添加定时器以及图片 2、初始化定时器以及图片 3、添加红包button,添加动画 4、在移动中的 view需要...
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...