UI基础3 UIControl

来源:互联网 发布:arduino 单片机 编辑:程序博客网 时间:2024/05/20 08:41

#import"AppDelegate.h"

#import"ViewController.h"

#import"F_Button.h"//自己创建的BUTTON,里面封装着如下方法

//自定义构造方法

- (instancetype)initWithFrame:(CGRect)frame title:(NSString*)title

{

//   ⭐️⭐️⭐️⭐️⭐️⭐️

    self = [super initWithFrame:CGRectMake(0,100, CGRectGetWidth(frame),CGRectGetHeight(frame))];

    if (self) {

       

       UILabel *label = [[UILabel alloc]initWithFrame:frame];

       label.text = title;

        label.textAlignment =NSTextAlignmentCenter;

       [selfaddSubview:label];

       

 }

 

#definekPingmu [UIScreen mainScreen].bounds

//宏定义屏幕的宽和高

#define SCREEN_HEIGHT self.window.frame.size.height//或者CGRectGetHeight([UIScreenmainScreen].bounds)

#define SCREEN_WIDTH self.window.frame.size.width//或者CGRectGetWidth([UIScreenmainScreen].bounds)

 

 

@interfaceAppDelegate ()

 

@end

 

@implementationAppDelegate

/*

 一:

 UIControl:控制类:点击一个视图,触发一个事件,执行某个任务

 UIControl是一个可以带有触发事件的视图

 UIControl的子类:这些视图都可以点击触发事件

 UIButton:按钮

 UISwitch:开关

 UISegmentedControl:分段选择控件

 UISlider:滑杆(例如调节音量)

 UITextField:文本输入控件(如短信文本编辑框)

 UIPageControl:页面控制(如翻页)

 

 UIControl常用属性:

 1.enabled:启用,激活用来设置视图是否可以使用触发事件,默认值是YES ,如果设置为NO,就是禁用这个视图的触发事件;

 2.selected:选中(状态)是否选中此控件,默认值是NO

 3.highlighted:高亮状态(如点中后为高亮),默认为NO

 

 控制事件:

 UIControlEventTouchDown⭐️⭐️⭐️

 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

 UIControlEventTouchDownRepeat

 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

 UIControlEventTouchDragInside⭐️⭐️⭐️

 当一次触摸在控件窗口内拖动时。

 UIControlEventTouchDragOutside

 当一次触摸在控件窗口外拖动时。

 UIControlEventTouchDragEnter

 当一次触摸从控件窗口之外拖动到内部时。

 UIControlEventTouchDragExit

 当一次触摸从控件窗口内部拖动到外部时。

 UIControlEventTouchUpInside⭐️⭐️⭐️

 所有在控件之内触摸并抬起事件。

 UIControlEventTouchUpOutside

 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

 UIControlEventTouchCancel⭐️⭐️

 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

 UIControlEventTouchChanged⭐️⭐️⭐️

 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

 UIControlEventEditingDidBegin⭐️⭐️⭐️

 当文本控件中开始编辑时发送通知。

 UIControlEventEditingChanged⭐️⭐️⭐️

 当文本控件中的文本被改变时发送通知。

 UIControlEventEditingDidEnd⭐️⭐️⭐️

 当文本控件中编辑结束时发送通知。

 UIControlEventEditingDidOnExit⭐️⭐️⭐️

 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

 UIControlEventAlltouchEvents

 通知所有触摸事件。

 UIControlEventAllEditingEvents

 通知所有关于文本编辑的事件。

 UIControlEventAllEvents

 通知所有事件。

 

 ⭐️⭐️⭐️⭐️重要方法:给视图添加响应事件的方法⭐️⭐️⭐️⭐️

 -(void)addTarget:(nullable id)target action:(SEL)actionforControlEvents:(UIControlEvents)controlEvents;

 1.Target:目标需要指定一个目标让其调用方法

 2.action:行动就是让这个目标做事,执行具体的方法

 3.SEL:运行时 @selecter()选择者又叫方法选择器

 4.UIControlEvents:控制事件具体执行这个行动的方式

 

 二:响应事件:

 

 三:封装:封装按钮 1.有提示文字 2.手指点击并抬起触发事件、

 有文字就要用UILabel

 可以点击要用UIControl(父类是UIView,不能直接添加图片文字)

 

 四:按钮UIButton

 

 */

 

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

//    [UIScreen mainScreen].bounds]可用宏定义定义字符串代替

    self.window = [[UIWindowalloc]initWithFrame:kPingmu];

    self.window.rootViewController = [[ViewControlleralloc]init];

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    

    

    UIControl *control = [[UIControlalloc]initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2, (SCREEN_HEIGHT-200),100,40)];

    control.backgroundColor = [UIColorbrownColor];

    

//   control这个对象添加一个按下的响应事件,让目标self去选择login方法执行

    [control addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchDown];

    NSLog(@"%f",CGRectGetHeight([UIScreenmainScreen].bounds));//self.window.frame.size.height

    NSLog(@"%f",CGRectGetWidth([UIScreenmainScreen].bounds));//self.window.frame.size.width

    

    [self.windowaddSubview:control];

    

   F_Button *button = [[F_Buttonalloc]initWithFrame:CGRectMake(0,0,100,40)title:@"登陆"];

    button.backgroundColor = [UIColororangeColor];

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

    [self.windowaddSubview:button];

    

    

                      //UIButton:按钮

//    UIButton:按钮

//    UISwitch:开关

//    UISegmentedControl:分段选择控件

//    UISlider:滑杆(例如调节音量)

//    UITextField:文本输入控件(如短信文本编辑框)

//    UIPageControl:页面控制(如翻页)

    return YES;

}

 

//如果控制类的视图调用此方法,会把当前控制类对象传到这个方法里

//公式: - (void)方法名:(类名 *)参数名

- (void)login:(UIControl*)sender{

    NSLog(@"sender =%@",sender);

    sender.backgroundColor = [UIColorcyanColor];

    [selfshowAlertViewWithMessage:@"登陆成功"];

}

 

//⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️封装弹出框:UILabel3秒消失:定时器⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️

- (void)showAlertViewWithMessage:(NSString *)message{

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 200, 40)];

    label.backgroundColor = [UIColororangeColor];

    label.text = message;

    label.textAlignment =NSTextAlignmentCenter;

    label.adjustsFontSizeToFitWidth =YES;

    label.tag =100;

    label.center =self.window.center;

    [self.windowaddSubview:label];

    [selfperformSelector:@selector(removeAlertView)withObject:nilafterDelay:1];

}

 

- (void)removeAlertView{

//   利用tag找到label的方法,找到的是label的父类,可以不用全局变量来移除

    UILabel *label = [self.windowviewWithTag:100];

    [label removeFromSuperview];

}

0 0