当前位置:首页 > 代码 > 正文

ios纯代码button(ios纯代码开发)

admin 发布:2022-12-19 19:34 136


今天给各位分享ios纯代码button的知识,其中也会对ios纯代码开发进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

iOS 怎么用代码自动生产BUTTON

两个for循环

for(int i=0 ; i4 ; i++)

{

for(int i=0 ; i4 ; i++)

{

uibutton * but=[uibutton but....];

but.fream=cgremake(); //自己设置坐标了

[view addsubview but];

}

}

搞定

iOS的UI开发中Button的基本编写方法解析

一、简单说明

一般情况下,点击某个控件后,会做出相应反应的都是按钮

按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置

二、按钮的三种状态

normal(普通状态)

默认情况(Default)

对应的枚举常量:UIControlStateNormal

highlighted(高亮状态)

按钮被按下去的时候(手指还未松开)

对应的枚举常量:UIControlStateHighlighted

disabled(失效状态,不可用状态)

如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击

对应的枚举常量:UIControlStateDisabled

三、注意点

(1)从Xcode5开始,图片资源都放到Images.xcassets中进行管理,可以使用拖拽的方式添加项目中用到的图片到Images.xcassets中

(2)若干多个控件共用一段代码,通常使用tag。

四、代码示例

(1)

复制代码 代码如下:

#import "LFViewController.h"

@interface LFViewController ()

@property (weak, nonatomic) IBOutlet UIButton *headImageView;

@end

@implementation LFViewController

// 在OC中,绝大多数的控件的监听方法的第一个参数就是控件本身

//- (IBAction)left:(UIButton *)button {

//

// NSLog(@"----");

//}

- (IBAction)move

{

// 通过frame修改head的位置

// 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”

// 允许修改“对象”的'“结构体属性”

// 1. 取出结构体属性

CGRect rect = self.headImageView.frame;

// 2. 修改结构体成员

rect.origin.y -= 20;

// 3. 设置对象的结构体属性

self.headImageView.frame = rect;

}

(2)

复制代码 代码如下:

#import "LFViewController.h"

使用git

1. 创建项目时,勾选git

2. 开发告一段落后,选择"Source Control""Commit",并编写注释

// 枚举类型实质上就是一个整数,作用就是用来替代魔法数字

// 枚举类型中,指定了第一个整数之后,后面的数字会递增

typedef enum

kMovingDirTop = 10,

kMovingDirBottom,

kMovingDirLeft,

kMovingDirRight,

} kMovingDir;

#define kMovingDelta 50

@interface LFViewController ()

@property (weak, nonatomic) IBOutlet UIButton *headImageView;

@end

@implementation LFViewController

- (IBAction)move:(UIButton *)button

// CGRect rect = self.headImageView.frame;

CGPoint p = self.headImageView.center;

// magic number魔法数字,其他程序员看到代码的时候,不知道是什么意思

switch (button.tag) {

case kMovingDirTop:

p.y -= kMovingDelta;

break;

case kMovingDirBottom:

p.y += kMovingDelta;

break;

case kMovingDirLeft:

p.x -= kMovingDelta;

break;

case kMovingDirRight:

p.x += kMovingDelta;

break;

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

self.headImageView.center = p;

[UIView commitAnimations];

- (IBAction)zoom:(UIButton *)button

CGRect rect = self.headImageView.bounds;

// 在C语言中,关于bool的判断:非零即真

if (button.tag) {

rect.size.width += 50;

rect.size.height += 50;

rect.size.width -= 50;

rect.size.height -= 50;

// 首尾动画

// beginAnimations表示此后的代码要“参与到”动画中

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:2.0];

self.headImageView.bounds = rect;

// self.headImageView.alpha = 0;

// commitAnimations,将beginAnimation之后的所有动画提交并生成动画

[UIView commitAnimations];

@end

五、补充笔记

1. IBAction的参数

- (IBAction)left:(UIButton *)button

(1) 在OC中,绝大多数的控件监听方法的第一个参数就是控件本身

(2) 默认连线时的参数类型是id

(3) 如果要在监听方法中,方便控件的使用,可以在连线时或者连线后,修改监听方法的参数类型

2. 修改对象的结构体成员

在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”

修改结构体属性的成员方法如下:

(1)使用临时变量记录对象的结构体属性

(2) 修改临时变量的属性

(3)将临时变量重新设置给对象的结构体属性

3. 在程序开发中需要避免出现魔法数字(Magic Number)

使用枚举类型,可以避免在程序中出现魔法数字

(1)枚举类型实质上就是一个整数,其作用就是用来替代魔法数字

(2)枚举类型中,指定了第一个整数之后,后面的数字会递增

4. frame bounds center

1 frame可以修改对象的位置和尺寸

2 bounds可以修改对象的尺寸

3 center可以修改对象的位置

5. 首尾式动画

复制代码 代码如下:

// beginAnimations表示此后的代码要“参与到”动画中

[UIView beginAnimations:nil context:nil];

// setAnimationDuration用来指定动画持续时间

[UIView setAnimationDuration:2.0];

self.headImageView.bounds = rect;

......

// commitAnimations,将beginAnimation之后的所有动画提交并生成动画

[UIView commitAnimations];

下面来罗列一下UIButton的基本属性罗列

第一、UIButton的定义

复制代码 代码如下:

UIButton *button=[[UIButton buttonWithType:(UIButtonType);

能够定义的button类型有以下6种,

复制代码 代码如下:

typedef enum {

UIButtonTypeCustom = 0, 自定义风格

UIButtonTypeRoundedRect, 圆角矩形

UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

UIButtonTypeInfoLight, 亮色感叹号

UIButtonTypeInfoDark, 暗色感叹号

UIButtonTypeContactAdd, 十字加号按钮

}UIButtonType;

第二、设置frame

复制代码 代码如下:

button1.frame = CGRectMake(20, 20, 280, 40);

[button setFrame:CGRectMake(20,20,50,50)];

第三、button背景色

复制代码 代码如下:

button1.backgroundColor = [UIColor clearColor];

[button setBackgroundColor:[UIColor blueColor]];

第四、state状态

forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

复制代码 代码如下:

enum {

UIControlStateNormal = 0, 常规状态显现

UIControlStateHighlighted = 1 0, 高亮状态显现

UIControlStateDisabled = 1 1, 禁用的状态才会显现

UIControlStateSelected = 1 2, 选中状态

UIControlStateApplication = 0x00FF0000, 当应用程序标志时

UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他

@property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently

@property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application

@property(nonatomic,getter=isHighlighted)BOOL highlighted;

第五 、设置button填充图片和背景图片

复制代码 代码如下:

[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

第六、设置button标题和标题颜色

复制代码 代码如下:

[button1 setTitle:@"点击" forState:UIControlStateNormal];

[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

第七、设置按钮按下会发光

复制代码 代码如下:

button.showsTouchWhenHighlighted=NO;

第八、添加或删除事件处理

复制代码 代码如下:

[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

第九、 设置按钮内部图片间距和标题间距

复制代码 代码如下:

UIEdgeInsets insets; // 设置按钮内部图片间距

insets.top = insets.bottom = insets.right = insets.left = 10;

bt.contentEdgeInsets = insets;

bt.titleEdgeInsets = insets; // 标题间距

ios中捏合手势的使用,对一个view进行捏合,怎么使这个view上的button不跟着捏合手势进行缩放呢?纯代码

在大多数情况下,虚拟设备是使用鼠标和键盘控制的,一些操作已经能用快捷键实现。 触摸手势说明: (1)右键按住+移动鼠标向左=放大; (2)右键按住+移动鼠标向右=缩小; (3)右键按住+移动鼠标向上=前倾; (4)右键按住+移动鼠标向下=后倾

ios中怎么用代码添加button

button.hidden = YES; button.hidden = NO; [button setHidden:YES]; [button setHidden:NO]; 以上均可

IOS开发UIButton基础&事件

本节学习内容:

1.UIButton的控件基本概念

2.UIButton的创建方法

3.UIButton的类型

4.可显示图片的UIButton

【viewController.m】

-(void)createUIRectButton{

//创建一个btn对象,根据类型来创建button

//圆角类型btn:UIButtonTypeRoundedRect

//通过类方法来创建ButtonWithType:类名+方法

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//设置button按钮的位置

btn.frame=CGRectMacke(100,100,100,40);

//设置按钮的文字内容

// @parameter p1:字符串类型,显示到按钮上的文字,

//P2:设置文字显示的状态类型:UICountrolStateNormal,正常状态

btn setTitle:@"按钮01" forState:UICountrolStateNormal];

//P1显示文字

//P2显示状态:UICountrolStateHighLighted

btn setTitle:@"按钮按下" forState:UICountrolStateHighLighted];

//设置button背景颜色

btn.backgroundColor=[UIColor grayColor];

//设置文字颜色

//p1 颜色,p2状态

[btn setTitleColor:[UIColor redColor] forState:UIControlStaNromal];

//设置按下状态的颜色

[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHiglighted];

//设置按钮风格颜色

[btn setTintColor:[UIColor whitecolor]];

//注:setTitleColor 优先级高于setTintColor

//titleLabel:UILabel空控

btn,titleLable.font=[UIFount systemFontOfSize:12];

//添加到试图中并显示

[self.view addSubview:btn]

}

//创建一个可以显示图片btn

-(void)createImageBtn{

//创建一个自定义类型的btn

UIButton *btnImage=[Button vuttonWithType:UIButtonTypeCustom];

//图片Btn位置

btnImage.frame=CGRectMake(100,200,100,100);

//默认图片

UIImage *icon01=[UIImage imageNamed:@"btn02.jpg"];

//点击后的图片

UIImage *icon02=[UIImage imageNamed:@"btn03.jpg"];

//设置按钮图片方法设置 p1:显示的图片对象,p1控件状态

[btnImage setImage:icon01 forState:UIControlStateNormal];

[btnImage setImage:icon02 forState:UIControlStateHighlighted]; 

[self.view addSubview:btnImage];

}

【UIButton事件】

本节学习内容:

1.UIButton的事件的概念

2.UIButton的添加方法

3.UIButton的响应函数

4.多按钮使用同一事件函数

【viewController.h】

-(void)createBtn{

//创建圆角按钮

UIButton *btn=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn.fram=CGRectMake(100,100,80,40);

[btn setTitle:@"按钮" froState:UIControlStateNormal];

//给按钮事件函数

//参数P1:谁来实现事件函数,实现者对像都就是“谁|"

//参数P2:@selector(pressBtn)函数对像,当按钮满足P3事件类型时,庙用函数

//参数p3:UIControlEvent:事件处理函数类型

//UIControlEventTouchUpinside:当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数

//UIControlEventTouchDowninside:当手指触摸屏幕上触发

/*

调用不带参数事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

/*

调用不带参数事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

//带参数事件

btn addTarget:self acion:@selector(pressBtn:)forCountrolEvents:UIControlEventTouchUpinside];

//确摸时调用事件函数

[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown]

[self.veiw addSubView:Btn];

UIButton *btn02=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn02.fram=CGRectMake(100,200,80,40);

[btn02 setTitle:@"按钮" froState:UIControlStateNormal];

//可以多个按钮使用同一个事函数来处理不同按钮事件

[btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchDown];

[self.veiw addSubView:Btn02];

//设置按钮标记值

btn.tag=101;

btn02.tag=102;

-(void)pressBtn02{

if(btn.tag==101){

NSLog(@"btn 01 pressed ");

}

if(btn.tag==102){

NSLog(@"btn 02 pressed");

}

-(void)touchDown

{

NSLog(@"按钮被触摸!");

}

-(void)pressBtn{

NSLog(@"按钮被按了一下!");

}

//参数为按钮本身

-(void)pressBtn:(UIButton*)btn{

NSLog(@"tbn pressed");

}

ios开发button点击弹出提示,代码怎么写?

按钮代码

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[myButton setTitle:@"求最佳哦~" forState:UIControlStateNormal];

[myButton setTitle:@"可以松手~" forState:UIControlStateHighlighted];

[myButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];

myButton.backgroundColor = [UIColor yellowColor];

myButton.bounds = CGRectMake(0, 0, 200, 100);

myButton.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

[self.view addSubview:myButton];

按钮响事件函数

-(void)myButton:(UIButton *)sender{

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮点击提示" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

[myAlertView show];

}

不懂的追问

关于ios纯代码button和ios纯代码开发的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/20714.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载