在AppDelegate中
//UIControl:
//觸摸一個視圖去執行某個任務
// 點擊某個視圖出發一個事件
// UIControl:是一個可以帶有觸發事件的視圖
//主要講的內容
// 0.UIControl的子類
// 1.UIControl
// 2.******響應事件
// 3.封裝
// 4.按鈕
// UIControl的子類:
// UIButton 按鈕
// UISwitch 開關
// UISegmentedControl 分段選擇控件
// UISlider 滑桿
// UITextField 文本輸入控件
// UIPageControl 頁面控制(點點)
// 這些視圖都可以點擊觸發一個事件
// 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:運行時@selecter()選擇者(方法選擇器)
// 作用:用來選擇一個方法
// 4.controlEvents:控制事件——>執行行動的方式
/*
UIControlEventTouchDown *****
單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat
多點觸摸按下事件,點觸計數大於1:用戶按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside *****
當一次觸摸在控件窗口內拖動時。
UIControlEventTouchDragOutside
當一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter
當一次觸摸從控件窗口之外拖動到內部時。
UIControlEventTouchDragExit
當一次觸摸從控件窗口內部拖動到外部時。
UIControlEventTouchUpInside *****
所有在控件之內觸摸擡起事件。
UIControlEventTouchUpOutside
所有在控件之外觸摸擡起事件(點觸必須開始與控件內部纔會發送通知)。
UIControlEventTouchCancel ***
所有觸摸取消事件,即一次觸摸因爲放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。
UIControlEventTouchChanged *****
當控件的值發生改變時,發送通知。用於滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。
UIControlEventEditingDidBegin *****
當文本控件中開始編輯時發送通知。
UIControlEventEditingChanged
當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd *****
當文本控件中編輯結束時發送通知。
UIControlEventEditingDidOnExit *****
當文本控件內通過按下回車鍵(或等價行爲)結束編輯時,發送通知。
UIControlEventAlltouchEvents
通知所有觸摸事件。
UIControlEventAllEditingEvents
通知所有關於文本編輯的事件。
UIControlEventAllEvents
通知所有事件。 */
//封裝:
//封裝一個按鈕 1、有提示文字 2、並且可以點擊
//有文字 UILable
//可以點擊 UIControl
//UIButton:是一個按鈕系統封裝好的UIControl
//裏面可以放文字、圖片以及設置文字圖片內容
//同時也可以觸發事件
//1.Button 的初始化有一個類方法
//+ (instancetype)buttonWithType:(UIButtonType)buttonType;
/*
// 定製的
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem //系統默認的樣式NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,//顯示詳細信息的樣式
UIButtonTypeInfoLight,//高亮的
UIButtonTypeInfoDark,//灰暗
UIButtonTypeContactAdd,//+
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
UIControlState:狀態
UIControlStateNormal:普通狀態
UIControlStateHighlighted :高亮狀態
UIControlStateDisabled :取消禁用狀態
UIControlStateSelected:選中狀態
*/
/*
1.封裝一個彈出框這種視圖 用戶可以傳入想顯示的內容以及彈出框的時間
*/
#import "AppDelegate.h"
#import "F_Button.h"
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.windowmakeKeyAndVisible];
F_Button *button = [[F_Buttonalloc] initWithFrame:CGRectMake(0,100, 40, 40) title:@"登陸"];
button.backgroundColor = [UIColorredColor];
button.tag = 101;
[button addTarget:selfaction:@selector(login:)forControlEvents:UIControlEventTouchDown];
[self.windowaddSubview:button];
button.enabled = NO;
// (屏幕的寬-控件的寬)/2 = x
UIControl *control = [[UIControlalloc]initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2, (SCREEN_HEIGHT-200),100, 40)];
// const 修飾符表示當前內容不允許改變
// 給conctol添加一個響應事件 按下
// 讓目標(self)去選擇一個方法(login)去執行
[control addTarget:selfaction:@selector(login:)forControlEvents:UIControlEventTouchDown];
control.backgroundColor = [UIColorgrayColor];
// control.center = self.window.center;//居中
[self.windowaddSubview:control];
UIImage *aImage = [UIImageimageNamed:@"a.png"];
UIImage *bImage = [UIImageimageNamed:@"b.png"];
UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(200, 40, aImage.size.width/5, aImage.size.height/5);
// button1.backgroundColor = [UIColor orangeColor];
// button1.titleLabel:Button上面的lable
// 普通狀態下的標題
// [button1 setTitle:@"登錄" forState:UIControlStateNormal];
// 選中狀態的標題
// [button1 setTitle:@"正在登陸" forState:UIControlStateSelected];
// button1.selected = YES;
// 設置字體顏色
// [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[button1 addTarget:selfaction:@selector(loginTo:)forControlEvents:UIControlEventTouchUpInside ];
// 設置陰影顏色
// - (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state
// 給按鈕設置背景圖片
[button1 setImage:aImageforState:UIControlStateNormal];
[button1 setImage:bImageforState:UIControlStateSelected];
// 設置按鈕的背景圖可以同時顯示上面的標題
// - (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state
// 當點擊的時候有高亮
button1.showsTouchWhenHighlighted =YES;
[self.windowaddSubview:button1];
//imageForState 通過按鈕的狀態找到按上面的圖片
UIImage *getImage = [button1imageForState:UIControlStateSelected];
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100,100, 100, 100)];
imageView.image = getImage;
[self.windowaddSubview:imageView];
// titleForState:獲得按鈕某個狀態的標題
// titleColorForState:獲得按鈕某個狀態的文字顏色
//titleShadowColorForState:獲得按鈕某個狀態的標題陰影顏色
// imageForState:通過按鈕的狀態找到按鈕這個狀態的圖片
// backgroundImageForState:獲得按鈕某個狀態的背景圖片
return YES;
}
- (void)loginTo:(UIButton *)senter{
// 通過設置selected狀態來跟新按鈕上面的標題
senter.selected = senter.selected !=YES ?YES:NO;
}
//如果控制類的視圖調用這個方法 會把當前的控制類對象傳到這個方法裏面
//公式: - (void)方法名:(類名 *)參數名
- (void)login:(UIControl *)sender{
// NSLog(@"登陸成功");
// NSLog(@"%@",sender);
// 如果沒有選中
if (sender.selected !=YES) {
sender.backgroundColor = [UIColorgreenColor];
// 更新選中狀態
sender.selected = YES;
}else{
sender.backgroundColor = [UIColorredColor];
// 更新選中狀態
sender.selected = NO;
}
NSLog(@"%d",sender.selected);
[selfshowAlertViewWithMessage:@"恭喜您!登陸成功"];
}
//封裝一個彈出框->UILable
//3秒之後消失 ->定時器
- (void)showAlertViewWithMessage:(NSString *)message
{
// 禁用Button
F_Button *button = [self.windowviewWithTag:101];
button.enabled = NO;
UILabel *lable = [[UILabelalloc] initWithFrame:CGRectMake(0,0,200, 40)];
lable.backgroundColor = [UIColorredColor];
lable.text = message;
lable.textColor = [UIColorblackColor];
lable.textAlignment =NSTextAlignmentCenter;
lable.adjustsFontSizeToFitWidth =YES;
lable.tag = 100;
lable.center = self.window.center;
[self.windowaddSubview:lable];
[selfperformSelector:@selector(removeAlertView)withObject:nilafterDelay:3];
// [self xxx];等3秒在執行
}
- (void)removeAlertView{
// 啓用Button
F_Button *button = [self.windowviewWithTag:101];
button.enabled = YES;
UILabel *lable = [self.windowviewWithTag:100];
[lable removeFromSuperview];
}
在創建的類的.m
#import "F_Button.h"
@implementation F_Button
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
self = [superinitWithFrame:frame];
if (self) {
self = [selfinitWithFrame:frame];
UILabel *lable = [[UILabelalloc] initWithFrame:CGRectMake(0,0, CGRectGetWidth(frame),CGRectGetHeight(frame)) ];
lable.text = title;
lable.textAlignment = NSTextAlignmentCenter;
[self addSubview:lable];
}
return self;
}
@end
在創建的.h中
#import <UIKit/UIKit.h>
@interface F_Button : UIControl
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title;
@end