#import "ViewAnimationEx.h"
#define BTN_MOVE 0
//ViewAnimatonExの実装
@implementation ViewAnimationEx
//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:text forState:UIControlStateNormal];
[button setFrame:rect];
[button setTag:tag];
return button;
}
//イメージビューの生成
- (UIImageView*)makeImageView:(CGRect)rect image:(UIImage*)image {
UIImageView* imageView=[[[UIImageView alloc] init] autorelease];
[imageView setImage:image];
[imageView setFrame:rect];
return imageView;
}
//初期化
- (void)viewDidLoad {
[super viewDidLoad];
//イメージビューの生成
_imageView=[self makeImageView:CGRectMake(10,10,300,300)
image:[UIImage imageNamed:@"sample.jpg"]];
[self.view addSubview:_imageView];
//ボタンの生成
UIButton* button=[self makeButton:CGRectMake(60,340,200,40)
text:@"UIViewアニメーション" tag:BTN_MOVE];
[button addTarget:self action:@selector(clickButton:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//アニメーションINDEXの初期化
_animeIdx=1;
}
//メモリ解放
- (void)dealloc {
[_imageView release];
[super dealloc];
}
//ボタンクリック時に呼ばれる
- (IBAction)clickButton:(UIButton*)sender {
if ([sender tag]==BTN_MOVE) {
//アニメーション前の位置・回転角度・透明度
if (_animeIdx==1) {
_imageView.transform=CGAffineTransformMakeScale(0.5f,0.5f);
_imageView.alpha=0.8f;
} else if (_animeIdx==2) {
CGAffineTransform trans=CGAffineTransformMakeRotation(180*(M_PI/180));
_imageView.transform=CGAffineTransformScale(trans,0.5f,0.5f);
_imageView.alpha=0.8f;
} else if (_animeIdx==3) {
_imageView.transform=CGAffineTransformMakeTranslation(0,400);
_imageView.alpha=0.8f;
} else if (_animeIdx==4) {
_imageView.frame=CGRectMake(60,340,200,40);
}
//UIViewアニメーションの設定開始
[UIView beginAnimations:@"anime0" context:NULL];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationRepeatAutoreverses:NO];
//UIViewアニメーションのデリゲート
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:
@selector(someAnimationWillStart:)];
[UIView setAnimationDidStopSelector:
@selector(someAnimationDidStop:finished:context:)];
//アニメーション後の位置・回転角度・透明度
if (_animeIdx==1 || _animeIdx==2 || _animeIdx==3) {
_imageView.transform=CGAffineTransformIdentity;
_imageView.alpha=1.0f;
} else if (_animeIdx==4) {
_imageView.frame=CGRectMake(10,10,300,300);
}
//UIViewアニメーションの実行
[UIView commitAnimations];
//アニメーションINDEXの遷移
_animeIdx++;
if (_animeIdx>4) _animeIdx=1;
}
}
//アニメーション開始時に呼ばれる
- (void)someAnimationWillStart:(id)sender {
NSLog(@"アニメ−ション開始");
}
//アニメーション停止時に呼ばれる
- (void)someAnimationDidStop:(NSString*)animationID
finished:(BOOL)finished context:(void*)context {
NSLog(@"アニメ−ション停止");
}
//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)orientation {
return YES;
}
@end |