简体   繁体   English

添加到UIView后如何显示UIImageview动画

[英]How to show UIImageview animation when added to a UIView

I am trying to create a custom view which saves all the UIImage's in an array and then assign this array to animationImages property of UIImageView. 我试图创建一个自定义视图,将所有UIImage保存在一个数组中,然后将此数组分配给UIImageView的animationImages属性。

But the problem is this works fine if I do this inside the UIViewController where I want this however when I am trying to create this through a custom UIView its not displaying anything. 但是问题是,如果我在想要的UIViewController中执行此操作,则此方法正常,但是当我尝试通过自定义UIView创建此控件时,它不显示任何内容。

Here's my code: 这是我的代码:

@interface IndicatorView : UIView

@property(nonatomic, strong) UIImageView * indicatorView;
-(void)createIndicator;
-(void)startAnimating;
-(void)stopAnimating;

@end

#import "ConfIndicatorView.h"

@implementation ConfIndicatorView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self createIndicator];

    }
    return self;
}


-(void)createIndicator
{
    NSArray * imageArray  = [[NSArray alloc] initWithObjects:
                             [UIImage imageNamed:@"spinner-1.png"],
                             [UIImage imageNamed:@"spinner-2.png"],
                             [UIImage imageNamed:@"spinner-3.png"],
                             nil];
    _indicatorView = [[UIImageView alloc] initWithFrame:self.frame];
    _indicatorView.animationImages = imageArray;
    _indicatorView.animationDuration = 1.0f;
    _indicatorView.contentMode = UIViewContentModeCenter;
[self addSubview:_indicatorView];
    [_indicatorView startAnimating];// Just added this to see if it works but still not
}
-(void)startAnimating
{
    [_indicatorView startAnimating];
}
-(void)stopAnimating
{
    [_indicatorView stopAnimating];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

And I am using this as: 我将其用作:

_spinner = [[IndicatorView alloc]initWithFrame:CGRectMake(160, 240, 80, 80)];
    [self.view addSubview:_spinner];
[_spinner startAnimating];

You should use bounds because you are doing it relative to the inner rect. 您应该使用bounds因为您是相对于内部矩形进行bounds
_indicatorView = [[UIImageView alloc] initWithFrame:self.frame];
change to 改成
_indicatorView = [[UIImageView alloc] initWithFrame:self.bounds];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM