菜鸟教程小白 发表于 2022-12-12 19:43:14

ios - 完成 objective-c 中的一项后的背靠背动画


                                            <p><p>我想在 3 个 ImageView 中连续制作动画,我使用了下面的代码,但它只在第一个 ImageView 中显示动画。我调试代码,它将进入方法,但它不会显示剩余的任何动画.下面是我的代码</p>

<pre><code>-(void)imageNewsAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
      _imgNews.animationImages=arrnews;
      _imgNews.animationDuration=1;
      _imgNews.animationRepeatCount=1;
      _imgNews.image = ;
      ;

    } completion:^(BOOL finished) {
      ;
      ;
    } ];
}

-(void)imageEventAnimation
{
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
      _imgEvents.animationImages=arrEvents;
      _imgEvents.animationDuration=1;
      _imgEvents.animationRepeatCount=1;
      _imgEvents.image = ;
      ;


    } completion:^(BOOL finished) {
      ;
    } ];

}
-(void)imageDirectoryAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
      _imgDirectory.animationImages=arrdir;
      _imgDirectory.animationDuration=1;
      _imgDirectory.animationRepeatCount=1;
      _imgDirectory.image = ;
      ;
    } completion:^(BOOL finished) {
      ;
    } ];

}

-(void)imageInfoAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
      _imgInfo.animationImages=arrinfo;
      _imgInfo.animationDuration=1;
      _imgInfo.animationRepeatCount=1;
      _imgInfo.image = ;
      ;
    } completion:^(BOOL finished) {
      ;
    } ];

}
</code></pre>

<p> <a href="/image/HszJq.png" rel="noreferrer noopener nofollow"><img src="/image/HszJq.png" alt="Below is the Screenshot of imageview place one after another"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>由于 startAnimating 没有完成处理程序,我会设置如下:</p>

<p><strong>CycleImageView.h</strong></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

typedef void (^CycleCompletion)(BOOL finished);

@interface CycleImageView : UIImageView

- (void)startAnimatingWithImages:(NSArray&lt;UIImage *&gt;*)images completion:(CycleCompletion)completion;

@end
</code></pre>

<p><strong>CycleImageView.m</strong></p>

<pre><code>#import &#34;CycleImageView.h&#34;

@interface CycleImageView()
@property (nonatomic, copy) CycleCompletion completion;
@end

@implementation CycleImageView

- (void)startAnimatingWithImages:(NSArray&lt;UIImage *&gt;*)images completion:(CycleCompletion)completion {
    self.completion = completion;

    NSMutableArray *imageRefs = ;
    for (NSInteger i = 0; i &lt; images.count; i++) {
      .CGImage];
    }

    CAKeyframeAnimation *animation = ;
    animation.delegate = self;
    animation.calculationMode = kCAAnimationDiscrete;
    animation.duration = 1;
    animation.values = imageRefs;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    ;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (self.completion != nil) {
      self.completion(flag);
      self.completion = nil;
    }
}

@end
</code></pre>

<p><strong>要使用它:</strong></p>

<p>将 imgNews、imgEvents、imgDirectory 和 imgInfo 设为 CycleImageView 的所有子类。然后,使用您提供的方法和变量,实现将变为:</p>

<pre><code>- (void)imageNewsAnimation {
    [_imgNews startAnimatingWithImages:arrnews completion:^(BOOL finished) {
       if (finished) {
         ;
       }
    }];
}

- (void)imageEventAnimation {
    [_imgEvent startAnimatingWithImages:arrEvents completion:^(BOOL finished) {
       if (finished) {
         ;
       }
    }];
}

- (void)imageDirectoryAnimation {
    [_imgDirectory startAnimatingWithImages:arrdir completion:^(BOOL finished) {
       if (finished) {
         ;
       }
    }];
}

- (void)imageInfoAnimation {
    [_imgInfo startAnimatingWithImages:arrinfo completion:^(BOOL finished) {
       if (finished) {
         // NOTE: Are you intentionally creating this indefinite animation loop?
         ;
       }
    }];
}
</code></pre>

<p><strong>编辑:</strong></p>

<p>创建了 <a href="https://gist.github.com/jordancarney/a9252d4891d01e4939386b155bea7725" rel="noreferrer noopener nofollow">gist</a> CycleImageView 也是如此。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 完成 objective-c中的一项后的背靠背动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37020573/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37020573/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 完成 objective-c 中的一项后的背靠背动画