Objective-C Learning Notes - UIImageView
UIImageView
创建方法
UIImageView *imageView1 = [[UIImageView alloc] init];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)];
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];
UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)];
UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];
Frame与 bounds 属性
设定 frame 属性:iamgeView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
bounds属性: imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
PS:frame属性用于设置其位置和大小,而 bounds 属性只能设置其大小,其中 xy 参数不起作用,即便是之前没有设定 frame 属性,控件最终的位置也不是 bounds 所设定的参数. bounds 实现的时讲 UIIamgeView 控件以原来的中心为中心进行缩放. 例如:
imageView.frame = CGRectMake(0,0,320,460);
imageView.bounds = CGRectMake(100,100,160,230);
执行之后,这个 imageView 的位置和大小是(80,115,160,230).
contentMode 属性
设置图片的显示方式:
UIViewContentModeScaleToFill;//强制缩放到 ImageView 尺寸
UIViewContentModeScaleAspectFit;//保持比例缩放
UIViewContentModeScaleAspectFill;//保持比例不变,可能截取部分页面
UIViewContentModeRedraw;
UIViewContentModeCenter;
UIViewContentModeTop;
UIViewContentModeBottom;
UIViewContentModeLeft;
UIViewContentModeRight;
UIViewContentModeTopLeft;
UIViewContentModeTopRight;
UIViewContentModebottomLeft;
UIViewContentModeBottomRight;
PS: 凡是没有带有 Scale 字样的参数,当图片尺寸超过 ImageView 尺寸时,只部分显示在 ImageView中.
改变位置
方式:
直接修改其 frame 属性;
修改其 center 中间点属性:imageView.center = CGPointMake(CGFloat x, CGFloat y);
使用 transform 属性: imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);//dx表示 x 轴防线移动距离, dy 表示 y 轴方向移动的距离
旋转图片
imageView.tranform = CGAfflineTransformMakeRotation(CGFloat angle);//angle单位是弧度 π
缩放图像
imageView.transform = CGAfflineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
//scale_w 是横向拉伸罗, scale_h 是纵向拉伸罗,单位就是缩放倍数
播放一系列图片
imageView.animationImages = imagesArray;//载入图片数组
imageView.animationDuration = [imagesArray count];//设定图片在多少秒之内播放完
imageView.animationRepeatCount = 0;//重复次数,0表示不限制
[imageView startAnimation];//开始播放
为图片添加单击触发事件
imageView.userInteractionEnabled = YES;//必须要打开这个先
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer: singleTap];
其他设置
imageView.hidden = YES/NO; //隐藏图片
imageView.alpha = (CGFloat) al; // 设置透明度
imageView.highlightedImage = (UIImage*)highlightedImage; // 设置高亮时显示的图片
imageView.image = (UIImage *)image; //设置正常显示的图片
[imageView sizeToFit]; //将图片尺寸调整到与内容图片相同
代码明天再写吧,碎觉了 www
20150211晚上我又来啦www
贴上动态实现代码:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage *myImage = [UIImage imageNamed:@"0.jpg"]; UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; myImageView.center = self.view.center; myImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; [self.view addSubview:myImageView]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
修改UIImageView透明度:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage *myImage = [UIImage imageNamed:@"0.jpg"]; UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; //用 UIImageView 得 alpha 属相设置图片透明度 myImageView.alpha = 0.5; myImageView.center = self.view.center; myImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; [self.view addSubview:myImageView]; self.view.backgroundColor = [UIColor blackColor]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end