使用自定义帧图像执行自定义动画

时间:2013-12-12 19:32:41

标签: ios iphone objective-c animation

我正在使用故事板。我有一个2 UIViewControler:view1和view2。 当按下view1上的任何一个按钮时,会执行一个segue,因此调用此方法:prepareForSegue:。现在我想在使用名为navExitFrame1,navExitFrame2 ...的6个自定义图像执行segue之前为view1设置动画,然后在Modal>翻转水平下的故事板中显示默认动画集。

修改

我现在正在使用此代码:

    void (^animationBlock)() = ^{

        NSArray *images = @[[UIImage imageNamed:@"navExitFrame1.png"],
                            [UIImage imageNamed:@"navExitFrame2.png"],
                            [UIImage imageNamed:@"navExitFrame3.png"],
                            [UIImage imageNamed:@"navExitFrame4.png"],
                            [UIImage imageNamed:@"navExitFrame5.png"],
                            [UIImage imageNamed:@"navExitFrame6.png"]];

        UIImage *myimage = [UIImage imageNamed:@"profile.png"];

        [profileButton.imageView setImage:myimage];

        [self.view bringSubviewToFront:imageView];
        imageView.hidden = NO;

        NSUInteger imagesCount = [images count];

        for(NSUInteger i=0; i<imagesCount; i++) {

            [UIView addKeyframeWithRelativeStartTime:i/(CGFloat)imagesCount
                                    relativeDuration:1/(CGFloat)imagesCount
                                          animations:^{

                                              imageView.image = images[i];

                                          }];
        }
    };

    [UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeDiscrete animations:animationBlock completion:^(BOOL finished) {

        if (sender == feelingsButton) {

            NSString *save = @"feelings";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];

        } else if (sender == placesButton) {

            NSString *save = @"places";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];

        }else if (sender == peopleButton) {

            NSString *save = @"people";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];

        }else if (sender == settingsButton) {

            NSString *save = @"settings";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];

        }else if (sender == profileButton) {

            NSString *save = @"profile";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];

        }else if (sender == mainWebView || sender == mainWebView.gestureRecognizers) {

            NSString *save = @"newsfeed";
            [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"segueID"];
        }

       [self performSegueWithIdentifier:@"ContentVWS" sender:nil];

    }];
}

这里发生的是动画正在发生但是在MICROsecond的空间中,我尝试修改持续时间的值

animateKeyframesWithDuration:1

但没有任何改变。在for循环中放置一个断点之后我意识到除了最后一个之外没有显示任何图像但是当我进入构建阶段时,图像被列为复制资源包并且图像名称拼写正确可能这就是为什么它是在如此短的时间内发生。也许是

的值
UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeDiscrete animations:animationBlock completion:^(BOOL finished) {

是不正确的,我在完成时对它们进行了NSloged但是只有变量i是正确的而且imagesCount也是正确的但是当我这样做时我/(CGFloat)imagesCount似乎总是因某种原因返回0。这与它有关吗?

1 个答案:

答案 0 :(得分:3)

好吧,iOS7中的新关键帧动画方法似乎不希望在图像视图上很好地处理uiimage的变化,因此我们将不得不使用CAKeyframe动画: 这个答案部分来自这里:http://stackoverflow.com/questions/16025769/how-do-i-simultaneously-animate-a-uiimageviews-image-and-the-uiimageview-itself

-(IBAction)animateImagesThenPushView:(id)sender{

    NSArray *images = @[[UIImage imageNamed:@"navExitFrame1.png"],
                        [UIImage imageNamed:@"navExitFrame2.png"],
                        [UIImage imageNamed:@"navExitFrame3.png"],
                        [UIImage imageNamed:@"navExitFrame4.png"],
                        [UIImage imageNamed:@"navExitFrame5.png"],
                        [UIImage imageNamed:@"navExitFrame6.png"]];

    NSMutableArray *keyTimes = [NSMutableArray arrayWithCapacity:images.count];
    for (int i = 0; i<images.count; i++) {
        keyTimes[i] = @(0.0 + (i+1)*(1.0/images.count));
    }

    CAKeyframeAnimation *imageAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    imageAnimation.calculationMode = kCAAnimationDiscrete; // or maybe kCAAnimationPaced
    imageAnimation.duration = .5;
    imageAnimation.keyTimes = keyTimes;
    imageAnimation.repeatCount = 0;
    // the following method will need to be implemented to cast your UIImage array to CGImages
    imageAnimation.values = [self animationCGImagesArrayFromImageArray:images];
    imageAnimation.delegate = self;
    [self.imageView.layer addAnimation:imageAnimation forKey:@"content"];

}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    self.imageView.image = [UIImage imageNamed:@"navExitFrame6.png"];
    // put your segue logic here to push the next view
}

-(NSArray*)animationCGImagesArrayFromImageArray:(NSArray*)imageArray {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:imageArray.count];
    for (UIImage *image in imageArray) {
        [array addObject:(id)[image CGImage]];
    }
    return [NSArray arrayWithArray:array];
}