home > 投稿 > Objective-C : CALayer を MovieClip に
2009/05/01

Objective-C : CALayer を MovieClip に


慣れない CALayer を MovieClip っぽく使えるように適当にラッピングしてみた。

import するとこんな風に書けて、だいぶ気が楽になります。

- (void)viewDidLoad {
	MovieClip *stage	= [[MovieClip alloc] initWithView:self.view];
	MovieClip *mc		= [[MovieClip alloc] init];
	MovieClip *img		= [[MovieClip alloc] initWithImage:@"test.png"];
	
	[mc addChild: img];
	[stage addChild: mc];

	mc.x = (320 - img.width)/2;
	mc.y = (460 - img.height)/2;
	mc.alpha = 0.5f;
	mc.visible = true;
}

CALayer 自体は mc.layer で参照して、
mc.layer.position = CGPointMake(0,0);
とか直接触れます。


MovieClip.h

//
//  MovieClip.h
//
//  Created by bascule on 09/04/30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/NSObject.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface MovieClip : NSObject {
	float		x;
	float		y;
	float		width;
	float		height;
	float		alpha;
	
	float		offset_x;
	float		offset_y;
	
	BOOL		visible;
	
	UIView		*view;
	CALayer		*layer;
	NSString	*text;
	CGPoint		*position;
}

- (MovieClip*)initWithView:(UIView*)view;

- (void)setX:(float)_x;
- (float)x;

- (void)setY:(float)_y;
- (float)y;

- (void)setPosition:(CGPoint)_point;
- (CGPoint)position;

- (void)setWidth:(float)_width;
- (float)width;

- (void)setHeight:(float)_height;
- (float)height;

- (void)setAlpha:(float)_alpha;
- (float)alpha;

- (void)setVisible:(BOOL)_visible;
- (BOOL)visible;

- (void)setLayer:(CALayer*)_layer;
- (CALayer*)layer;

- (void)setView:(UIView*)_view;
- (UIView*)view;

- (void)addChild:(MovieClip*)mc;

@end


MovieClip.m

//
//  MovieClip.m
//
//  Created by bascule on 09/04/30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MovieClip.h"

@interface MovieClip (MovieClipPrivate)
- (void)set_pos;
- (void)set_rect;
@end

@implementation MovieClip

- (void)setPosition:(CGPoint)_point{
	x = _point.x;
	y = _point.y;
	layer.position	= CGPointMake(x + offset_x , y + offset_y );
}
- (CGPoint)position{
	//ここ間違えてる
	return layer.position;
}


- (MovieClip*)init
{
	layer		= [CALayer layer];
	width		= 0;
	height		= 0;
	x			= 0;
	y			= 0;
	offset_x	= 0;
	offset_y	= 0;
	visible		= true;
	alpha		= 1;
	
	[self set_pos];
	return self;
}


- (MovieClip*)initWithView:(UIView*)_view
{
	//UIImage	*img			= [UIImage imageNamed:img_str];
	view	= _view;
	layer	= view.layer;
	width	= layer.frame.size.width;
	height	= layer.frame.size.height;
	x		= self.x;
	y		= self.y;
	visible	= true;
	alpha	= 1;
	
	[self set_pos];
	[self set_rect];
	
	return self;
}

- (MovieClip*)initWithImage:(NSString*)img_str
{
	UIImage	*img	= [UIImage imageNamed:img_str];
	
	layer	= [CALayer layer];
	width	= img.size.width;
	height	= img.size.height;
	x		= 0;
	y		= 0;
	visible	= true;
	alpha	= 1;
	
	[self set_pos];
	[self set_rect];
	
	layer.contents	= (id)img.CGImage;
	//view			= img;
	
	return self;
}

- (void)set_rect{
	layer.frame		= CGRectMake(x,y,width,height);
	offset_x		= width/2;
	offset_y		= height/2;
}

- (void)set_pos{
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:0.0f] forKey:kCATransactionAnimationDuration];
	layer.position	= CGPointMake(x + offset_x , y + offset_y );
	[CATransaction commit];
}
- (void)set_alpha{
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:0.0f] forKey:kCATransactionAnimationDuration];
	
	if(visible){
		layer.opacity = alpha;
	}else{
		layer.opacity = 0;
	}
	
	[CATransaction commit];
}

- (void)setView:(UIView*)_view{
	view=_view;
	[self initWithView:view];
}

- (UIView*)view{
	return view;
}

- (void)setLayer:(CALayer*)_layer{
	layer=_layer;
}

- (CALayer*)layer{
	return layer;
}

- (void)setX:(float)_x{
	x = _x;
	[self set_pos];
}

- (float)x{
	x = layer.position.x - offset_x;
	return x;
}
- (void)setY:(float)_y{
	y = _y;
	[self set_pos];
}

- (float)y{
	y = layer.position.y - offset_y;
	return y;
}

- (void)setWidth:(float)_width{
	width = _width;
	[self set_rect];
}

- (float)width{
	return layer.frame.size.width;
}

- (void)setHeight:(float)_height{
	height = _height;
	[self set_rect];
}

- (float)height{
	return layer.frame.size.height;
}

- (void)setAlpha:(float)_alpha{
	alpha = _alpha;
	[self set_alpha];
}

- (float)alpha{
	float f;
	if(visible){
		f = layer.opacity;
	}else{
		f = alpha;
	}
	return f;
}

- (void)setVisible:(BOOL)_visible{
	visible = _visible;
	[self set_alpha];
}
- (BOOL)visible{
	return visible;
}

- (void)addChild:(MovieClip*)mc{
	[layer addSublayer:mc.layer];
}

@end