当前位置: 移动技术网 > IT编程>移动开发>IOS > ios开发UI篇--UIButton

ios开发UI篇--UIButton

2019年03月07日  | 移动技术网IT编程  | 我要评论

概述

  • uibutton 是执行自定义代码以响应用户交互的控件。
  • uibutton 其实包含 uiimageviewuilabel 两个控件,uibutton 继承于 uicontrol,所以有 addtarget 监听事件

属性和方法

初始化button不用alloc init方法,使用便利构造器方法

uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom];

uibutton的类型如下

uibutton类型说明
uibuttontypecustom 没有按钮样式,一般设置该样式,根据需要自定义
uibuttontypesystem 系统样式按钮,例如导航栏和工具栏中显示的按钮。
uibuttontypedetaildisclosure 详细披露按钮。
uibuttontypeinfolight 具有浅色背景的信息按钮。
uibuttontypeinfodark 信息按钮有一个黑暗的背景。
uibuttontypecontactadd 联系人添加按钮。
uibuttontypeplain 没有模糊背景视图的标准系统按钮。
uibuttontyperoundedrect 已经废弃,使用uibuttontypesystem代替

uibutton的状态如下

uibutton的状态说明
uicontrolstatenormal 控件的正常状态或默认状态 - 即已启用但未选中或高亮显示。
uicontrolstatehighlighted 突出显示的控制状态。按钮处于选中状态时的状态
uicontrolstatedisabled 一个控件的禁用状态。
uicontrolstateselected 选择一个控件的状态。
uicontrolstatefocused 集中控制状态。
uicontrolstateapplication 额外的控制状态标志可用于应用程序使用。
uicontrolstatereserved 控制状态标志保留给内部框架使用。

设置frame

[btn setframe:cgrectmake(100, 100, 100, 30)];

设置某一状态下的标题

[btn settitle:@"测试" forstate:(uicontrolstatenormal)];

设置某一状态下的标题颜色

[btn settitlecolor:[uicolor redcolor] forstate:(uicontrolstatenormal)];

设置某一状态下的阴影颜色

[btn settitleshadowcolor:[uicolor purplecolor] forstate:uicontrolstatenormal];

设置某一状态下的背景颜色

[btn setbackgroundcolor:[uicolor blackcolor]];

设置标题字体大小

 btn.titlelabel.font = [uifont systemfontofsize:30];

设置某一状态下的背景图片(背景图片显示在其标题和前景图像后面。)

[btn setbackgroundimage:[uiimage imagenamed:@"登录logo"] forstate:(uicontrolstatenormal)];

设置某一状态下的前景图片

[btn setimage:[uiimage imagenamed:@"验证码"] forstate:(uicontrolstatenormal)];

设置标题内边距

btn.titleedgeinsets = uiedgeinsetsmake(0, 10, 0, 0);   top, left, bottom, right;

 

设置图片内边距

btn.imageedgeinsets = uiedgeinsetsmake(0, -10, 0, 0);

设置内容内边距

btn.contentedgeinsets = uiedgeinsetsmake(10, -30, 10, 10);

标题的阴影改变时,按钮是否高亮显示。默认为no

btn.reversestitleshadowwhenhighlighted = yes;

按钮高亮的情况下,图像的颜色是否要加深一点。默认是yes

btn.adjustsimagewhenhighlighted = yes;

按钮禁用的情况下,图像的颜色是否要加深一点。默认是yes

btn.adjustsimagewhendisabled = yes;

按下按钮是否会发光 默认是no

btn.showstouchwhenhighlighted = no;

返回button 某个状态下的标题

- (nullable nsstring *)titleforstate:(uicontrolstate)state;

返回button 某个状态下的标题颜色

- (nullable uicolor *)titlecolorforstate:(uicontrolstate)state;

返回button 某个状态下的阴影标题颜色

- (nullable uicolor *)titleshadowcolorforstate:(uicontrolstate)state;

返回button 某个状态下的图片

- (nullable uiimage *)imageforstate:(uicontrolstate)state;

返回button 某个状态下的背景图片

- (nullable uiimage *)backgroundimageforstate:(uicontrolstate)state;

返回button 某个状态下的富文本标题

- (nullable nsattributedstring *)attributedtitleforstate:(uicontrolstate)state ns_available_ios(6_0);

获取按钮当前标题

nsstring *title = btn.currenttitle;

获取按钮当前标题颜色

uicolor *color = btn.currenttitlecolor;

获取按钮当前阴影标题颜色

uicolor *shandowcolor = btn.currenttitleshadowcolor;

获取按钮当前按钮内图像

uiimage *image = btn.currentimage;

获取按钮当前标题背景图片

uiimage *backgroundimage = btn.currentbackgroundimage;

获取按钮当前标题富文本

nsattributedstring *astring = btn.currentattributedtitle;

指定背景边界

- (cgrect)backgroundrectforbounds:(cgrect)bounds;

指定内容边界

- (cgrect)contentrectforbounds:(cgrect)bounds;

指定标题边界

- (cgrect)titlerectforcontentrect:(cgrect)contentrect;

指定图片边界

- (cgrect)imagerectforcontentrect:(cgrect)contentrect;

给按钮添加点击事件

[btn addtarget:self action:@selector(action:) forcontrolevents:uicontroleventtouchupinside];

uibutton点击事件如下

uibutton点击事件说明
uicontroleventtouchdown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
uicontroleventtouchdownrepeat 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
uicontroleventtouchdraginside 当一次触摸在控件窗口内拖动时。
uicontroleventtouchdragoutside 当一次触摸在控件窗口之外拖动时。
uicontroleventtouchdragenter 当一次触摸从控件窗口之外拖动到内部时
uicontroleventtouchdragexit 当一次触摸从控件窗口内部拖动到外部时。
uicontroleventtouchupinside 所有在控件之内触摸抬起事件
uicontroleventtouchupoutside 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
uicontroleventtouchcancel 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
uicontroleventvaluechanged 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
uicontroleventeditingdidbegin 当文本控件中开始编辑时发送通知
uicontroleventeditingchanged 当文本控件中的文本被改变时发送通知。
uicontroleventeditingdidend 当文本控件中编辑结束时发送通知。
uicontroleventeditingdidendonexit 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
uicontroleventalltouchevents 通知所有触摸事件
uicontroleventalleditingevents 通知所有关于文本编辑的事件。
uicontroleventapplicationreserved range available for application use
uicontroleventsystemreserved range reserved for internal framework use
uicontroleventallevents 通知所有事件

 


作者:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网