画像ビューの使用方法
画像ビューは、単一の画像またはアニメーションシーケンスの画像を表示するために使用されます。
重要なプロパティ
- image
- highlightedImage
- userInteractionEnabled
- animationImages
- animationRepeatCount
重要なメソッド
- (id)initWithImage:(UIImage *)image
- (id)initWithImage:(UIImage *)image highlightedImage: (UIImage *)highlightedImage
- (void)startAnimating
- (void)stopAnimating
カスタムメソッド 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];
}
もう1つのカスタムメソッド 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];
}
注意:私たちは「AppleUSA1.jpg」と「AppleUSA2.jpg」という名前の画像をプロジェクトに追加する必要があります。これは、プロジェクトファイルが一覧表示されるナビゲーションエリアに画像をドラッグすることで行えます。
ViewController.m の viewDidLoad を次のように更新します。
(void)viewDidLoad
{
[super viewDidLoad];
[self addImageView];
}
その他の拡張