ios开发控件-UIView半栈工程师程序员

UIImageView

2018-01-17  本文已影响54人  无邪8

一、简介

<<图像的视图对象提供了一个用于显示单个图像或者一系列动画图像的基于视图的容器。对于动画图像,UIImageView的类提供设置动画的持续时间和频率的控制。您还可以启动和停止动画自由。

<<UIImageView和UILabel一样,也是UIKit框架中非常常用的视图类。继承关系和UILabel完全一致(都是继承于UIView),功能也相似(用户交互都默认为关,主要用于展示),只不过UIImageView用于展示图片,UILabel用于展示文字。

<<继承关系:UIImageView --> UIView -->UIResponder-->NSObject

格式为

1--> 设置图片,默认显示(属性的作用)

imageView .image = [UIImage imageNamed:@"logo.png"];(这是具体的例子)

@property (nullable, nonatomic, strong) UIImage *image; // 默认为空(这是属性的说明)

二、UIImageView的初始化方法(属性的顺序与苹果API一致)

1-->初始化UIImageView并设置image

   UIImageView *imageView = [[UIImageView alloc] initWithImage:oneImage]; //把oneImage添加到imageView上

- (instancetype)initWithImage:(nullable UIImage *)image; //初始化UIImageView并设置image

2-->初始化UIImageView并设置image与highlightedImage

UIImageView *imageView = [[UIImageView alloc] initWithImage:oneImage highlightedImage:twoImage]; //把oneImage添加到imageView上并设置highlightedImage

- (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage NS_AVAILABLE_IOS(3_0);

三、UIImageView的属性

1-->设置图片

imageView .image = [UIImage imageNamed:@"logo.png"];

@property (nullable, nonatomic, strong) UIImage *image; // 默认为空

2-->设置高亮状态下显示的图片

imageView .highlightedImage = [UIImage imageNamed:@"logo.png"];

@property (nullable, nonatomic, strong) UIImage *highlightedImage NS_AVAILABLE_IOS(3_0); //默认为空

3--> 设置是否允许用户交互

   imageView.userInteractionEnabled = YES; // 设置图片可以交互

@property (nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // 设置是否允许用户交互,默认不允许用户交互

4--> 设置是否为高亮状态

   imageView.highlighted = YES; //设置为高亮状态

@property (nonatomic, getter=isHighlighted) BOOL highlighted NS_AVAILABLE_IOS(3_0); //设置是否为高亮状态,默认为普通状态

5-->设置序列帧动画的图片数组

   imageView.animationImages = [NSArray array]; //将序列帧数组赋给UIImageView的animationImages属性

@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages; //该数组必须包含的UIImage对象,默认为空

详见UIImageView的属性之animationImages详解

6-->设置高亮状态下序列帧动画的图片数组

   imageView.highlightedAnimationImages = [NSArray array]; //将序列帧数组赋给UIImageView的highlightedAnimationImages属性

@property (nullable, nonatomic, copy) NSArray <UIImage *>*highlightedAnimationImages; //该数组必须包含的UIImage对象,默认为空

7-->设置序列帧动画播放的时常

imageView.animationDuration = 0.3; //设置动画时间

@property (nonatomic) NSTimeInterval animationDuration; //一个循环的图像。默认是图像的数量* 1/30秒(即30个fps)

8-->设置序列帧动画播放的次数

imageView.animationRepeatCount = 2;//设置动画次数 0 表示无限

@property (nonatomic) NSInteger animationRepeatCount; //0表示无限(默认值为0)

9-->色彩,痕迹,相当于是一个描述一个视图中的线条的颜色

imageView.tintColor=[UIColor redColor];

@property (null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);//tintColor是通过superview层次结构继承的

10--> 开启动画

[imageView startAnimating]; 

- (void)startAnimating;

11-->停止动画

[imageView stopAnimating]; 

- (void)stopAnimating;

12-->获取旋转状态

BOOL isAnimating=imageView.animating; //获取旋转状态

#if UIKIT_DEFINE_AS_PROPERTIES//UIKIT定义的属性

@property(nonatomic, readonly, getter=isAnimating) BOOL animating;

#else

- (BOOL)isAnimating;

#endif

四、UIImageView的TVOS_ONLY属性

@property (nonatomic) BOOL adjustsImageWhenAncestorFocused UIKIT_AVAILABLE_TVOS_ONLY(9_0);

@property(readonly,strong) UILayoutGuide *focusedFrameGuide UIKIT_AVAILABLE_TVOS_ONLY(9_0);

@property (nonatomic, strong, readonly) UIView *overlayContentView UIKIT_AVAILABLE_TVOS_ONLY(11_0);

@property (nonatomic) BOOL masksFocusEffectToContents UIKIT_AVAILABLE_TVOS_ONLY(11_0);

五、UIImageView的拓展

1、图片的加载方式

<<有缓存

UIImage *image =[UIImage imageNamed:@"图片名"];

使用场合:图片比较小、使用频率比较高

建议:把需要缓存的图片放到Assets.xcassets

<<没有缓存

NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片扩展名"];

第一种:

UIImage *image = [UIImage imageWithContentOfFile:file];

第二种:

NSData *data=[NSData dataWithContentsOfFile:filePath];

UIImage *image2=[UIImage imageWithData:data];

[imageView setImage:image2];

*只要方法名带有file的,都是传全路径

使用场合:图片比较大,使用频率比较低

建议:不需要缓存的图片不能放在Assets.xcassets中

详见iOS UIImageView加载图片的两种方式:

2、图片的内容模式

typedef NS_ENUM(NSInteger,UIViewContentMode) {

UIViewContentModeScaleToFill,

UIViewContentModeScaleAspectFit,// contents scaled to fit with fixed aspect. remainder is transparent

UIViewContentModeScaleAspectFill,// contents scaled to fill with fixed aspect. some portion of content may be clipped.

UIViewContentModeRedraw,// redraw on bounds change (calls -setNeedsDisplay)

UIViewContentModeCenter,// contents remain same size. positioned adjusted.

UIViewContentModeTop,

UIViewContentModeBottom,

UIViewContentModeLeft,

UIViewContentModeRight,

UIViewContentModeTopLeft,

UIViewContentModeTopRight,

UIViewContentModeBottomLeft,

UIViewContentModeBottomRight,

};

经验:

1.带有scale单词的图片有可能被拉伸:

UIViewContentModeScaleToFill:将图片拉伸填充整个imageView;图片显示的    尺寸跟imageView的尺寸是一样的.

2.带有scale单词的,并且带有aspect单词的:可能会被拉伸,但是会保持图片原来的宽高比

UIViewContentModeScaleAspectFit:保证刚好能看到图片的全部

UIViewContentModeScaleAspectFull:拉伸至图片的宽度或者高度跟imageView一样.

3.不带有scale单词的,图片绝对不会被拉伸,保持图片原来的宽度和高度

具体效果详见IOS UIImageView的contentMode属性

3、UIImageView添加特效

// 是否栅格化。

  // YES:会栅格化层中的数据(如:图像)

  // NO:不栅格化

  // 我们知道,栅格化数据是将矢量图变为位图。所以,如果层中的内容是变动的,每次都需要栅格化,就会影像效率。一般设置为NO

  [imgShadow.layer setShouldRasterize:NO];

  // 设置边框颜色

  [imgShadow.layer setBorderColor: [[UIColor whiteColor] CGColor]];

  // 设置边框宽度

  [imgShadow.layer setBorderWidth: 1.0];

  // 设置投影偏移量,CGSizeMake(x轴方向, y轴方向)

  [[imgShadow layer] setShadowOffset:CGSizeMake(1, 1)];

  // 设置投影颜色

  [[imgShadow layer] setShadowColor:[UIColor redColor].CGColor];

  // 设置投影半径

  [[imgShadow layer] setShadowRadius:3];

  // 设置透明度

  [[imgShadow layer] setShadowOpacity:1];

// 当设置为YES时,超过边界的将被遮盖(隐藏),经常与cornerRadius,属性使用。这样,圆角外的区域将被遮盖

[imgShadow.layer setMasksToBounds:YES];

  // 设置圆角

  imgShadow.layer.cornerRadius = 10;

参考

iOS开发UI篇-常用控件详解(UIImageView)

UIImageView属性

上一篇 下一篇

猜你喜欢

热点阅读