今日かえるときに、ビルの上の看板が目についたのでそれを作ってみる。と言うことで、蛍光灯をピカピカさせて縦の線が流れるよう見せている掲示板を模したiPhoneアプリのサンプルコードです。
サンプルを動かすとこんな感じです
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property int counter;
@property NSString *lastLightName;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
}
– (void)viewDidAppear:(BOOL)animated
{
self.view.backgroundColor = [UIColor darkGrayColor];
[self createSignboard];
}
– (void)createSignboard
{
UILabel *label = [[UILabel alloc] init];
// label.text = @”Singboard!”;
// label.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:50];
NSMutableAttributedString *atext = [[NSMutableAttributedString alloc] initWithString:@”Signboard!”];
[atext addAttributes:@{
NSFontAttributeName : [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:80],
NSStrokeWidthAttributeName : @-4,
NSStrokeColorAttributeName : [UIColor greenColor],
} range:NSMakeRange(0, atext.length)];
label.attributedText = atext;
[label sizeToFit];
label.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
[self.view addSubview:label];
int lightNumber = 40;
for (int i=0; i<lightNumber; i++) {
float x = i * CGRectGetMaxX(self.view.bounds) / lightNumber;
float y = 0;
CALayer *light = [CALayer layer];
light.name = [NSString stringWithFormat:@”light %d”, i+1];
light.frame = CGRectMake(x, y, CGRectGetMaxX(self.view.bounds) / lightNumber, CGRectGetMaxY(self.view.bounds));
light.borderWidth = 1;
light.borderColor = [UIColor grayColor].CGColor;
[self.view.layer insertSublayer:light below:label.layer];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.counter == 0) {
self.counter = 1;
self.lastLightName = @”light 40″;
[NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(light:) userInfo:nil repeats:YES];
}
}
– (void)turnOnFirstLight
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name == %@”, @”light 1″];
CALayer *light = [self.view.layer.sublayers filteredArrayUsingPredicate:predicate][0];
light.backgroundColor = [UIColor whiteColor].CGColor;
}
– (void)light:(NSTimer*)sender
{
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
if (self.counter % 4 == 1) {
[self turnOnFirstLight];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name contains %@”, @”light”];
__block NSMutableArray *currentLightNames = [[NSMutableArray alloc] init];
[[self.view.layer.sublayers filteredArrayUsingPredicate:predicate] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CALayer *light = (CALayer*)obj;
if(light.backgroundColor == [UIColor whiteColor].CGColor) {
int lastNumber = [[self.lastLightName componentsSeparatedByString:@” “][1] intValue] – 1;
int number = [[light.name componentsSeparatedByString:@” “][1] intValue];
if (number <= lastNumber) {
light.backgroundColor = [UIColor clearColor].CGColor;
[currentLightNames addObject:light.name];
} else {
self.lastLightName = [NSString stringWithFormat:@”light %d”, lastNumber];
light.name = [NSString stringWithFormat:@”stop %d”, lastNumber];
if (lastNumber < 1) {
[sender invalidate];
}
}
}
}];
for (NSString *s in currentLightNames) {
int next = [[s componentsSeparatedByString:@” “][1] intValue] + 1;
NSString *param = [NSString stringWithFormat:@”light %d”, next];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name == %@”, param];
CALayer *light = [self.view.layer.sublayers filteredArrayUsingPredicate:predicate][0];
light.backgroundColor = [UIColor whiteColor].CGColor;
}
[CATransaction commit];
self.counter++;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end