海に日の出を見に行ったら残念ながら雲がかかってみれなかった。なので、雲を消して日の出を見るようなiPhoneアプリのサンプルを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (strong, nonatomic) NSMutableArray *cloudArr;
@property (weak, nonatomic) CALayer *sun;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.layer.backgroundColor = [UIColor darkGrayColor].CGColor;
[self createSun];
[self createSea];
[self createCloud];
}
– (void)createSea
{
CALayer *sea = [CALayer layer];
float y = CGRectGetMaxY(self.view.frame);
sea.frame = CGRectMake(0, y- 150, 320, 150);
sea.backgroundColor = [UIColor colorWithHue:25.0/36.0 saturation:0.8 brightness:1.0 alpha:1.0].CGColor;
[self.view.layer addSublayer:sea];
}
– (void)createSun
{
CALayer *sun = [CALayer layer];
sun.frame = CGRectMake(0, 0, 160, 160);
sun.position = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMaxY(self.view.frame)-100);
sun.backgroundColor = [UIColor colorWithHue:1.4/36.0 saturation:0.80 brightness:0.95 alpha:1.0].CGColor;
sun.cornerRadius = 80;
[self.view.layer addSublayer:sun];
self.sun = sun;
}
– (void)createCloud
{
self.cloudArr = [[NSMutableArray alloc] init];
float points[] = {
50, 50,
40, 270,
120, 250,
0, 300,
100, 300,
200, 300,
180, 400,
};
for (int i=0; i<sizeof(points)/(2.0 * sizeof(float)); i++) {
UIView *cloud = [[UIView alloc] initWithFrame:CGRectMake(points[2*i], points[2*i+1], 80*1.618, 80)];
[self.view addSubview:cloud];
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@”position.x”];
move.duration = 2.0 + (arc4random()%10) *0.2;
move.timeOffset = (arc4random()%10) *0.3;
move.repeatCount = 100;
move.autoreverses = YES;
for (int i=0; i<3; i++) {
CALayer *bar = [CALayer layer];
bar.frame = CGRectMake(i * 30, i * 20, cloud.frame.size.width–60, cloud.frame.size.height/3.0);
bar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9].CGColor;
bar.cornerRadius = cloud.frame.size.height/6.0;
[cloud.layer addSublayer:bar];
move.fromValue = @(i * 30);
move.toValue = @(i * 30 + 50);
[bar addAnimation:move forKey:nil];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[cloud addGestureRecognizer:tap];
[self.cloudArr addObject:cloud];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
gr.view.alpha = 0;
} completion:^(BOOL finished) {
[gr.view removeFromSuperview];
}];
[self.cloudArr removeObject:gr.view];
[self checkSunRise];
}
– (void)checkSunRise
{
if (self.cloudArr.count == 0) {
CABasicAnimation *rise = [CABasicAnimation animationWithKeyPath:@”position.y”];
rise.fromValue = @(self.sun.position.y);
rise.toValue = @(160);
rise.duration = 3.0;
self.sun.position = CGPointMake(self.sun.position.x, 160);
[self.sun addAnimation:rise forKey:nil];
[UIView animateWithDuration:0.3f animations:^{
self.view.layer.backgroundColor = [UIColor redColor].CGColor;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0f animations:^{
self.view.layer.backgroundColor = [UIColor colorWithHue:25.0/36.0 saturation:0.1 brightness:1.0 alpha:1.0].CGColor;
}];
}];
}
}
@end