基于在IOS相互作用状态如何改变所显示的图像/ GIF

问题描述:

我有需要基于交互状态,以显示图像和gif应用程式分别基于在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]; 
}