20150630_UI之按钮UIButton使用

来源:互联网 发布:自定义oid取不到数据 编辑:程序博客网 时间:2024/06/10 05:10

UIButton的使用从下面的实例来说明,代码按顺序可以一步一步的添加,运行看效果.

////  MyClass.m//  IOS150630_UI(01)_UIButton////  Created by PengJunlong on 15/6/30.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "MyClass.h"@implementation MyClass- (void)btnClicked{    NSLog(@"MyClass中的按钮被点击");}@end

////  AppDelegate.m//  IOS150630_UI(01)_UIButton////  Created by PengJunlong on 15/6/30.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "AppDelegate.h"#import "MyClass.h"@interface AppDelegate (){    MyClass *_myClass;}@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //UIButton    //按钮    //通过工厂方法创建Button对象    //以下是button的类型,显示什么形状的button    //UIButtonTypeCustom = 0,                         // no button type    //UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button,矩形    //UIButtonTypeDetailDisclosure, //有个i标志    //UIButtonTypeInfoLight,    //信息按钮有一个浅色的背景    //UIButtonTypeInfoDark,     //信息按钮有一个深色的背景    //UIButtonTypeContactAdd,   //加号按钮    //UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem(IOS7之后) instead    UIButton *buttonSys = [UIButton buttonWithType:UIButtonTypeSystem];    buttonSys.frame = CGRectMake(20, 100, self.window.frame.size.width-40, 50);    buttonSys.backgroundColor = [UIColor grayColor];    //设置button标题    //UIControlStateNormal       = 0,                       //正常状态,没有被点击时的状态    //UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set    //UIControlStateDisabled     = 1 << 1,    //UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)    //UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use    //UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use    [buttonSys setTitle:@"按钮1" forState:UIControlStateNormal];    buttonSys.tag = 1;    //设置高亮状态,按钮被点击不放    [buttonSys setTitle:@"按钮1被点击" forState:UIControlStateHighlighted];    [buttonSys setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];//设置按钮标题颜色    //修改字体大小    //buttonSys.font = [UIFont systemFontOfSize:25];//修改字体大小,font在button已经不建议使用了,使用titleLabel替代    buttonSys.titleLabel.font = [UIFont systemFontOfSize:25];    //在高亮状态下显示触摸两点    buttonSys.showsTouchWhenHighlighted = YES;    [buttonSys addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];    button2.frame = CGRectMake(20, 200, self.window.frame.size.width-40, 50);    button2.backgroundColor = [UIColor blueColor];    [button2 setTitle:@"按钮二" forState:UIControlStateNormal];    [button2 setTitle:@"按钮二被点击" forState:UIControlStateHighlighted];        [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];    //设置button的tag属性值,可以区分是哪个按钮点击    button2.tag = 2;    //按钮添加点击事件    //- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;    //target:执行对象    //action:触发对象中的要执行的方法,这是一个选择器    //controlEvents:触发事件,何时触发    /*     typedef NS_OPTIONS(NSUInteger, UIControlEvents) {        UIControlEventTouchDown           = 1 <<  0,      // on all touch downs        UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)        UIControlEventTouchDragInside     = 1 <<  2,        UIControlEventTouchDragOutside    = 1 <<  3,        UIControlEventTouchDragEnter      = 1 <<  4,        UIControlEventTouchDragExit       = 1 <<  5,        UIControlEventTouchUpInside       = 1 <<  6,        UIControlEventTouchUpOutside      = 1 <<  7,        UIControlEventTouchCancel         = 1 <<  8,                UIControlEventValueChanged        = 1 << 12,     // sliders, etc.                UIControlEventEditingDidBegin     = 1 << 16,     // UITextField        UIControlEventEditingChanged      = 1 << 17,        UIControlEventEditingDidEnd       = 1 << 18,        UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing                UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events        UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField        UIControlEventApplicationReserved = 0x0F000000,  // range available for application use        UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use        UIControlEventAllEvents           = 0xFFFFFFFF    };     */    [button2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];    //_myClass = [[MyClass alloc] init];//_myClass必须是全局的变量,不能是局部变量,否则离开这个方法后对象就被释放了    //[button2 addTarget:_myClass action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeContactAdd];    button3.frame = CGRectMake(20, 300, self.window.frame.size.width-40, 50);    button3.backgroundColor = [UIColor cyanColor];    button3.tag = 3;    [button3 setTitle:@"按钮三" forState:UIControlStateNormal];    [button3 setTitle:@"按钮三被点击" forState:UIControlStateHighlighted];    [button3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];        //自定义按钮    UIButton *cumButton = [UIButton buttonWithType:UIButtonTypeCustom];    cumButton.frame = CGRectMake(20, 400, self.window.frame.size.width-40, 50);    cumButton.backgroundColor = [UIColor yellowColor];    [cumButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [cumButton setTitle:@"自定义按钮" forState:UIControlStateNormal];    [cumButton setTitle:@"自定义按钮被点击" forState:UIControlStateHighlighted];    [cumButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];    cumButton.tag = 4;    [cumButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];        //设置图片    //设置标题图片    [cumButton setImage:[UIImage imageNamed:@"front.png"] forState:UIControlStateNormal];    //设置背景图片    //设置了背景图片后,再设置背景颜色不管用    [cumButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];    //设置是否高亮状态下背景图片是否变暗    cumButton.adjustsImageWhenHighlighted = NO;        //创建一个圆角button    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];    button4.frame = CGRectMake(20, 500, self.window.frame.size.width-40, 50);    button4.backgroundColor = [UIColor yellowColor];    button4.layer.cornerRadius = 20;    //设置圆角半径    [button4 setTitle:@"圆角按钮" forState:UIControlStateNormal];        self.window.backgroundColor = [UIColor whiteColor];    [self.window addSubview:buttonSys];    [self.window addSubview:button2];    [self.window addSubview:button3];    [self.window addSubview:cumButton];    [self.window addSubview:button4];    self.window.rootViewController = nil;    return YES;}- (void)btnClicked:(UIButton *)btn{    NSLog(@"----按钮%@被点击了----",btn.currentTitle);}- (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
图像资源可以自己找

0 0
原创粉丝点击