我有三个要在背景中旋转的图像。以下是我到目前为止所拥有的。我想要一个可以容纳所有这些 UIImageViews 并在后台随机显示它们的类。我阅读了 UIView 和 frame 方法,但我不知道如何添加它们,因为它只需要一帧。
因此,我使用 NSArray 来保存所有对象。现在唯一的问题是当新背景出现时,旧背景并没有消失。现在我要删除旧背景吗?
如果有人能指出我正确的方向,那就太好了。
谢谢!
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *imageArray;
@property (strong, nonatomic) UIImageView *imageView;
- (IBAction)buttonPressed:(UIButton *)sender;
@end
// .m 文件
@implementation ViewController
@synthesize imageArray;
@synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[UIImageView alloc] init];
UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];
UIImage *image3 = [UIImage imageNamed:@"image3.png"];
imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed:(UIButton *)sender {
NSUInteger index = arc4random_uniform(self.imageArray.count);
[imageView setImage:[imageArray objectAtIndex:index]];
}
@end