matsuda
12/28/2011 - 1:34 AM

iPhone拡大縮小アニメーション

iPhone拡大縮小アニメーション

/*
 ScalableViewController.h
 */
@interface ScalableViewController : UIViewController
@property (nonatomic, retain) UIView *scaledView;
@end


/*
 ScalableViewController.h
 */
#define TIME_FOR_SHRINKING 0.31f // Has to be different from SPEED_OF_EXPANDING and has to end in 'f'
#define TIME_FOR_EXPANDING 0.30f // Has to be different from SPEED_OF_SHRINKING and has to end in 'f'
#define SCALED_DOWN_AMOUNT 0.01  // For example, 0.01 is one hundredth of the normal size

@implementation ScalableViewController

@synthesize scaledView = _scaledView;

- (void)removeScaledView
{
    if (_scaledView) {
        [_scaledView removeFromSuperview];
        [_scaledView release], _scaledView = nil;
    }
}

- (void)shrinkThumbView
{
    self.view.userInteractionEnabled = NO;

    UIView *thumbView = ...;
    CGPoint offset = ...;

    UIImageView *scaledView = (UIImageView *)self.scaledView;

    CGRect sf = CGRectMake(offset.x, offset.y, thumbView.frame.size.width, thumbView.frame.size.height);
    [UIView animateWithDuration:TIME_FOR_SHRINKING
                          delay:0.0
                        options:UIViewAnimationCurveEaseIn
                     animations:^{
                         scaledView.frame = sf;
                     }
                     completion:^(BOOL finished) {
                         [self removeScaledView];
                         self.view.userInteractionEnabled = YES;
                     }];
}

- (void)expandThumbView
{
    self.view.userInteractionEnabled = NO;

    UIView *thumbView = ...;
    CGPoint offset = ...;

    CGRect f = self.contentView.frame;
    UIImageView *scaledView = [[[UIImageView alloc] initWithFrame:CGRectMake(offset.x, offset.y, thumbView.frame.size.width, thumbView.frame.size.height)] autorelease];

    UITapGestureRecognizer *gesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureOnScaledView:)] autorelease];
    scaledView.userInteractionEnabled = YES;
    scaledView.exclusiveTouch = YES;
    [scaledView addGestureRecognizer:gesture];

    [self.view addSubview:scaledView];
    self.scaledView = scaledView;

    CGRect sf = CGRectMake(0, 0, f.size.width, f.size.height);
    [UIView animateWithDuration:TIME_FOR_EXPANDING
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         scaledView.frame = sf;
                     }
                     completion:^(BOOL finished) {
                         self.view.userInteractionEnabled = YES;
                     }];
}

- (void)handleTapGestureOnScaledView:(UITapGestureRecognizer *)gestureRecognizer
{
    [self shrinkThumbView];
}

- (void)thumbsViewCell:(UITableViewCell *)cell didSelectThumb:(id)thumb
{
    if (_scaledView) {
        [self shrinkThumbView];
    } else {
        [self expandThumbView];
    }
}

@end