您的图像是堆叠的,因为当您通过该方法添加图像时addSubview
,它将一直存在,直到您使用该方法调用删除该图像removeFromSuperview
。但是,让我们让这个过程更加健壮一些。
首先,在您的 Storyboard 文件中,将 UIImageView 拖到您的视图中。将其尺寸设置为您的 likieng。然后按下助手编辑器(右上角的小燕尾服图标),它应该打开您正在编辑的视图控制器的相应.h
文件。取你在视图上的 UIImageView,按住 CTRL 并拖到你的.h
文件上,给它起一个名字imageToDisplay
当您放手时,您应该会在.h
文件中看到如下代码:
@property (weak, nonatamoic) IBOutlet UIImageView *imageToDisplay;
再添加一个属性,这次是 NSArray,这一次你不需要 Storyboard:
@property (strong, nonatomic) NSArray *imageArray;
现在回到您的.m
文件中,在您的viewDidLoad
方法中,让我们创建一个UIImage
s 数组:
UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];
_imageArray = @[image1, image2, image3, image4, image5];
现在我们的数组中有 5 个图像,我们现在可以从数组中选择一个随机图像并将其分配给我们的UIImageView
属性,该属性连接到我们的视图。另外,让我们的_imageToDisplay alpha
属性等于 0.0,这样我们就可以更深入地了解显示的图像以及显示方式:
//Still in viewDidLoad
_imageToDisplay.alpha = 0.0;
我们还可以做一些有趣的事情,比如动画和其他类似的事情。让我们创建一个方法来简化这个过程:
-(void)displayImageRandomlyOnView {
//First, let's pick a random image to display. We will do this by generating a random number based from the count of our imageArray
NSUInteger randomIndex = arc4random_uniform([_imageArray count]);
//Now let's pick the random image with the objectAtIndex method for NSArray's. We will assign this image to our _imageToDisplay's .image property.
_imageToDisplay.image = [_imageArray objectAtIndex:randomIndex];
//Now using your original code, we can randomly define the frame of our _imageToDisplay
int xValue = arc4random() % 320;
int yValue = arc4random() % 480;
_imageToDisplay.frame = CGRectMake(xValue, yValue, CGRectGetWidth(_imageToDisplay.frame), CGRectGetHeight(_imageToDisplay.frame));
//Now if you remember, our imageToDisplay 's value is still 0.0, so let's animate it to show our randomly chosen image and location.
[UIView animateWithDuration:1.0 animations:^ {
_imageToDisplay.alpha = 1.0;
}];
}
这是代码在.h
文件中的显示方式:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageToDisplay;
@property (strong, nonatomic) NSArray *imageArray;
@end
这是所有代码在.m
文件中的显示方式:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];
_imageArray = @[image1, image2, image3, image4, image5];
_imageToDisplay.alpha = 0.0;
}
-(void)displayImageRandomlyOnView {
//First, let's pick a random image to display. We will do this by generating a random number based from the count of our imageArray
NSUInteger randomIndex = arc4random_uniform([_imageArray count]);
//Now let's pick the random image with the objectAtIndex method for NSArray's. We will assign this image to our _imageToDisplay's .image property.
_imageToDisplay.image = [_imageArray objectAtIndex:randomIndex];
//Now using your original code, we can randomly define the frame of our _imageToDisplay
int xValue = arc4random() % 320;
int yValue = arc4random() % 480;
_imageToDisplay.frame = CGRectMake(xValue, yValue, CGRectGetWidth(_imageToDisplay.frame), CGRectGetHeight(_imageToDisplay.frame));
//Now if you remember, our imageToDisplay 's value is still 0.0, so let's animate it to show our randomly chosen image and location.
[UIView animateWithDuration:1.0 animations:^ {
_imageToDisplay.alpha = 1.0;
}];
}
现在每次运行此方法时,都会显示随机选择的图像以及随机位置。我会使用您的随机值来确保图像将保持在您的视图范围内。
此外,作为一个挑战,尝试将此方法连接到UIButton
视图中的 a,并使其每次按下按钮时,-(void)displayImageRandomlyOnView
都会调用我们创建的方法并更改您的图像。
如果您需要更多说明,请告诉我!