//
// 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年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...