博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS开发之UIButton的介绍
阅读量:6278 次
发布时间:2019-06-22

本文共 6058 字,大约阅读时间需要 20 分钟。

hot3.png

 

#import "AppDelegate.h"

AppDelegate ()

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[UIViewController alloc] init];

    self.window.rootViewController.view.userInteractionEnabled = NO;

    

    

    //=================UIButton===============

    //UIButton:UIControl(都有事件响应机制):UIView

    //1.创建button对象

    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];

    //2.设置背景颜色

    button.backgroundColor = [UIColor greenColor];

    

    //设置tag

    button.tag = 100;

    

    //3.显示在界面上

    [self.window addSubview:button];

    

    //4.给按钮添加事件(核心方法)

    //当指定的事件发生的时候,响应消息的对象会去调用指定的消息

    //参数1:响应消息的对象

    //参数2:消息 (可以不带参,但是如果带参只能有一个参数,并且这个参数的实参就是按钮本身) --必须实现

    

    //参数3:事件

    //UIControlEventTouchUpInside   按下松开

    //UIControlEventTouchDown  按下

    

    //UIControlEventTouchUpInside:当手指按下按钮并且松开的一瞬间,self去调用onclicked:方法

    //UIControlEventTouchDown:当手指按下按钮的瞬间,self去调用onclicked:方法

    [button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchDown];

    

    //====================系统按钮============================

//    UIButtonTypeCustom  自定制类型相当于[[UIButton alloc] init]创建的按钮

//    UIButtonTypeSystem  相当于[[UIButton alloc] init],不能去设置图片和文字(属于淘汰的枚举值)

//    

//    UIButtonTypeDetailDisclosure,  系统详情按钮

//    UIButtonTypeInfoLight,  系统详情按钮

//    UIButtonTypeInfoDark,  系统详情按钮

//    UIButtonTypeContactAdd,  系统添加按钮

//    UIButtonTypeRoundedRect = UIButtonTypeSystem,

    

    //1.创建按钮

    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];

//    //设置背景颜色

    button2.backgroundColor = [UIColor lightGrayColor];

//    //设置frame

    button2.frame = CGRectMake(100, 100, 100, 100);

    //2.显示在界面上

    [_window addSubview:button2];

    

    //3.添加事件

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

    

    

    //================文字按钮====================

    //1.创建按钮对象

    UIButton * button3 = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

    //2.显示在界面上

    [_window addSubview:button3];

    

    //设置背景颜色

    button3.backgroundColor = [UIColor redColor];

    

    //3.给不同状态设置标题(让按钮上显示文字,只能通过这个方法来设置)

    //参数1:标题(按钮上显示的文字)

    //参数2:

//    UIControlStateNormal    正常状态 ,如果只设置了正常状态下的文字,那么其他的状态下文字和正常状态下文字一样

//    UIControlStateHighlighted  高亮状态

//    UIControlStateDisabled   不可点击状态

//    UIControlStateSelected   选中状态

    [button3 setTitle:@"来点我啊" forState:UIControlStateNormal];

    [button3 setTitle:@"还真点啊" forState:UIControlStateHighlighted];

    

    //让按钮不能点击(默认是可以点击的)

//    button3.enabled = NO;

    [button3 setTitle:@"点不了" forState:UIControlStateDisabled];

    

    //按钮成为选中状态(默认是NO)

//    button3.selected = YES;

    [button3 setTitle:@"已经被点了" forState:UIControlStateSelected];

    

    //4.给不同状态设置不一样的文字颜色

    [button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

    [button3 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

    

    

    //5.文字字体

    //titleLabel就是按钮上专门用来显示文字的部分,但是不能通过拿到这个label来设置按钮上的文字和文字颜色,只能设置字体

    button3.titleLabel.font = [UIFont systemFontOfSize:25];

    

    

    //=================图片按钮======================

    //1.创建一个自定制按钮

    UIButton * button4 = [UIButton buttonWithType:UIButtonTypeCustom];

    //2.设置frame

    button4.frame = CGRectMake(100, 280, 80, 80);

    //3.显示在界面上

    [_window addSubview:button4];

    

    //设置按钮背景颜色

    button4.backgroundColor = [UIColor lightGrayColor];

    

    //4.给不同状态设置不同图片

    //参数1:图片

    //参数2:按钮状态

    //设置正常状态下的图片

    [button4 setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];

    //设置高亮状态下的图片

    [button4 setImage:[UIImage imageNamed:@"button_up.png"] forState:UIControlStateHighlighted];

    

    //5.设置图片到按钮边界的边距

    //上、左、下、右

    [button4 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

    //=================图片和文字同时存在================

    //1.创建按钮对象

    UIButton * button5 = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 200, 100)];

    

    //2.添加到界面上

    [_window addSubview:button5];

    

    //4.设置文字(文字颜色默认是白色)

    [button5 setTitle:@"hello" forState:UIControlStateNormal];

    //设置文字颜色

    [button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    

    //3.设置图片

    [button5 setImage:[UIImage imageNamed:@"player_down_1"] forState:UIControlStateNormal];

    

    //5.添加按钮点击事件

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

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    return YES;

}

#pragma mark - 按钮

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

    

    if (btn.tag == 100) {

        

        btn.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0f green:arc4random()%256/255.0f blue:arc4random()%256/255.0f alpha:1];

        

    }

    

    NSLog(@"按钮");

}

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

转载于:https://my.oschina.net/luhoney/blog/654778

你可能感兴趣的文章
CCNP-6 OSPF试验2(BSCI)
查看>>
Excel 2013 全新的图表体验
查看>>
openstack 制作大于2TB根分区自动扩容的CENTOS镜像
查看>>
Unbuntu安装遭遇 vmware上的Easy install模式
查看>>
几个常用的ASP木马
查看>>
python分析postfix邮件日志的状态
查看>>
Mysql-5.6.x多实例配置
查看>>
psutil
查看>>
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>
Linux系统安装VMware Tools
查看>>
asp.net 页面右下角弹出类似QQ或MSN的消息提示
查看>>
游戏开发经常使用算法概述
查看>>
EDM制作要点
查看>>