iOS阶段学习第27天笔记(UIButton-UIImageView的介绍)

时间:2021-07-15 21:06:29

iOS学习(UI)知识点整理

一、关于UIButton的介绍

1)概念:UIButton 是一种常用的控件,通过点击触发相应的功能

2)UIButton 的几种常用的状态
        1、UIControlStateNormal  正常状态
        2、UIControlStateHighlighted 高亮状态
        3、UIControlStateSelected 选中状态  -> 当button的selected设置成yes之后才能触发

3)UIButton常用的几种事件
      1、UIControlEventTouchUpInside  按钮按下并抬起事件
      2、UIControlEventTouchDown   按钮按下事件
      3、UIControlEventTouchDownRepeat 按钮多次点击触发事件

4)UIButton 初始化实例代码

 1 UIButton *button = [[UIButton alloc] init];
 2 button.frame = CGRectMake(20, 50, 50 , 50);
 3 button.backgroundColor = [UIColor clearColor];
 4 [button setTitle:@"按钮1 正常状态" forState:UIControlStateNormal];
 5 [button setTitle:@"按钮1 高亮状态" forState:UIControlStateHighlighted];
 6 [button setTitle:@"按钮1 选中状态" forState:UIControlStateSelected];
 7 
 8 //按钮点击时触发事件
 9 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
10 //按钮按下后触发事件
11 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDown];
12 //按钮双击触发事件
13 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDownRepeat];
14 //设置按钮高亮状态下的字体颜色
15 [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
16 //button字体变为35号加粗的字体
17 button.titleLabel.font = [UIFont boldSystemFontOfSize:35];
18 //设置圆角    
19 button.layer.cornerRadius = 5.f;
20 //设置边框宽度
21 button.layer.borderWidth = 2.1;
22 //设置边框颜色
23 button.layer.borderColor = [UIColor lightGrayColor].CGColor;
24  //设置按钮背景图   
25 UIImage *imageNormal = [UIImage imageNamed:@"camera"];
26 //设置imageNormal为按钮的正常情况的图片
27 [button setImage:imageNormal forState:UIControlStateNormal];
28     
29 UIImage *imageHightLight = [UIImage imageNamed:@"camera2"];
30 //设置imageHightLight为按钮的高亮情况的图片
31 [button setImage:imageHightLight forState:UIControlStateHighlighted];
32 //当button设置了图片的时候 并且没有设置高亮状态下得图片,取消高亮状态, 默认是Yes
33 button.adjustsImageWhenHighlighted = YES;    
34 [self.window addSubview:button];

 
5)防止按钮多次点击重复提交数据的实例代码

 1 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
 3 - (void)buttonTapped:(UIButton *)button
 4 {
 5     //设置按钮不可点击
 6     button.userInteractionEnabled = NO;  
 8     //延迟执行方法 防止按钮被快速点击或者不希望点击造成错误
 9     [self performSelector:@selector(delayMethod:) withObject:button afterDelay:1]; 
11 }
12 
13 //延迟方法->设置按钮为可点击状态
14 - (void)delayMethod:(UIButton *)button
15 {
16     button.userInteractionEnabled = YES;    
17 }

 

二、关于UIImageView的介绍

1)概念:UIImageView 是iOS中专门用于展示图片的控件

2)UIImageView 初始化 实例代码

 1     UIImageView *imageView = [[UIImageView alloc] init];
 2     imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
 3     imageView.backgroundColor = [UIColor whiteColor];
 4     imageView.center = self.view.center;
 5 
 6     //tag设置控件的唯一标识,值不能重复
 7     imageView.tag = 100;
 8 
 9     //UIImageView的 clipsToBounds属性,设置为yes的时候超出部分,不予以显示
10     imageView.clipsToBounds = YES;
11 
12     //读取一张图片
13     UIImage *image = [UIImage imageNamed:@"icon"];
14     imageView.image = image;
15 
16     //设置图片展示模式
17     imageView.contentMode = UIViewContentModeScaleAspectFill;
18 
19     //打开imageview的用户交互 注:要实现图片点击事件此属性必须设置为YES
20     imageView.userInteractionEnabled = YES;
21     [self.view addSubview:imageView];
22 
23     //为UIImageView添加点击事件  
24     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
action:@selector(imageViewTapped:)];
25 [imageView addGestureRecognizer:tap];

 
3)UI_ImageView中常用的几种填充模式
   1、UIViewContentModeScaleToFill  拉伸image使其充满UIImageView
   2、UIViewContentModeScaleAspectFill 拉伸image使其不变形,并且充满UIImageView
   3、UIViewContentModeScaleAspectFit 拉伸imgage使其不变形,并且完全显示在UIImageView中

4)UITapGestureRecognizer  除了可以给UI_ImageView添加点击方法外还可以给其他控件添加点击方法
     如:UI_Lable、UI_View...等

5)iOS中获取图片的三种方法
 方法一: 

1 //把图片对象加载到内存中
2 UIImage *image1 = [UIImage imageNamed:@"camera"];
3 CGSize size = image1.size;
4 NSLog(@"size.w %f   size.h %f",size.width ,size.height);
5 //如果图片的格式是png,则后缀名可以省略,其他格式不能省略
6 UIImage *image2 = [UIImage imageNamed:@"icon.jpeg"];

方法二: 

//使用场景:读取大图片,比较占内存的,需要及时释放的图片要用这种方法
 //读取icon.jpeg
NSString *imagePath3 = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];
UIImage *image3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];

NSString *imagePath3_1 = [[NSBundle mainBundle] pathForResource:@"icon.jpeg" ofType:nil];
UIImage *image3_1 = [[UIImage alloc] initWithContentsOfFile:imagePath3_1];

方法三:

1 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];
2 
3  UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];