Core AnimationでFront Rowみたいなアプリを作ってみた

公開日: : 最終更新日:2013/11/12 cocoa

記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。

http://farm4.static.flickr.com/3128/2407571099_d538aa6470.jpg

と言っても、ほとんどがチュートリアルのコード。キーボードの上下キーを押すと、カーソルがなめらかに上下移動します。例によって背景では3Dアニメーションしてます。

moveUpイベントでのカーソルアニメーションコードが非常にシンプル。素晴しい。


- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}

- (void)drawRect:(NSRect)rect {
// Drawing code here.
}

- (void)awakeFromNib
{
names=[[NSArray arrayWithObjects:@"Item1",@"Item2",
@"Item3",@"Item4",@"Item5",
nil]retain];
[self setupLayers];
}

-(void)setupLayers;
{
CGFloat width=400.0;
CGFloat height=35.0;
CGFloat spacing=20.0;
CGFloat fontSize=32.0;
CGFloat initialOffset=100.0;


[[self window]makeFirstResponder:self];


QCCompositionLayer *rootLayer;
rootLayer = [QCCompositionLayer compositionLayerWithFile:
[[NSBundle mainBundle]pathForResource:@"Background"
ofType:@"qtz"]];

[self setLayer:rootLayer];
[self setWantsLayer:YES];

menusLayer = [[CALayer layer]retain];
menusLayer.frame = rootLayer.frame;

menusLayer.layoutManager=[CAConstraintLayoutManager layoutManager];

[rootLayer addSublayer:menusLayer];

NSInteger i;
for (i=0;i<[names count];i++) {
NSString *name=[names objectAtIndex:i];
CATextLayer *menuItemLayer=[CATextLayer layer];
menuItemLayer.string=name;
menuItemLayer.font=@"Lucida-Grande";
menuItemLayer.fontSize=fontSize;
menuItemLayer.foregroundColor=CGColorCreateGenericRGB(1.0,1.0,1.0,1.0);

[menuItemLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMaxY
relativeTo:@"superlayer"
attribute:kCAConstraintMaxY
offset:-(i*height+spacing+initialOffset)]];

[menuItemLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]];

[menusLayer addSublayer:menuItemLayer];
}

[menusLayer layoutIfNeeded];

selectionLayer=[[CALayer layer]retain];
selectionLayer.bounds=CGRectMake(0.0,0.0,width,height);

selectionLayer.borderWidth=2.0;
selectionLayer.borderColor=CGColorCreateGenericRGB(1.0f,1.0f,1.0f,1.0f);
selectionLayer.cornerRadius = height/2;

CIFilter*filter=[CIFilter filterWithName:@"CIBloom"];
[filter setDefaults];
[filter setValue:[NSNumber numberWithFloat:5.0]forKey:@"inputRadius"];

[filter setName:@"pulseFilter"];

[selectionLayer setFilters:[NSArray arrayWithObject:filter]];

CABasicAnimation* pulseAnimation=[CABasicAnimation animation];

pulseAnimation.keyPath=@"filters.pulseFilter.inputIntensity";

pulseAnimation.fromValue=[NSNumber numberWithFloat:0.0];
pulseAnimation.toValue=[NSNumber numberWithFloat:1.5];

pulseAnimation.duration = 1.0;
pulseAnimation.repeatCount = 1e100f;
pulseAnimation.autoreverses = YES;

pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut];

[selectionLayer addAnimation:pulseAnimation forKey:@"pulseAnimation"];

[rootLayer addSublayer:selectionLayer];

[self changeSelectedIndex:0];
}

-(void)changeSelectedIndex:(NSInteger)theSelectedIndex
{
selectedIndex=theSelectedIndex;

if (selectedIndex == [names count]) selectedIndex=[names count]-1;
if (selectedIndex < 0) selectedIndex=0;

selectionLayer.position = [
(CALayer*)[[menusLayer sublayers]
objectAtIndex:selectedIndex] position];
};

-(void)moveUp:(id)sender
{
[self changeSelectedIndex:selectedIndex-1];
}

-(void)moveDown:(id)sender
{
[self changeSelectedIndex:selectedIndex+1];
}

-(void)dealloc
{
[menusLayer autorelease];
[selectionLayer autorelease];
[names autorelease];
[super dealloc];
}

関連記事

サンフランシスコのピア39にあるチャウダーズでクラムチャウダーを食す!

lolipop アップルの開発者向けイベント「WWDC2014」

ミスドのカルピスドーナツとカルピスポンデリングを食べてみた!

ミスドで期間限定のカルピスコラボ商品「カルピスドーナツ」と「カルピ

十三カレー計画で牛すじカレーネギのせを食す!(大阪・十三)

「iPhoneアプリ開発キャンプ@大阪」のランチで、十三カレー計画

大阪・難波の加寿屋 法善寺でかすうどんを食す。ランチタイムはおにぎり2個まで無料!

大阪・難波の加寿屋 法善寺 (かすうどん KASUYA)で、かす

ライブドアブログで運営していた「あきお商店」を「卵は世界である」に改名しました

少し前からライブドアブログで「あきお商店」というブログをやって

→もっと見る

PAGE TOP ↑