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 初始化实例代码
1UIButton *button =[[UIButton alloc] init];2button.frame = CGRectMake(20,50,50,50);3button.backgroundColor =[UIColor clearColor];4[button setTitle:@"按钮1 正常状态"forState:UIControlStateNormal];5[button setTitle:@"按钮1 高亮状态"forState:UIControlStateHighlighted];6[button setTitle:@"按钮1 选中状态"forState:UIControlStateSelected];78//按钮点击时触发事件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号加粗的字体17button.titleLabel.font = [UIFont boldSystemFontOfSize:35];18//设置圆角19button.layer.cornerRadius =5.f;20//设置边框宽度21button.layer.borderWidth =2.1;22//设置边框颜色23button.layer.borderColor =[UIColor lightGrayColor].CGColor;24//设置按钮背景图25UIImage *imageNormal = [UIImage imageNamed:@"camera"];26//设置imageNormal为按钮的正常情况的图片27[button setImage:imageNormal forState:UIControlStateNormal];2829UIImage *imageHightLight = [UIImage imageNamed:@"camera2"];30//设置imageHightLight为按钮的高亮情况的图片31[button setImage:imageHightLight forState:UIControlStateHighlighted];32//当button设置了图片的时候 并且没有设置高亮状态下得图片,取消高亮状态, 默认是Yes33button.adjustsImageWhenHighlighted =YES;34[self.window addSubview:button];
5)防止按钮多次点击重复提交数据的实例代码
1[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];3- (void)buttonTapped:(UIButton *)button4{5//设置按钮不可点击6button.userInteractionEnabled =NO;8//延迟执行方法 防止按钮被快速点击或者不希望点击造成错误9[self performSelector:@selector(delayMethod:) withObject:button afterDelay:1];11}1213//延迟方法->设置按钮为可点击状态14- (void)delayMethod:(UIButton *)button15{16button.userInteractionEnabled =YES;17}
二、关于UIImageView的介绍
1)概念:UIImageView 是iOS中专门用于展示图片的控件
2)UIImageView 初始化 实例代码
1UIImageView *imageView =[[UIImageView alloc] init];2imageView.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.width);3imageView.backgroundColor =[UIColor whiteColor];4imageView.center =self.view.center;56//tag设置控件的唯一标识,值不能重复7imageView.tag =100;89//UIImageView的 clipsToBounds属性,设置为yes的时候超出部分,不予以显示10imageView.clipsToBounds =YES;1112//读取一张图片13UIImage *image = [UIImage imageNamed:@"icon"];14imageView.image =image;1516//设置图片展示模式17imageView.contentMode =UIViewContentModeScaleaspectFill;1819//打开imageview的用户交互 注:要实现图片点击事件此属性必须设置为YES20imageView.userInteractionEnabled =YES;21[self.view addSubview:imageView];2223//为UIImageView添加点击事件24UITapGestureRecognizer *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//把图片对象加载到内存中2UIImage *image1 = [UIImage imageNamed:@"camera"];3CGSize size =image1.size;4NSLog(@"size.w %f size.h %f",size.width ,size.height);5//如果图片的格式是png,则后缀名可以省略,其他格式不能省略6UIImage *image2 = [UIImage imageNamed:@"icon.jpeg"];
方法二:
//使用场景:读取大图片,比较占内存的,需要及时释放的图片要用这种方法//读取icon.jpegNSString *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];
方法三:
1NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon"ofType:@"jpeg"];23UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];