您的位置:首页 > 产品设计 > UI/UE

UIImageView详解

2016-01-27 10:15 369 查看
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 五种初始化方法
// 第一种 只初始化
UIImageView *imageView1 = [[UIImageView alloc] init];

// 第二种 用xib初始化
//UIImageView *imageView2 = [[UIImageView alloc] initWithCoder:NSCoder ];

// 第三种 用位置和大小初始化
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 250, 250)];

// 第四种 用图片初始化
UIImageView *imageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];

// 第五种 用两张图片初始化 普通状态、高亮状态下各一张
UIImageView *imageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName1"] highlightedImage:[UIImage imageNamed:@"imageName2"]];

// 设置普通状态下的图片 若imageView用第四、五种方法初始化的 则会覆盖之前的正常状态下的图片
imageView1.image = [UIImage imageNamed:@"imageName"];

// 设置高亮状态下的图片 若imageView用第五种方法初始化的 则会覆盖之前的高亮状态下的图片 (默认nil)
imageView1.highlightedImage =[UIImage imageNamed:@"imageName"];

// 设置是否可以和用户交互 (默认NO) 用于手势
imageView1.userInteractionEnabled = YES;

// 设置是否高亮 (默认NO)
imageView1.highlighted = YES;

// 在iOS 7后,UIView新增加了一个tintColor属性,这个属性定义了一个非默认的着色颜色值,其值的设置会影响到以视图为根视图的整个视图层次结构。它主要是应用到诸如app图标、导航栏、按钮等一些控件上,以获取一些有意思的视觉效果
// 默认情况下,一个视图的tintColor是为nil的,这意味着视图将使用父视图的tint color值。当我们指定了一个视图的tintColor后,这个色值会自动传播到视图层次结构(以当前视图为根视图)中所有的子视图上。如果系统在视图层次结构中没有找到一个非默认的tintColor值,则会使用系统定义的颜色值(蓝色,RGB值为[0,0.478431,1],我们可以在IB中看到这个颜色)。因此,这个值总是会返回一个颜色值,即我们没有指定它。
// 想深入了解请猛戳这里:http://www.cocoachina.com/ios/20150703/12363.html?utm_medium=referral&utm_source=pulsenews
imageView1.tintColor = [UIColor redColor];

// 设置UIImage的渲染模式:UIImage.renderingMode 即设置一个UIImage在渲染时是否使用当前视图的Tint Color。
imageView1.image = [[UIImage imageNamed:@"imageName"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
/*
UIImageRenderingModeAutomatic          根据图片的使用环境和所处的绘图上下文自动调整渲染模式 (默认)
UIImageRenderingModeAlwaysOriginal     始终绘制图片原始状态,不使用Tint Color
UIImageRenderingModeAlwaysTemplate     始终根据Tint Color绘制图片,忽略图片的颜色信息
*/

// 设置内容模式 按照原始比例缩放图片,保持纵横比
imageView1.contentMode = UIViewContentModeScaleAspectFit;
/*
UIViewContentModeScaleToFill           拉伸自适应填满整个视图 (默认)
UIViewContentModeScaleAspectFit        自适应比例大小显示
UIViewContentModeScaleAspectFill       原始大小显示
UIViewContentModeRedraw                尺寸改变时重绘
UIViewContentModeCenter                中间
UIViewContentModeTop                   顶部
UIViewContentModeBottom                底部
UIViewContentModeLeft                  中间贴左
UIViewContentModeRight                 中间贴右
UIViewContentModeTopLeft               贴左上
UIViewContentModeTopRight              贴右上
UIViewContentModeBottomLeft            贴左下
UIViewContentModeBottomRight           贴右下
*/

NSMutableArray *imageArray = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"animation1_0%d", i] ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[imageArray addObject:image];
}
// 设置在普通状态下 动画的数组
imageView1.animationImages = imageArray;

// 高亮下的动画数组
NSMutableArray *highArray = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"animation2_0%d", i] ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[highArray addObject:image];
}

// 设置在高亮状态下的动画的数组
imageView1.highlightedAnimationImages = highArray;

// 设置动画的时间  默认的时间是每秒三十张图片 即如果有10张图片 则默认的时间是10/30秒
imageView1.animationDuration = 2;

// 设置动画重复的次数 (默认0 即无限次)
imageView1.animationRepeatCount = 2;

//最后把这个imageView添加上去
_imageView = imageView1;
imageView1.frame = CGRectMake(20, 100, 250, 250);
[self.view addSubview:imageView1];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 是否在执行动画
if ([_imageView isAnimating]) {
// 停止动画
[_imageView stopAnimating];
} else {
// 开始动画 从数组的第一张图片开始
[_imageView startAnimating];
}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiimageview