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 初始化实例代码
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(, , , );
button.backgroundColor = [UIColor clearColor];
[button setTitle:@"按钮1 正常状态" forState:UIControlStateNormal];
[button setTitle:@"按钮1 高亮状态" forState:UIControlStateHighlighted];
[button setTitle:@"按钮1 选中状态" forState:UIControlStateSelected]; //按钮点击时触发事件
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
//按钮按下后触发事件
[button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDown];
//按钮双击触发事件
[button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDownRepeat];
//设置按钮高亮状态下的字体颜色
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
//button字体变为35号加粗的字体
button.titleLabel.font = [UIFont boldSystemFontOfSize:];
//设置圆角
button.layer.cornerRadius = .f;
//设置边框宽度
button.layer.borderWidth = 2.1;
//设置边框颜色
button.layer.borderColor = [UIColor lightGrayColor].CGColor;
//设置按钮背景图
UIImage *imageNormal = [UIImage imageNamed:@"camera"];
//设置imageNormal为按钮的正常情况的图片
[button setImage:imageNormal forState:UIControlStateNormal]; UIImage *imageHightLight = [UIImage imageNamed:@"camera2"];
//设置imageHightLight为按钮的高亮情况的图片
[button setImage:imageHightLight forState:UIControlStateHighlighted];
//当button设置了图片的时候 并且没有设置高亮状态下得图片,取消高亮状态, 默认是Yes
button.adjustsImageWhenHighlighted = YES;
[self.window addSubview:button];
5)防止按钮多次点击重复提交数据的实例代码
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonTapped:(UIButton *)button
{
//设置按钮不可点击
button.userInteractionEnabled = NO;
//延迟执行方法 防止按钮被快速点击或者不希望点击造成错误
[self performSelector:@selector(delayMethod:) withObject:button afterDelay:];
} //延迟方法->设置按钮为可点击状态
- (void)delayMethod:(UIButton *)button
{
button.userInteractionEnabled = YES;
}
二、关于UIImageView的介绍
1)概念:UIImageView 是iOS中专门用于展示图片的控件
2)UIImageView 初始化 实例代码
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(, , self.view.frame.size.width, self.view.frame.size.width);
imageView.backgroundColor = [UIColor whiteColor];
imageView.center = self.view.center; //tag设置控件的唯一标识,值不能重复
imageView.tag = ; //UIImageView的 clipsToBounds属性,设置为yes的时候超出部分,不予以显示
imageView.clipsToBounds = YES; //读取一张图片
UIImage *image = [UIImage imageNamed:@"icon"];
imageView.image = image; //设置图片展示模式
imageView.contentMode = UIViewContentModeScaleAspectFill; //打开imageview的用户交互 注:要实现图片点击事件此属性必须设置为YES
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView]; //为UIImageView添加点击事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(imageViewTapped:)];
[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中获取图片的三种方法
方法一:
//把图片对象加载到内存中
UIImage *image1 = [UIImage imageNamed:@"camera"];
CGSize size = image1.size;
NSLog(@"size.w %f size.h %f",size.width ,size.height);
//如果图片的格式是png,则后缀名可以省略,其他格式不能省略
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];
方法三:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"]; UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];