日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl

發(fā)布時(shí)間:2025/6/15 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IOS中最常用到的控件UIButton、UISlider、UISwitch、UISegmentedControl通過Xib文件拖動(dòng)生成非常簡(jiǎn)單,其實(shí)用代碼實(shí)現(xiàn)也是一樣的簡(jiǎn)單,當(dāng)然,用代碼實(shí)現(xiàn)能夠掌握到更多的東西。

上圖中包涵提到的4種控件,UIButton按鈕、UISlider滑塊、UISwitch開關(guān)、UISegmentedControl分類

首先創(chuàng)建一個(gè)名為CodeControls的Empty Application項(xiàng)目


AppDelegate.h和AppDelegate.m文件中和IOS代碼實(shí)現(xiàn)Hello World中的一樣


MainViewController.h

[cpp]?view plaincopy
  • <span?style="font-size:10px;">#import?<UIKit/UIKit.h>??
  • ??
  • @interface?MainViewController?:?UIViewController??
  • ??
  • @property?(strong,?nonatomic)?UIButton?*myBtn;??
  • @property?(strong,?nonatomic)?UISlider?*mySlider;??
  • @property?(strong,?nonatomic)?UISwitch?*mySwitch;??
  • @property?(strong,?nonatomic)?UISegmentedControl?*mySc;??
  • ??
  • @end</span>??

  • MainViewController.m

    [cpp]?view plaincopy
  • <span?style="font-size:10px;">#import?"MainViewController.h"??
  • ??
  • @interface?MainViewController?()??
  • ??
  • @end??
  • ??
  • @implementation?MainViewController??
  • @synthesize?myBtn,mySlider,mySwitch,mySc;??
  • ??
  • -?(void)viewDidLoad??
  • {??
  • ????//?加載UIView??
  • ????UIView?*mainView?=?[[UIView?alloc]?initWithFrame:[[UIScreen?mainScreen]?applicationFrame]];??
  • ????mainView.backgroundColor?=?[UIColor?whiteColor];??
  • ????self.view?=?mainView;??
  • ????[mainView?release];??
  • ??????
  • ????//?創(chuàng)建一個(gè)Button按鈕??
  • ????UIButton?*btn?=?[UIButton?buttonWithType:UIButtonTypeRoundedRect];??
  • ????btn.frame?=?CGRectMake(100,?30,?57,?57);??
  • ????[btn?setTitle:@"Button"?forState:UIControlStateNormal];??
  • ????[btn?setTitleColor:[UIColor?blackColor]?forState:UIControlStateNormal];??
  • ????[btn?setBackgroundImage:[UIImage?imageNamed:@"icon.png"]?forState:UIControlStateNormal];??
  • ????[btn?addTarget:self?action:@selector(onClick:)?forControlEvents:UIControlEventTouchUpInside];??
  • ????myBtn?=?btn;??
  • ????[self.view?addSubview:myBtn];??
  • ??????
  • ??????
  • ????//?創(chuàng)建一個(gè)Slider劃塊按鈕??
  • ????UISlider?*slider?=?[[[UISlider?alloc]?initWithFrame:CGRectMake(50,?180,?200,?10)]?autorelease];??
  • ????slider.minimumValue?=?0.0f;??
  • ????slider.maximumValue?=?100.0f;??
  • ????slider.value?=?50.0f;??
  • ????[slider?addTarget:self?action:@selector(onChange:)?forControlEvents:UIControlEventTouchUpInside];??
  • ????mySlider?=?slider;??
  • ????[self.view?addSubview:mySlider];??
  • ??????
  • ????//?創(chuàng)建一個(gè)UISwitch開關(guān)按鈕??
  • ????UISwitch?*sbtn?=?[[[UISwitch?alloc]?initWithFrame:CGRectMake(50,?210,?200,?50)]?autorelease];??
  • ????[sbtn?addTarget:self?action:@selector(onSwitch:)?forControlEvents:UIControlEventTouchUpInside];??
  • ????mySwitch?=?sbtn;??
  • ????[self.view?addSubview:mySwitch];??
  • ??????
  • ????//?創(chuàng)建一個(gè)UISegmentedControl??
  • ????NSArray?*btnList?=?[NSArray?arrayWithObjects:@"left",@"center",@"right",?nil];??
  • ????UISegmentedControl?*sc?=?[[[UISegmentedControl?alloc]?initWithItems:btnList]?autorelease];??
  • ????sc.frame?=?CGRectMake(50,?250,?200,?60);??
  • ????[sc?addTarget:self?action:@selector(onSelect:)?forControlEvents:UIControlEventTouchUpInside];??
  • ????mySc?=?sc;??
  • ????[self.view?addSubview:mySc];??
  • ??????
  • ????[super?viewDidLoad];??
  • }??
  • ??
  • //?點(diǎn)擊Button觸發(fā)??
  • -?(void)onClick:(id?*)sender??
  • {??
  • ??
  • }??
  • ??
  • //?拉動(dòng)Slider劃塊觸發(fā)??
  • -?(void)onChange:(id?*)sender??
  • {??
  • ??????
  • }??
  • ??
  • //?選擇Switch觸發(fā)??
  • -?(void)onSwitch:(id?*)sender??
  • {??
  • ??????
  • }??
  • ??
  • //?選擇UISegmentedControl觸發(fā)??
  • -?(void)onSelect:(id?*)sender??
  • {??
  • }??
  • </span>??
  • 這里沒有寫點(diǎn)擊每個(gè)控件的具體實(shí)現(xiàn)方法。

    UICnotrol Class 下的所有Touch事件

    [cpp]?view plaincopy
  • UIControlEventTouchDown?????????????
  • UIControlEventTouchDownRepeat???????
  • UIControlEventTouchDragInside???????
  • UIControlEventTouchDragOutside??????
  • UIControlEventTouchDragEnter????????
  • UIControlEventTouchDragExit?????????
  • UIControlEventTouchUpInside?????????
  • UIControlEventTouchUpOutside????????
  • UIControlEventTouchCancel???????????
  • UIControlEventValueChanged??????????
  • UIControlEventEditingDidBegin???????
  • UIControlEventEditingChanged????????
  • UIControlEventEditingDidEnd?????????
  • UIControlEventEditingDidEndOnExit???
  • UIControlEventAllTouchEvents????????
  • UIControlEventAllEditingEvents??????
  • UIControlEventApplicationReserved???
  • UIControlEventSystemReserved????????
  • UIControlEventAllEvents??


  • UIButton Class下的所有按鈕樣式

    [cpp]?view plaincopy
  • UIButtonTypeCustom??
  • UIButtonTypeRoundedRect??
  • UIButtonTypeDetailDisclosure??
  • UIButtonTypeInfoLight??
  • UIButtonTypeInfoDark??
  • UIButtonTypeContactAdd??
  • 總結(jié)

    以上是生活随笔為你收集整理的IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。