动画背靠背完成目标c

时间:2016-05-04 06:58:06

标签: ios objective-c animation

我想在3个图像视图中连续动画做背靠背我已经使用下面的代码,但它只显示第一个imageview中的动画。我调试代码,它将进入方法,但它不会显示剩余任何动画.Below是我的代码

-(void)imageNewsAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
        _imgNews.animationImages=arrnews;
        _imgNews.animationDuration=1;
        _imgNews.animationRepeatCount=1;
        _imgNews.image = [_imgNews.animationImages lastObject];
        [_imgNews startAnimating];

    } completion:^(BOOL finished) {
        [_imgNews stopAnimating];
        [self imageEventAnimation];
    } ];
}

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


    } completion:^(BOOL finished) {
        [self imageDirectoryAnimation];
    } ];

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

}

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

}

Below is the Screenshot of imageview place one after another

2 个答案:

答案 0 :(得分:2)

由于startAnimating没有完成处理程序,我会设置如下内容:

<强> CycleImageView.h

#import <UIKit/UIKit.h>

typedef void (^CycleCompletion)(BOOL finished);

@interface CycleImageView : UIImageView

- (void)startAnimatingWithImages:(NSArray<UIImage *>*)images completion:(CycleCompletion)completion;

@end

<强> CycleImageView.m

#import "CycleImageView.h"

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

@implementation CycleImageView

- (void)startAnimatingWithImages:(NSArray<UIImage *>*)images completion:(CycleCompletion)completion {
    self.completion = completion;

    NSMutableArray *imageRefs = [NSMutableArray array];
    for (NSInteger i = 0; i < images.count; i++) {
        [imageRefs addObject:(id)images[i].CGImage];
    }

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    animation.delegate = self;
    animation.calculationMode = kCAAnimationDiscrete;
    animation.duration = 1;
    animation.values = imageRefs;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:animation forKey:@"animation"];
}

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

@end

使用它:

制作imgNews,imgEvents,imgDirectory和imgInfo CycleImageView的所有子类。然后,使用提供的方法和变量,实现将更改为:

- (void)imageNewsAnimation {
    [_imgNews startAnimatingWithImages:arrnews completion:^(BOOL finished) {
       if (finished) {
           [self imageEventAnimation];
       } 
    }];
}

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

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

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

修改

也为CycleImageView创建了一个gist

答案 1 :(得分:0)

使用计时器执行动画,如

viewDidLoad

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(imageAnimation:) userInfo:nil repeats:YES];
i = 0;

然后

- (void)imageAnimation:(UIImage *) image {
    i++;
    if (i > 2) {
        i = 0;
    }
    NSArray *arr = @[[UIImage imageNamed:@"img1"],[UIImage imageNamed:@"img2"],[UIImage imageNamed:@"img3"]];
    UIImage * toImage = [arr objectAtIndex:i];
    [UIView transitionWithView:self.view
                      duration:2
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.imageview.image = toImage;
                    } completion:NULL];
}

希望这有帮助!