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); }