代码之家  ›  专栏  ›  技术社区  ›  questionar

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

  •  0
  • questionar  · 技术社区  · 7 年前

    这是我的密码

    #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会连续播放

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   questionar    7 年前

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