[英]How to show UIImageview animation when added to a UIView
我試圖創建一個自定義視圖,將所有UIImage保存在一個數組中,然后將此數組分配給UIImageView的animationImages屬性。
但是問題是,如果我在想要的UIViewController中執行此操作,則此方法正常,但是當我嘗試通過自定義UIView創建此控件時,它不顯示任何內容。
這是我的代碼:
@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
我將其用作:
_spinner = [[IndicatorView alloc]initWithFrame:CGRectMake(160, 240, 80, 80)];
[self.view addSubview:_spinner];
[_spinner startAnimating];
您應該使用bounds
因為您是相對於內部矩形進行bounds
。
_indicatorView = [[UIImageView alloc] initWithFrame:self.frame];
改成
_indicatorView = [[UIImageView alloc] initWithFrame:self.bounds];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.