我想通过计时器更改我的自定义UIImageView子类的图片。但它不起作用。你能救我吗?
这是我的代码:
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self update];
}
return self;
}
-(void)update {
self.image = [UIImage imageNamed:@"Green.png"];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(change) userInfo:nil repeats:YES];
}
-(void)change {
self.image = [UIImage imageNamed:@"Red.png"];
}
谢谢!
答案 0 :(得分:-1)
使用image
属性也可以将任何图像分配给imageView小整改,请参见下面的代码。
另外,不要将变量名称与其类属性相同。
-(void)update {
self.image.image = [UIImage imageNamed:@"Green.png"];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(change) userInfo:nil repeats:YES];
}
-(void)change {
self.image.image = [UIImage imageNamed:@"Red.png"];
}
self.image。图片 peoperty在这里添加
编辑:
将您的媒体资源从UIImage
更改为UIImageView
,如下所示
@property (strong, nonatomic) IBOutlet UIImageView *image;
.h头文件
#import <UIKit/UIKit.h>
@interface orientation : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *image;
@end
.m消息文件
#import "orientation.h"
@interface orientation ()
@end
@implementation orientation
- (void)viewDidLoad {
[super viewDidLoad];
[self update];
// Do any additional setup after loading the view.
}
-(void)update {
self.image.image = [UIImage imageNamed:@"investment1.png"];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(change) userInfo:nil repeats:YES];
}
-(void)change {
self.image.image = [UIImage imageNamed:@"logout.png"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end