添加自定义方法 addImageView
-(void)addImageView {
UIImageView *imgview = [[UIImageView alloc]
initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"AppleUSA1.jpg"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];
}
添加另一个自定义方法 addImageViewWithAnimation
此方法解释了如何在 imageView 中为图像设置动画。
-(void)addImageViewWithAnimation {
UIImageView *imgview = [[UIImageView alloc]
initWithFrame:CGRectMake(10, 10, 300, 400)];
// set an animation
imgview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"AppleUSA1.jpg"],
[UIImage imageNamed:@"AppleUSA2.jpg"], nil];
imgview.animationDuration = 4.0;
imgview.contentMode = UIViewContentModeCenter;
[imgview startAnimating];
[self.view addSubview:imgview];
}
Note −
我们必须将名为“AppleUSA1.jpg”和“AppleUSA2.jpg”的图像添加到我们的项目中,这可以通过将图像拖到我们的导航器区域来完成,其中列出了我们的项目文件。
更新 ViewController.m 中的 viewDidLoad 如下 -
(void)viewDidLoad {
[super viewDidLoad];
[self addImageView];
}