學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
更新時間:2020年05月26日 14:56:15 作者:android_it
這篇文章主要為大家詳細(xì)介紹了iOS開關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS開關(guān)按鈕UISwitch控件的具體代碼,供大家參考,具體內(nèi)容如下
在ViewController.h里面
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
//定義一個開關(guān)控件
//作用可以進(jìn)行狀態(tài)的改變
//開,關(guān):兩種狀態(tài)可以切換
//所有UIKit框架庫中的控件均已UI開頭
//蘋果官方的控件都定義在UIKit框架庫中
UISwitch * _mySwitch;
}
@property(retain,nonatomic) UISwitch * mySwitch;
@end
在ViewController.m里面
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mySwitch=_mySwitch;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創(chuàng)建一個開關(guān)對象
//繼承于UIView的
_mySwitch = [[UISwitch alloc]init];
//蘋果官方的控件的位置設(shè)置
//位置X,Y的值可以改變(100,100)
//寬度和高度值無法改變(80,40)寫了也沒有用的,不會起到作用的。默認(rèn)的。
_mySwitch.frame=CGRectMake(100, 200, 180, 40);
//開關(guān)狀態(tài)設(shè)置屬性
//YES:開啟狀態(tài)
//NO:關(guān)閉狀態(tài)
_mySwitch.on=YES;
//也可以使用set函數(shù)
//[_mySwitch setOn:YES];
//設(shè)置開關(guān)狀態(tài)
//p1:狀態(tài)設(shè)置
//p2:是否開啟動畫效果
//[_mySwitch setOn:YES animated:YES];
[self.view addSubview:_mySwitch];
//設(shè)置開啟狀態(tài)的風(fēng)格顏色
[_mySwitch setOnTintColor:[UIColor orangeColor]];
//設(shè)置開關(guān)圓按鈕的風(fēng)格顏色
[_mySwitch setThumbTintColor:[UIColor blueColor]];
//設(shè)置整體風(fēng)格顏色,按鈕的白色是整個父布局的背景顏色
[_mySwitch setTintColor:[UIColor greenColor]];
//向開關(guān)控件添加事件函數(shù)
//p1:函數(shù)實現(xiàn)對象
//p2:函數(shù)對象
//p3:事件響應(yīng)時的事件類型UIControlEventValueChanged狀態(tài)發(fā)生變化時觸發(fā)函數(shù)
[_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
}
//參數(shù)傳入開關(guān)對象本身
- (void) swChange:(UISwitch*) sw{
if(sw.on==YES){
NSLog(@"開關(guān)被打開");
}else{
NSLog(@"開關(guān)被關(guān)閉");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解iOS中Button按鈕的狀態(tài)和點擊事件
- 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法
- IOS UITableViewCell詳解及按鈕點擊事件處理實例
- iOS開發(fā)中UISwitch按鈕的使用方法簡介
- 詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法
- 詳解iOS-按鈕單選與多選邏輯處理
- iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- iOS 防止按鈕多次點擊造成多次響應(yīng)的方法
- iOS實現(xiàn)全局懸浮按鈕
相關(guān)文章
iOS mobileconfig配置文件進(jìn)行簽名的配置方法
這篇文章主要介紹了iOS mobileconfig配置文件進(jìn)行簽名的配置方法,給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
IOS9.0 LaunchScreen.StroyBoard自定義啟動圖片詳解
這篇文章主要介紹了IOS9.0 LaunchScreen.StroyBoard自定義啟動圖片詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02

