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

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];
时间: 2024-08-22 00:56:59

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

iOS阶段学习第33天笔记(自定义标签栏(tabBar)介绍)

iOS学习(UI)知识点整理 一.自定义标签栏 1)方法一 单个创建标签栏 1 #import "AppDelegate.h" 2 #import "SecondViewController.h" 3 #import "ViewController.h" 4 #import "ThirdViewController.h" 5 #import "ForthViewController.h" 6 #import

iOS阶段学习第32天笔记(页面传值方法介绍)

iOS学习(UI)知识点整理 一.界面传值方法 1.方法一  Block传值  通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 1)SubViewController.h 文件的代码实现 1 #import <UIKit/UIKit.h> 2 @interface SubViewController : UIViewController 3 @property (nonatomic,copy) void(^callback)(UIColor *colo

IOS 阶段学习第24天笔记(Block的介绍)

IOS学习(OC语言)知识点整理 一.Block 的介绍 1)概念: block 是一种数据类型,类似于C语言中没有名字的函数,可以接收参数,也可以返回值与C函数一样被调用 封装一段代码 可以在任何地方调用 block 也可以作为函数参数,以及函数返回值 2)Block 实例代码 1 //定义了一个block类型MyBlock,MyBlock类型的变量只能指向带两个int的参数和返回int的代码块 2 typedef int (^MyBlock)(int,int); 3 //定义一个函数指针 4

IOS 阶段学习第23天笔记(XML数据格式介绍)

IOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使用比较广泛的两种网络传输数据格式 两者分别占比: 1. json:市场上占90%,轻量级的表示数据 2.xml:占10%,表示数据比较复杂 3)XML三种数据解析方法: 1.DOM解析:将整个xml数据加载到内存中,构造一个对象,从根结点开始一级一级的解析提取数据.缺点:如果数据大, 比较占内存,解

iOS阶段学习第26天笔记(UILabel的介绍)

iOS学习(UI)知识点整理 一.关于UILabel的使用介绍 1)概念:UILabel是一个继承自UIView的用于展示文本信息的控件 2)UI中所有的控件都继承自UIView 即UIView 是UI的祖宗类. 3)UILable的实例化方式 代码: 1 UILabel *label=[[UILabel alloc]init]; //初始化UILabel 2 label.text=@"Hello,KingKong";//给label赋值文本内容 3 label.backgroundC

iOS阶段学习第30天笔记(UIViewController—UINavigationController)

iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构,在UI方便的 具体表现为View加ViewController.所以UIViewController是iOS应用当中非常常用而且很重要的一个类;一般使用都是自己写 一个类继承UIViewController这个类.在UIViewController里面有一个很重要的属性那就是View,也就 是这个C

iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 //分段选择器 4 //在iOS6里,每个段的宽度会根据字数来决定 5 //iOS7以后,每个段的宽度相同,宽度取决于最长的字数 6 self.view.backgroundColor=[UIColor whiteColor]; 7 seg=[[UISegmentedControl all

IOS阶段学习第四天笔记(循环)

    IOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ }  两种 2.单分支 if表达式成立则执行{ }里的语句:双分支 if表达式不成立 则执行else{ }里面的语句 3.字符串的输出系统会从数组地址一直打印到字符 ‘\0’为止,如果没有正确初始化,可能会打印出数组外的信息 4.如果分支结构里面只有一条执行语句可省略外面的大括号{} . 二.开关语句 1.语句结构 :switch (参数) case

IOS阶段学习第三天笔记(运算符)

                                         IOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运算符优先级 括号()> * ,/ ,%  >+,- 4)%表示取余.取模  a%b 表示a除以b取余数 5)整数相除保留两位小数处理方法如: printf(“%.2f”,(float)14/9); 6)自增自减运算符