▼iPhoneプログラミングメモ▼
トランジション

トランジションを行うプログラムを作成する。

画像の準備
Resourcesに追加。

pic0.jpg


pic1.jpg


ソースコードの記述
TransitionEx.h
#import <UIKit/UIKit.h>

//TransitionExの宣言
@interface TransitionEx : UIViewController {
    int          _animeIdx;
    UIImageView* _imageView;
}
@end

TransitionEx.m
#import "TransitionEx.h"

//TransitionExの実装
@implementation TransitionEx

//イメージビューの生成
- (UIImageView*)makeImageView:(CGRect)rect idx:(int)idx {
    UIImageView* imageView=[[[UIImageView alloc] init] autorelease];
    imageView.image=[UIImage imageNamed:
        [NSString stringWithFormat:@"pic%d.jpg",idx]];
    imageView.frame=rect;
    imageView.contentMode=UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask=
        UIViewAutoresizingFlexibleWidth|
        UIViewAutoresizingFlexibleHeight|
        UIViewAutoresizingFlexibleLeftMargin|
        UIViewAutoresizingFlexibleRightMargin|
        UIViewAutoresizingFlexibleTopMargin|
        UIViewAutoresizingFlexibleBottomMargin;
    imageView.backgroundColor=[UIColor whiteColor];
    return imageView;
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //イメージビューの生成
    _imageView=[[self makeImageView:self.view.frame idx:0] retain];
    [self.view addSubview:_imageView];
    
    //アニメーションINDEXの初期化
    _animeIdx=1;
}

//メモリ解放
- (void)dealloc {
    [_imageView release];
    [super dealloc];
}

//タッチ終了時に呼ばれる
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    //ビューの生成(2)
    UIImageView* nextView=[[self makeImageView:
        _imageView.frame idx:_animeIdx%2] retain];

    //UIViewアニメーションの設定開始
    [UIView beginAnimations:@"transition" context:NULL];
    [UIView setAnimationDuration:3.0f];
        
    //トランジションアニメーションの設定
    UIViewAnimationTransition transition=0;
    if (_animeIdx==1) transition=UIViewAnimationTransitionFlipFromLeft;
    if (_animeIdx==2) transition=UIViewAnimationTransitionFlipFromRight;
    if (_animeIdx==3) transition=UIViewAnimationTransitionCurlUp;
    if (_animeIdx==4) transition=UIViewAnimationTransitionCurlDown;
    [UIView setAnimationTransition:transition
        forView:self.view cache:YES];
        
    //ビューの変更
    [_imageView removeFromSuperview];
    [_imageView release];
    _imageView=[nextView retain];
    [self.view addSubview:_imageView];
        
    //UIViewアニメーションの実行
    [UIView commitAnimations];
    
    //アニメーションINDEXの遷移
    _animeIdx++;
    if (_animeIdx>4) _animeIdx=1;
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end


−戻る−