如何根据 ios 中的交互状态更改显示的 image/gif

How to change the displayed image/gif based on interaction state in ios

我有一个应用程序需要根据交互状态分别显示图像和gif

这是我的代码

#import "InteractionView.h"
#import "FLAnimatedImage.h"

@interface InteractionView() {
CADisplayLink *_displayLink;
UIImage* micImage;
FLAnimatedImage *image;
FLAnimatedImageView *imageView;
}
@end

@implementation InteractionView

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    [self initialize];
}

return self;
}

- (void)awakeFromNib {
[super awakeFromNib];
[self initialize];
}

- (void)initialize {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = YES;
micImage = [UIImage imageNamed: @"mic_button.png"];

_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(redrawView:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

_interactionState = image;
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"button_audio" withExtension:@"gif"];
NSString*stringPath = [imgPath absoluteString];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
image = [FLAnimatedImage animatedImageWithGIFData:data];
imageView = [[FLAnimatedImageView alloc] init];
}

- (void)dealloc {
[_displayLink invalidate];
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
_displayLink = nil;
}

- (void)setInteractionState:(InteractionState)interactionState {
if (_interactionState == image && interactionState != image) {
    _displayLink.paused = NO;
}

_interactionState = interactionState;
}

- (void)redrawView:(CADisplayLink *)displayLink {
[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)frame {
CGContextRef context = UIGraphicsGetCurrentContext();
if ( _interactionState == gif) {
    imageView.animatedImage = image;
    imageView.frame = CGRectMake(CGRectGetMinX(frame) + 40, CGRectGetMinY(frame), 70, 70);
    [self addSubview:imageView];   
} else if (_interactionState == image) {
    UIBezierPath* micPath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + 40, CGRectGetMinY(frame), 70, 70)];
    CGContextSaveGState(context);
    [micPath addClip];
    CGContextScaleCTM(context, 1, -1);
    CGContextDrawTiledImage(context, CGRectMake(CGRectGetMinX(frame) + 40, CGRectGetMinY(frame), 70, 70), micImage.CGImage);
    CGContextRestoreGState(context);
}
[self setNeedsDisplay];
}
@end

当我启动应用程序时,交互状态为图像,因此当交互更改为 gif 时显示图像,之后显示 gif,如果再次将交互更改为图像但不显示图像gif连续播放

如何解决这个问题,如果交互状态是图像,那么图像应该在那里,如果交互状态是 gif,那么 gif 应该在那里

drawRect 和 reDrawView 更改为

- (void)redrawView:(CADisplayLink *)displayLink {
if ( _interactionState == gif ) {
    imageView.animatedImage = image;
    imageView.frame = CGRectMake(40, 0, 70, 70);
    imageView.tag = 17;
    [self addSubview:imageView];   
}
if( _interactionState == image ) {
    UIView *viewToRemove = [self viewWithTag:17];
    [viewToRemove removeFromSuperview];
}
[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)frame {
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath* micPath = [UIBezierPath bezierPathWithRect: CGRectMake(40, 0, 70, 70)];
CGContextSaveGState(context);
[micPath addClip];
CGContextScaleCTM(context, 1, -1);
CGContextDrawTiledImage(context, CGRectMake(40, 0, 70, 70), micImage.CGImage);
CGContextRestoreGState(context);
[self setNeedsDisplay];
}