0

アプリケーション内で UIImageView をアニメーション化し、その中の UIImage を周期的に変更して、アニメーション化された写真のスライドショーのように見せる必要があります。

現在、NSTimer を使用して、毎秒NUIImage の変更とアニメーション自体を起動しています。

- (void)viewDidLoad {

    // NSArray initialization

    NSTimer *timer = [NSTimer timerWithTimeInterval:16
                                         target:self
                                       selector:@selector(onTimer)
                                       userInfo:nil
                                        repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [timer fire];

    [super viewDidLoad];
}

これはonTimerセレクターコードです:

- (void) onTimer {

    // cycle through max 9 images
    if(imageIndex > 8)
        imageIndex = 0;

    // set the image
    [_imageContainer setImage:[images objectAtIndex:imageIndex]];

    // reset width and height of the UIImage frame
    CGRect frame = [_imageContainer frame];
    frame.size.width -= 170.0f;
    frame.size.height -= 100.0f;
    [_imageContainer setFrame:frame];


    // fade in
    [UIView animateKeyframesWithDuration:2.0f delay:0.0f options:0 animations:^{
        [_imageContainer setAlpha:1];
    } completion:^(BOOL finished) {
        // image movement
        [UIView animateKeyframesWithDuration:12.0f delay:0.0f options:0 animations:^{
            CGRect frame = [_imageContainer frame];
            frame.size.width += 170.0f;
            frame.size.height += 100.0f;
            [_imageContainer setFrame:frame];
        } completion:^(BOOL finished) {
            // fade out
            [UIView animateKeyframesWithDuration:2.0f delay:0.0f options:0 animations:^{
                [_imageContainer setAlpha:0];
            } completion:nil];
        }];
    }];

    imageIndex++;
}

これは、私が望むものを達成するための非常に生の、しかし「機能する」方法のように見えますが、理想的な方法ではない可能性があることを認識しています。

私が探しているものを達成するためのより良い方法はありますか?

4

1 に答える 1

0

フェードアニメーションの更新された回答

- (void)viewDidLoad
{
    [super viewDidLoad];

    // self.animationImages is your Image Array
    self.animationImages = @[[UIImage imageNamed:@"Image1"], [UIImage imageNamed:@"Image2"]];

    // make the first call
    [self animateImages];
}

- (void)animateImages
{
    static int count = 0;

    UIImage *image = [self.animationImages objectAtIndex:(count % [animationImages count])];

    [UIView transitionWithView:self.animationImageView
                      duration:1.0f // animation duration
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.animationImageView.image = image; // change to other image
                    } completion:^(BOOL finished) {
                        [self animateImages]; // once finished, repeat again
                        count++; // this is to keep the reference of which image should be loaded next
                    }];
}
于 2015-10-28T17:50:56.337 に答える