UIButton的简单使用
2015-12-03 17:28
417 查看
UIControl :
控制类
,点击一个视图,执行某个任务,点击某个视图,触发某个事件;是一个带有触发事件的
视图
主要内容:
0、UIControl
的
子类
UIButton //
按钮
UISwitch //
开 关
UISegmentedControl //分段选择控件
UISlider //滑竿(类似于调节音量的)
UITextField //文本输入控件
UIPageControl //翻页控件
这些视图都可以点击触发事件
1、UIControl
2、响应事件
3、封装
4、按钮
UIControl常用属性
1、enabled
:启用
激活
用来设置视图是否可以使用,默认值是YES,如果设置成NO,就是禁用这个视图的触发事件
2、selected:选中(状态)
是不是选中了这个控件,默认值是NO
3、highlighted
:高亮(状态)
默认值是NO
重要方法:
给视图添加响应事件的方法
-(void)addTarget:(nullable
id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
1、Target
:目标->让谁取调用一个方法
2、action
:行动->
要让这个目标
做什么事
3、SEL
:运行时
-> @selectot()
选择者,方法选择器:用来选择一个方法
4、ControlEvents:控制事件
->
执行行动的方式
UIControlEventTouchDown//单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat//多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside//当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside//当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter//当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit//当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside//所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside//所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel//所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged//当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin//当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged//当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd//当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit//当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents//通知所有触摸事件。
UIControlEventAllEditingEvents//通知所有关于文本编辑的事件。
UIControlEventAllEvents//通知所有事件。
//
// AppDelegate.m
// login
//
// Created by Treney on 15/12/1.
// Copyright © 2015年 Treney. All rights reserved.
//
#import "AppDelegate.h"
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//定义宏定义 数字尽量用大写
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)
@interface AppDelegate ()
{
UILabel *labelTest;
UIImageView *imgView;
NSMutableArray *imageList;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
imageList = [[NSMutableArray alloc]init];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"12.jpg"]];//设置背景颜色
#pragma mark - 背景图
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 570)];//实例化视图
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];//设置视图背景颜色
[self.window addSubview:view];//添加到窗体
#pragma mark - copyright
UILabel *lable = [[UILabel alloc]init];//实例化lable对象
lable.frame = CGRectMake(0,500, 320, 50);//设置实例化对象的大小和坐标
lable.text=@"Copyright © Treney 2015年 All rights reserved.";//设置显示字
lable.textColor = [UIColor blackColor];//设置字体颜色
lable.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
lable.font = [UIFont boldSystemFontOfSize:12];//设置字体加粗 既能加粗 又能调大小
lable.numberOfLines = 0;//设置文本行数 代表自适应行数 非0 是几就是几行
[self.window addSubview:lable];
#pragma mark - 登陆名 框
UILabel *login = [[UILabel alloc]init];//实例化lable对象
login.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
login.frame = CGRectMake(60,350,72, 20);//设置实例化对象的大小和坐标
login.text=@"登陆名:";//设置显示字
login.textColor = [UIColor whiteColor];//设置字体颜色
login.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
login.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
login.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
[self.window addSubview:login];
login.layer.cornerRadius = 5.5;//设置弧度
login.layer.masksToBounds = YES;
UILabel *login_1 = [[UILabel alloc]init];//实例化lable对象
login_1.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
login_1.frame = CGRectMake(130,350,150, 20);//设置实例化对象的大小和坐标
login_1.alpha =0.3;//设置透明度
login_1.text=@"请输入账号:";//设置显示字
login_1.textColor = [UIColor whiteColor];//设置字体颜色
login_1.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
login_1.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
login_1.layer.cornerRadius = 5.5;//设置弧度
login_1.layer.masksToBounds = YES;
[self.window addSubview:login_1];
#pragma mark - 密码框
UILabel *password = [[UILabel alloc]init];//实例化lable对象
password.frame = CGRectMake(60,400, 72, 20);//设置实例化对象的大小和坐标
password.backgroundColor = [UIColor darkGrayColor];
password.text=@"密 码:";//设置显示字
password.textColor = [UIColor whiteColor];//设置字体颜色
password.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
password.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
password.numberOfLines = 0;//设置文本行数 代表自适应行数 非0 是几就是几行
password.layer.cornerRadius = 5.5;//设置弧度
password.layer.masksToBounds = YES;
[self.window addSubview:password];
UILabel *passwod_1 = [[UILabel alloc]init];//实例化lable对象
passwod_1.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
passwod_1.frame = CGRectMake(130,400,150, 20);//设置实例化对象的大小和坐标
passwod_1.alpha =0.3;//设置透明度
passwod_1.text=@"请输入密码:";//设置显示字
passwod_1.textColor = [UIColor whiteColor];//设置字体颜色
passwod_1.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
passwod_1.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
passwod_1.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
passwod_1.layer.cornerRadius = 5.5;//设置弧度
passwod_1.layer.masksToBounds = YES;
[self.window addSubview:passwod_1];
#pragma mark - button
labelTest = [[UILabel alloc]init];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(70,450, 50, 20)];
// 设置背景颜色
[button setBackgroundColor:color];//设置背景颜色
// 设置Button上面显示的问题
[button setTitle:@"登陆" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:18];
[button addTarget:self action:@selector(onLoginClick) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
[button setShowsTouchWhenHighlighted:YES];//设置高亮
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(140, 450, 50, 20)];
// 设置背景颜色
[button1 setBackgroundColor:color];//设置背景颜色
[button1 setTitle:@"点我" forState:UIControlStateNormal];
[button1 setShowsTouchWhenHighlighted:YES];//设置高亮
[button1 addTarget:self action:@selector(Test) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button1.titleLabel.font = [UIFont systemFontOfSize:18];
UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(210, 450, 50, 20)];
// 设置背景颜色
[button2 setBackgroundColor:color];//设置背景颜色
[button2 setTitle:@"注册" forState:UIControlStateNormal];
[button2 setShowsTouchWhenHighlighted:YES];//设置高亮
[button2 addTarget:self action:@selector(onPassClick) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button2.titleLabel.font = [UIFont systemFontOfSize:18];
//img button
UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(150, 250, 50, 20)];
// 设置背景颜色
button3.backgroundColor = [UIColor clearColor];//设置背景颜色
[button3 setTitle:@"image" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button3.titleLabel.font = [UIFont systemFontOfSize:18];
//GIF视图
imgView = [[UIImageView alloc]init];
imgView.frame = CGRectMake(0, 150, 320, 210);//设置坐标
imgView.animationDuration = 0.8; //anmiationDuration 设置每张图的的时间间隔
imgView.animationRepeatCount =0 ;//设置图片重复计数
[self.window addSubview:imgView];//添加到窗口
button.layer.cornerRadius = 5.5;//设置弧度
button.layer.masksToBounds = YES;
button1.layer.cornerRadius = 5.5;
button1.layer.masksToBounds = YES;
button2.layer.cornerRadius = 5.5;//设置弧度
button2.layer.masksToBounds = YES;
[view addSubview:button];
[view addSubview:button1];
[view addSubview:button2];
[view addSubview:button3];
#pragma mark - 清除button
UIButton *cleanButton = [[UIButton alloc]initWithFrame:CGRectMake(92.5, 520.5, 50, 10)];
[cleanButton setTitle:@"Treney" forState:UIControlStateNormal];
cleanButton.backgroundColor = [UIColor clearColor];//设置背景颜色
[cleanButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
cleanButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];//设置字体加粗 既能加粗 又能调大小
[cleanButton addTarget:self action:@selector(onCleanButton) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
[self.window addSubview:cleanButton];
return YES;
}
#pragma mark - 响应事件
-(void)onPassClick{
labelTest.frame = CGRectMake(0,230, 320, 50);//设置实例化对象的大小和坐标
labelTest.text=@"你还是猪
控制类
,点击一个视图,执行某个任务,点击某个视图,触发某个事件;是一个带有触发事件的
视图
主要内容:
0、UIControl
的
子类
UIButton //
按钮
UISwitch //
开 关
UISegmentedControl //分段选择控件
UISlider //滑竿(类似于调节音量的)
UITextField //文本输入控件
UIPageControl //翻页控件
这些视图都可以点击触发事件
1、UIControl
2、响应事件
3、封装
4、按钮
UIControl常用属性
1、enabled
:启用
激活
用来设置视图是否可以使用,默认值是YES,如果设置成NO,就是禁用这个视图的触发事件
2、selected:选中(状态)
是不是选中了这个控件,默认值是NO
3、highlighted
:高亮(状态)
默认值是NO
重要方法:
给视图添加响应事件的方法
-(void)addTarget:(nullable
id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
1、Target
:目标->让谁取调用一个方法
2、action
:行动->
要让这个目标
做什么事
3、SEL
:运行时
-> @selectot()
选择者,方法选择器:用来选择一个方法
4、ControlEvents:控制事件
->
执行行动的方式
UIControlEventTouchDown//单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat//多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside//当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside//当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter//当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit//当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside//所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside//所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel//所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged//当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin//当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged//当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd//当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit//当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents//通知所有触摸事件。
UIControlEventAllEditingEvents//通知所有关于文本编辑的事件。
UIControlEventAllEvents//通知所有事件。
//
// AppDelegate.m
// login
//
// Created by Treney on 15/12/1.
// Copyright © 2015年 Treney. All rights reserved.
//
#import "AppDelegate.h"
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//定义宏定义 数字尽量用大写
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)
@interface AppDelegate ()
{
UILabel *labelTest;
UIImageView *imgView;
NSMutableArray *imageList;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
imageList = [[NSMutableArray alloc]init];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"12.jpg"]];//设置背景颜色
#pragma mark - 背景图
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 570)];//实例化视图
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];//设置视图背景颜色
[self.window addSubview:view];//添加到窗体
#pragma mark - copyright
UILabel *lable = [[UILabel alloc]init];//实例化lable对象
lable.frame = CGRectMake(0,500, 320, 50);//设置实例化对象的大小和坐标
lable.text=@"Copyright © Treney 2015年 All rights reserved.";//设置显示字
lable.textColor = [UIColor blackColor];//设置字体颜色
lable.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
lable.font = [UIFont boldSystemFontOfSize:12];//设置字体加粗 既能加粗 又能调大小
lable.numberOfLines = 0;//设置文本行数 代表自适应行数 非0 是几就是几行
[self.window addSubview:lable];
#pragma mark - 登陆名 框
UILabel *login = [[UILabel alloc]init];//实例化lable对象
login.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
login.frame = CGRectMake(60,350,72, 20);//设置实例化对象的大小和坐标
login.text=@"登陆名:";//设置显示字
login.textColor = [UIColor whiteColor];//设置字体颜色
login.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
login.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
login.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
[self.window addSubview:login];
login.layer.cornerRadius = 5.5;//设置弧度
login.layer.masksToBounds = YES;
UILabel *login_1 = [[UILabel alloc]init];//实例化lable对象
login_1.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
login_1.frame = CGRectMake(130,350,150, 20);//设置实例化对象的大小和坐标
login_1.alpha =0.3;//设置透明度
login_1.text=@"请输入账号:";//设置显示字
login_1.textColor = [UIColor whiteColor];//设置字体颜色
login_1.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
login_1.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
login_1.layer.cornerRadius = 5.5;//设置弧度
login_1.layer.masksToBounds = YES;
[self.window addSubview:login_1];
#pragma mark - 密码框
UILabel *password = [[UILabel alloc]init];//实例化lable对象
password.frame = CGRectMake(60,400, 72, 20);//设置实例化对象的大小和坐标
password.backgroundColor = [UIColor darkGrayColor];
password.text=@"密 码:";//设置显示字
password.textColor = [UIColor whiteColor];//设置字体颜色
password.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
password.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
password.numberOfLines = 0;//设置文本行数 代表自适应行数 非0 是几就是几行
password.layer.cornerRadius = 5.5;//设置弧度
password.layer.masksToBounds = YES;
[self.window addSubview:password];
UILabel *passwod_1 = [[UILabel alloc]init];//实例化lable对象
passwod_1.backgroundColor = [UIColor darkGrayColor];//设置背景颜色
passwod_1.frame = CGRectMake(130,400,150, 20);//设置实例化对象的大小和坐标
passwod_1.alpha =0.3;//设置透明度
passwod_1.text=@"请输入密码:";//设置显示字
passwod_1.textColor = [UIColor whiteColor];//设置字体颜色
passwod_1.textAlignment = NSTextAlignmentCenter;//设置对齐方式(alignment对齐方式)
passwod_1.font = [UIFont boldSystemFontOfSize:18];//设置字体加粗 既能加粗 又能调大小
passwod_1.numberOfLines = 1;//设置文本行数 代表自适应行数 非0 是几就是几行
passwod_1.layer.cornerRadius = 5.5;//设置弧度
passwod_1.layer.masksToBounds = YES;
[self.window addSubview:passwod_1];
#pragma mark - button
labelTest = [[UILabel alloc]init];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(70,450, 50, 20)];
// 设置背景颜色
[button setBackgroundColor:color];//设置背景颜色
// 设置Button上面显示的问题
[button setTitle:@"登陆" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:18];
[button addTarget:self action:@selector(onLoginClick) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
[button setShowsTouchWhenHighlighted:YES];//设置高亮
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(140, 450, 50, 20)];
// 设置背景颜色
[button1 setBackgroundColor:color];//设置背景颜色
[button1 setTitle:@"点我" forState:UIControlStateNormal];
[button1 setShowsTouchWhenHighlighted:YES];//设置高亮
[button1 addTarget:self action:@selector(Test) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button1.titleLabel.font = [UIFont systemFontOfSize:18];
UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(210, 450, 50, 20)];
// 设置背景颜色
[button2 setBackgroundColor:color];//设置背景颜色
[button2 setTitle:@"注册" forState:UIControlStateNormal];
[button2 setShowsTouchWhenHighlighted:YES];//设置高亮
[button2 addTarget:self action:@selector(onPassClick) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button2.titleLabel.font = [UIFont systemFontOfSize:18];
//img button
UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(150, 250, 50, 20)];
// 设置背景颜色
button3.backgroundColor = [UIColor clearColor];//设置背景颜色
[button3 setTitle:@"image" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button3.titleLabel.font = [UIFont systemFontOfSize:18];
//GIF视图
imgView = [[UIImageView alloc]init];
imgView.frame = CGRectMake(0, 150, 320, 210);//设置坐标
imgView.animationDuration = 0.8; //anmiationDuration 设置每张图的的时间间隔
imgView.animationRepeatCount =0 ;//设置图片重复计数
[self.window addSubview:imgView];//添加到窗口
button.layer.cornerRadius = 5.5;//设置弧度
button.layer.masksToBounds = YES;
button1.layer.cornerRadius = 5.5;
button1.layer.masksToBounds = YES;
button2.layer.cornerRadius = 5.5;//设置弧度
button2.layer.masksToBounds = YES;
[view addSubview:button];
[view addSubview:button1];
[view addSubview:button2];
[view addSubview:button3];
#pragma mark - 清除button
UIButton *cleanButton = [[UIButton alloc]initWithFrame:CGRectMake(92.5, 520.5, 50, 10)];
[cleanButton setTitle:@"Treney" forState:UIControlStateNormal];
cleanButton.backgroundColor = [UIColor clearColor];//设置背景颜色
[cleanButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
cleanButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];//设置字体加粗 既能加粗 又能调大小
[cleanButton addTarget:self action:@selector(onCleanButton) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
[self.window addSubview:cleanButton];
return YES;
}
#pragma mark - 响应事件
-(void)onPassClick{
labelTest.frame = CGRectMake(0,230, 320, 50);//设置实例化对象的大小和坐标
labelTest.text=@"你还是猪
相关文章推荐
- android开发游记:timesquere时间瀑布
- SequenceInputStream
- SequenceInputStream
- IOS-- UIView 基本属性用法
- NSData和UIImage之间的转换
- iOS UITabBarController 典型应用
- IOS--UILable
- UIImagePickerView的应用 (李梦珂)
- IOS--UIButton
- IOS--UIProcessView
- Design Pattern ——Builder
- IOS--UISlider 滑块控件
- UItextfield 限制输入字数 奔溃解决的新方法
- IOS--UISwitch 开关控件
- Android AsyncTask(异步耗时 更新UI)
- Libgdx学习笔记:UI之技能冷却按钮
- IOS控件UITableView详解
- UITextField 总结
- iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)
- 选择移动web开发框架研究——有mui、frozenui以及Sencha Touch等