按鈕通常使用 Touch Up Inside 事件來體現(xiàn),能夠抓取用戶用手指按下并在該按鈕上松開發(fā)生的事件棘伴。
當(dāng)檢測到事件后寞埠,便可能觸發(fā)相應(yīng)視圖控件中的操作(IBAction)
例子:1. 分享給大家
點擊 選中狀態(tài)打勾。再次點擊 取消打鉤
/**
* 初始化最后一個imageView
* @param imageView 最后一個imageView
*/
-(void)setupLastImageView:(UIImageView *)imageView
{
//1. 分享給大家(checkbox)
【 按鈕的類型只能在初始化的時候設(shè)置】
//UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom]; 兩者一樣
[shareButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[shareButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
[startBtn setImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
startBtn.size = startBtn.currentBackgroundImage.size ;
shareButton.width = 100;
shareButton.height = 30;
shareButton.CenterX = imageView.CenterX;
shareButton.CenterY = imageView.height * 0.65;
//設(shè)置圖片和按鈕
[shareButton setTitle:@"分享給大家" forState:UIControlStateNormal];
[shareButton setTitleColor: [UIColor blackColor ] forState:UIControlStateNormal];
shareButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
// addTarget : 【讓誰去做事情】焊夸, action : 方法 : 【做什么事情】 事件:
[shareButton addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
}
//分享按鈕點擊
-(void)shareClick:(UIButton *)shareBtn
{
shareBtn.selected = !shareBtn.isSelected; // 點擊按鈕取反
}
##例子:2. 九宮格
// 中間的標題按鈕 【】
//UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom]; 兩者一樣
UIButton *titleButton = [[UIButton alloc]init];
titleButton.width = 150;
titleButton.height = 20;
//設(shè)置圖片和按鈕
[titleButton setTitle:@"首頁" forState:UIControlStateNormal];
[titleButton setTitleColor: [UIColor blackColor ] forState:UIControlStateNormal];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateSelected];
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 70, 0, 0);
titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
//創(chuàng)建一個按鈕 初始化按鈕的frame
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50,50,50,50)];
//創(chuàng)建一個圓角矩形的按鈕
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// 點屬性 set 方法
// 裁剪多余部分 超出范圍
self.showCarView. clipsToBounds = YES ;
// 設(shè)置背景色
button.backgroundColor =[UIColor whiteColor];
// 字體大小
button.titleLabel.font = [UIFont systemFontOfSize:14];
// 在view上的位置
button.frame =CGRectMake(20,20,20,20)
// 文字內(nèi)容
[button setTitle:@“這是按鈕哦” forState:UIControlStateNormal];
// 文字顏色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 圖片
[button setImage:[UIImage imageNamed:[UIImage imageNamed:@“123.png”]] forState:UIControlStateNormal];
startBtn.size = startBtn.currentBackgroundImage.size ;
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 70, 0, 0);
titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
titleButton. contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
//設(shè)置邊框?qū)挾?
button.layer.borderWidth = 1.0
//設(shè)置邊框顏色
button.layer.borderColor =[UIColor orangeColor].CGColor
//設(shè)置圓角半徑
button.layer.cornerRadius = 5.0 ;
//圓
button.layer.cornerRadius = button.frame.size.width * 0.5 ;
self.btn.enable = NO ; // 是否啟用仁连?
//監(jiān)聽按鈕點擊 target : 事件處理者。action : 事件處理方法 forControlEvents : 按鈕的觸摸點擊事件
buttonClick: 有參數(shù) buttonClick 無參數(shù) @selector () 返回SEL 數(shù)據(jù)類型
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//按鈕添加到視圖中
[self.view addSubview:button];
按鈕的屬性
按鈕的樣式 :ButtonWithType:
UIButtonTypeCustom: 自定義風(fēng)格
UIButtonTypeRoundedRect : 圓角矩形
UIButtonTypeDetailDisclosure:藍色小箭頭阱穗,主要做詳細說明用
UIButtonTypeInfoLight: 亮色感嘆號
UIButtonTypeInfoDark:暗色感嘆號
UIButtonTypeContactAdd: 十字加號按鈕
按鈕的狀態(tài):forState
UIControlStateNormal 常規(guī)狀態(tài)
UIControlStateHighlighted 高亮狀態(tài)
UIControlStateDisabled 禁用狀態(tài)
UIControlStateSelected 選中狀態(tài)
按鈕的方法
//設(shè)置按鈕中的文字
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
//設(shè)置按鈕中的文字顏色
-(void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state
//設(shè)置標題陰影的變化方式
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
//設(shè)置按鈕中的圖片
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states
//設(shè)置按鈕中的背景圖片
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line
* 默認情況下饭冬,當(dāng)按鈕高亮的情況下使鹅,圖像的顏色會被畫深一點,如果這下面的這個屬性設(shè)置為no昌抠,
* 那么可以去掉這個功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情況一樣患朱,默認情況下,當(dāng)按鈕禁用的時候扰魂,圖像會被畫得深一點麦乞,設(shè)置NO可以取消設(shè)置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的這個屬性設(shè)置為yes的狀態(tài)下蕴茴,按鈕按下會發(fā)光*/
button1.showsTouchWhenHighlighted = YES;
需求: 例子
1.ios button 點擊Action連接到.h 文件的 @interface 會在 @implementatopm生成方法
2. 鏈接到.m 文件中 @interface 中 也會在@implementation 生成方法
3. 直接鏈接到.m 的@implementation 會生成方法 劝评! 這三個有什么區(qū)別嗎
解決:1..h 文件里面的成員外部可以訪問(可以設(shè)置輸出口)
2. 會自動生成set 和 get 方法,如果選擇Action(可以設(shè)置輸出口)
3. 可以生成set 和 get 方法倦淀, 不能設(shè)置輸出口
需求:點語法和setter 如何區(qū)分使用蒋畜?
例子: [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
btn.contentVerticalAlignment = UIControlContentVerticalAlignment;
解決:用點語法 實際上是調(diào)用了屬性的set 和 get方法
UIButton titleColor 是個方法不是屬性 所以點不出來 setTitleColor 是直接調(diào)用的方法
【常用】點擊事件
Sent Events :
Did End On Exit 用戶點擊return 或者 done 按鈕
Editing Changed 字符增減,cursor改變位置等
Editing Did Begin 當(dāng)field得到焦點
Editing Did End 焦點離開field
Touch Cancel 取消當(dāng)前區(qū)域的點擊操作
Touch Down 按下按鈕
Touch Down Repeat 重復(fù)按下按鈕
Touch Drag Enter 僅當(dāng)觸摸拖動進入控件范圍時發(fā)生一次
Touch Drag Exit 僅當(dāng)觸摸拖動離開控件范圍時發(fā)生一次
Touch Drag Inside 當(dāng)觸摸拖動在控件范圍內(nèi)會隨用戶的持續(xù)拖動而持續(xù)發(fā)生
Touch Drag Outside 當(dāng)觸摸拖動離開控件范圍后會隨用戶的持續(xù)拖動而持續(xù)發(fā)生
Touch Up Inside 按住按鈕后 在按鈕范圍內(nèi)松開 (常用)
Touch Up Outside 按住按鈕后 在按鈕范圍外松開
Value Changed 一個點擊拖拽或者操作一個區(qū)域 產(chǎn)生一系列的值```
## 1. 自定義Button 例子
![左邊文字 右邊圖片的樣式](http://upload-images.jianshu.io/upload_images/2442197-afdd528c2e9d2b21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
只要 自定義一個XMGButton 繼承自UIButton
然后在撞叽。m文件中 寫入一下代碼就好了姻成。
當(dāng)你要使用這個自定義 Button的時候
你必須導(dǎo)入這個頭文件
import "XMGButton.h"
然后在你要用的UIViewController 中 寫入
XMGButton *button = [[XMGButton alloc] init];
和其他的相應(yīng)代碼即可。
## 2. 自定義Button 例子 方式二
![方式二.png](http://upload-images.jianshu.io/upload_images/2442197-793b3aa45a5a818e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## 3. 自定義Button 例子
上圖 下文字: 具體操作參考上圖
![5AD19F32-6385-4F93-9D24-2AD0E31AAC42.png](http://upload-images.jianshu.io/upload_images/2442197-970c7f79128d18bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## 3. 自定義Button --- 記住密碼 打鉤選中
![記住密碼 打鉤選中](http://upload-images.jianshu.io/upload_images/2442197-3736640d746bde80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#4.圖片的拉伸問題
![圖片樣式.png](http://upload-images.jianshu.io/upload_images/2442197-c4c9c28c71715a1e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
方式1:
![E72660821A2E52F690DCAEEFDA8EB4BB.jpg](http://upload-images.jianshu.io/upload_images/2442197-a595acd8a959154c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
方式二:
![CC438E35-E0B7-4297-B0E6-849C5F63B7FF.png](http://upload-images.jianshu.io/upload_images/2442197-d0c03afacf966356.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##需求:關(guān)于圖片拉伸 的問題解決愿棋。
方法二 :在圖片中設(shè)置
1.如果是正方形的 就設(shè)置 slicing 為 水平和垂直的科展。
- 如果是QQ 聊天類型的圖片 就查看圖片像素 比如 Size 128 -112 pixels
就在left 設(shè)置64 right 63 top 56 Bottom 39 width = height = 1 ;
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = titleButton.titleLabel.font;
CGFloat titleW = [titleButton.currentTitle sizeWithAttributes:attrs].width;
NSStringFromCGRect(self.titleLabel.frame);
NSStringFromCGRect(self.imageView.frame);