UIImageView介绍
UIImageView是用来放置图片的容器,在app的开发中,想要展示图片就要使用UIImageView这个控件。下面来介绍使用
UIImageView的创建
UIImageView和UILabel一样都是继承自UIView的,所以UIImageView的创建和UIView一样。
//UIImageView的创建
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
使用command+鼠标左键查看UIImageView的头文件可以看到除了继承自UIView的创建方法还有2个创建方法
- (instancetype)initWithImage:(nullable UIImage *)image;
- (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage NS_AVAILABLE_IOS(3_0);
无论是哪一种创建方法,有一点需要明确的是,控件是用来展示图片的。所以位置大小和图片是关键。下面来介绍一下UIImageView的常用属性和方法
UIImageView的常用属性
1、image属性
image属性是图片,即加载到UIImageView上的图片,通过这个属性的设置可以让图片展示到界面上。
//给imageView设置图片 这里使用了UIImage类来给image属性赋值 UIImage类给image赋值常用的方法有两个
//+ (nullable UIImage *)imageNamed:(NSString *)name; 使用图片名称赋值
//+ (nullable UIImage *)imageWithContentsOfFile:(NSString *)path; 使用图片地址赋值
imageView.image = [UIImage imageNamed:@"image"];
UIImageView其他的常用属性都是继承自UIView。这里不再重复介绍,在UIImageView的头文件中可以看到一些其他的属性和方法:
@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
@property (nullable, nonatomic, copy) NSArray<UIImage *> *highlightedAnimationImages NS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
@property (nonatomic) NSTimeInterval animationDuration; // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
@property (nonatomic) NSInteger animationRepeatCount; // 0 means infinite (default is 0)
- (void)startAnimating;
- (void)stopAnimating;
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, getter=isAnimating) BOOL animating;
#else
- (BOOL)isAnimating;
这些是和图片有关的动画设置,这里先不记录,等以后写到关于动画的时候在统一记录。