博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIImangeView的用法
阅读量:4566 次
发布时间:2019-06-08

本文共 4013 字,大约阅读时间需要 13 分钟。

1.UIImageView 是用来设置图片的显示方式

- (void)viewDidLoad {    [super viewDidLoad];//    UIImageView *imangview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"3.jpg"]];        imangview.frame=CGRectMake(20, 20, 50, 100);    //imangview.image=[UIImage imageNamed:@"3.jpg"];    [self.view addSubview:imangview];}

运行结果如下:

2.UIImageView的动画效果
#import "ViewController.h"@interface ViewController (){    UIImageView *image;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];      image=[[UIImageView alloc]initWithFrame:CGRectMake(20,20, 200,200)];    image.image=[UIImage imageNamed:@"12"];    //图片的调整方法    [image sizeToFit];//调整image,调整其尺寸适应图片大小(image的size等于图片的size)        //image.contentMode=UIViewContentModeScaleAspectFit;//以image宽和高中较小的为标准进行等比缩放    //image.contentMode=UIViewContentModeScaleAspectFill;//以image宽和高中较大的为标准进行等比缩放。如果image尺寸不够,则进行负方向调整,但是不会影响iamge的frame    //image.contentMode=UIViewContentModeRedraw;        //image.contentMode=UIViewContentModeCenter;//图片显示image的原始大小,显示的位置居于imageview的中心,如果imageview的尺寸不够,则负向调整    NSLog(@"****%@",NSStringFromCGRect(image.frame));
 

 

如下的简单例子
#import "ViewController.h"@interface ViewController (){    UIImageView *image;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];      image=[[UIImageView alloc]initWithFrame:CGRectMake(20,20, 200,200)];    NSMutableArray *arr=[[NSMutableArray alloc]initWithCapacity:0];    for (int i=1; i<6; i++) {        UIImage *animalim=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];//多个图片组成的数组        [arr addObject:animalim];       }    image.animationImages=arr;//循环播放的图片    image.animationDuration=5;//循环所有图片所需时间    image.animationRepeatCount=2;//循环次数,0代表无限循环    [image startAnimating ];//开始动画   image.userInteractionEnabled=YES;//允许交互    [self.view addSubview:image];        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];    button.frame=CGRectMake(20, 20, 20, 20);    button.backgroundColor=[UIColor redColor];    [button addTarget:self action:@selector(stopAnimation) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    }-(void)stopAnimation{    [image stopAnimating];    image.image=[UIImage imageNamed:@"5.jpg"];//按下停止后,出现的某一背景图片    NSLog(@"停止播放");}

 

3.UIImageView的点击和拖动效果
如下的简单例子
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(50, 20, 100, 100)];    imgview.image=[UIImage imageNamed:@"1.jpg"];//背景图片1        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagetaped:)];        imgview.tag=1;    //注意一个手势不能添加多个视图    [imgview addGestureRecognizer:tap];    imgview.userInteractionEnabled=YES;       UIImageView *imgview1=[[UIImageView alloc]initWithFrame:CGRectMake(50, 130, 100, 100)];    imgview1.image=[UIImage imageNamed:@"6.jpg"];//背景图片2    imgview1.tag=2;    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagetaped:)];     [imgview1 addGestureRecognizer:tap1];    imgview1.userInteractionEnabled=YES;    //tap.numberOfTapsRequired=2;//点击次数    //tap1.numberOfTapsRequired=2;//手指个数、单击    [self.view addSubview:imgview1];        //设置img和img1不同的手势    //点动手势    UITapGestureRecognizer *pan=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagetapedpan:)];    [imgview1 addGestureRecognizer:pan];    //拖动手势    UIPanGestureRecognizer *pan1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imagetapedpan:)];    [imgview addGestureRecognizer:pan1];     [self.view addSubview:imgview];}-(void)imagetapedpan:(UITapGestureRecognizer *)pan{    NSLog(@"****状态%lu",pan.state);    CGPoint point=[pan locationInView:self.view];    pan.view.center=point;}-(void)imagetaped:(UITapGestureRecognizer *)ges{    UIView *view=ges .view;    NSLog(@"######视图被点击%zi",view.tag);}

 

 

转载于:https://www.cnblogs.com/wyhwyh2114/p/4956260.html

你可能感兴趣的文章
Paint Chain HDU - 3980(sg)
查看>>
Chales常用操作
查看>>
C++ 运算符重载<<
查看>>
windows镜像
查看>>
Flask 模板语法
查看>>
ZOJ FatMouse' Trade 贪心
查看>>
音乐播放器
查看>>
SQL COOKBOOK (Ch.1-10)
查看>>
创建数组
查看>>
dict使用
查看>>
[转] 移动平台Html5的viewport使用经验
查看>>
ASP.NET MVC的帮助类HtmlHelper和UrlHelper
查看>>
《Python数据科学手册》第五章机器学习的笔记
查看>>
ubuntu16.04 配置爬虫环境
查看>>
Centos7,PHP7安装swoole
查看>>
02_ListActive中响应事件 并LogCat输出
查看>>
doubleclick adx note
查看>>
Celery框架
查看>>
[c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
查看>>
[转载,感觉写的非常详细]DUBBO配置方式详解
查看>>