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

标签 ios iphone objective-c animation

我正在使用 Storyboard。我有一个 2 UIViewControler:view1 和 view2。 当按下 view1 上的 6 个按钮中的任何一个时,将执行一个 segue,因此调用此方法:prepareForSegue:。现在我想在 view1 执行 segue 之前使用 6 个自定义图像(称为 navExitFrame1、navExitFrame2...)设置动画,然后在 Storyboard中的 Modal>flip horizo​​ntal 下显示默认动画集。

编辑:

我现在使用这段代码:

    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];

    }];
}

这里发生的是动画正在发生,但在一微秒的空间内,我尝试修改 Duration 的值

animateKeyframesWithDuration:1

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

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

不正确,我在完成时记录了它们,但只有变量 i 是正确的并且 imagesCount 也是正确的但是当我这样做时 i/(CGFloat)imagesCount 似乎总是由于某种原因返回 0。这有什么关系吗?

最佳答案

好吧,似乎 iOS7 中新的关键帧动画方法不想在 ImageView 上很好地适应 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];
}

关于ios - 使用自定义帧图像执行自定义动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552534/

相关文章:

ios - 不要一次上传所有东西 Firebase

ios - fetchAssetCollectionsWithType 仅检索视频

iphone - 对于给定的配置,我应该尝试使用 NSUserDefaults 还是去 CoreData?

iphone - iPad 标签栏应用程序不自动旋转

objective-c - 如何使用箭头键移动鼠标光标

objective-c - forwardingTargetForSelector : work? 怎么办

ios - 如何编辑使用 dequeueReusableCellWithIdentifier 后在新单元格中自动创建的文本标签

ios - 我如何删除 Objective-c 中的 cksubscription?

ios - 修改曝光持续时间并返回 AVCaptureExposureModeContinuousAutoExposure 后的奇怪行为

iphone - PresentModalViewController 操作耗时