您的位置:首页 > 产品设计 > UI/UE

IOS开发之UIButton的介绍

2016-04-07 00:00 190 查看
摘要: IOS开发之UIButton的介绍
#import "AppDelegate.h"@interface  AppDelegate ()@end@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];//    //设置framebutton2.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.设置framebutton4.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
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIButton