2013年4月28日日曜日

UIImageViewのアニメーション

UIImageViewをアニメーションするテストクラス

実際にはアニメーションの時間が長いと複数見えるし、アニメーション途中で終了させる場合にはビクつく問題がある。
でもアニメーションの時間が短いとそんなには目立たないかな。

※iOS6のARC前提
※閉じるアニメーションの終了のBlockで強引にremoveFromSuperview※しているが
 一応アニメーションの完了まではViewの実態は保持される
 (実際に利用側でインスタンスにnilを代入しているが落ちない)

<hファイル>

#import <UIKit/UIKit.h>

@interface AnimationImageView : UIImageView

- (void)setEndFrame:(CGRect)rect;
- (void)setStartFrame:(CGRect)rect;
- (void)setTime:(float)time;
- (void)OpenAnimation;
- (void)CloseAnimation;

@end



<mファイル>
#import "AnimationImageView.h"

@implementation AnimationImageView
{
    float animeTime;
    CGRect startFrame;
    CGRect endFrame;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        animeTime = 0.0;
        startFrame = CGRectNull;
        endFrame = CGRectNull;
    }
    return self;
}

- (void)setEndFrame:(CGRect)rect
{
    NSLog(@"setEndFrame");
    endFrame = rect;
}
- (void)setStartFrame:(CGRect)rect
{
    NSLog(@"setStartFrame");
    startFrame = rect;
}
- (void)setTime:(float)time
{
    NSLog(@"setTime");
    animeTime = time;
}
- (void)OpenAnimation
{
    NSLog(@"OpenAnimation");

    //アニメーションなしの場合
    if (    (animeTime == 0.0)
        ||  (CGRectIsNull(startFrame))
        ||  (CGRectIsNull(endFrame))
        )
        return;
    
    self.frame = startFrame;
    self.alpha = 0.0;
    [UIView animateWithDuration:animeTime
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.frame = endFrame;
                         self.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Open End!");
                     }];
}
- (void)CloseAnimation
{
    NSLog(@"CloseAnimation");
    //アニメーションなしの場合
    if (    (animeTime == 0.0)
        ||  (CGRectIsNull(startFrame))
        ||  (CGRectIsNull(endFrame))
        )
    {
        //
    }
    else
    {
        [UIView animateWithDuration:animeTime
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             self.frame = startFrame;
                             self.alpha = 0.0;
                         }
                         completion:^(BOOL finished){
                             NSLog(@"Close end!");
                             [self removeFromSuperview];
                         }];
    }
}

@end

<使い方>
- (IBAction)pushBtnAnime:(id)sender
{
    if (animView == nil)
    {
        NSLog(@"pushBtnOpen");
        
        UIImage *img = [UIImage imageNamed:@"testImage.png"];
        animView = [[AnimationImageView alloc] initWithImage:img];

        //iPad決めうちで右からセンターへアニメーション
        CGRect srect = CGRectMake(768,((1024-img.size.height)/2),
                                  img.size.width,img.size.height);
        CGRect erect = CGRectMake(((768-img.size.width)/2),((1024-img.size.height)/2),
                                  img.size.width,img.size.height);
        [animView setStartFrame:srect];
        [animView setEndFrame:erect];
        [animView setTime:0.5];
        [self.view addSubview:animView];
        [animView OpenAnimation];
    }
    else
    {
        [animView CloseAnimation];
        animView = nil;
    }
}

0 件のコメント:

コメントを投稿