日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UISwitch,UISegmentedControl及UISlider的初步学习

發(fā)布時(shí)間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UISwitch,UISegmentedControl及UISlider的初步学习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end 復(fù)制代碼// AppDelegate.m #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];ViewController *viewController = [[ViewController alloc]init];UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];self.window.rootViewController = navigationController;[self.window makeKeyAndVisible];return YES; } - (void)applicationWillResignActive:(UIApplication *)application { } - (void)applicationDidEnterBackground:(UIApplication *)application { } - (void)applicationWillEnterForeground:(UIApplication *)application { } - (void)applicationDidBecomeActive:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { } @end 復(fù)制代碼// ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end 復(fù)制代碼// ViewController.m #import "ViewController.h" #import "DemonstrationController.h" #import "RightController.h" @interface ViewController () @property(strong,nonatomic)UISwitch *rightSwitch; @property(strong,nonatomic)UISwitch *leftSwitch; @property(strong,nonatomic)UILabel *sliderValue; @end @implementation ViewController - (void)viewDidLoad {[super viewDidLoad];CGRect screen = [[UIScreen mainScreen] bounds];CGFloat switchScreenSpace = 39;//初始化開關(guān)控件self.rightSwitch = [[UISwitch alloc] init];CGRect frame = self.rightSwitch.frame;//origin:表示控件的坐標(biāo)原點(diǎn)frame.origin = CGPointMake(switchScreenSpace,98);//重新設(shè)置控件的位置self.rightSwitch.frame = frame;//設(shè)置控件的狀態(tài)self.rightSwitch.on = true;//添加事件[self.rightSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];//添加開關(guān)控件到控制器視圖[self.view addSubview:self.rightSwitch];self.leftSwitch = [[UISwitch alloc] init];frame = self.leftSwitch.frame;frame.origin = CGPointMake(screen.size.width - (frame.size.width + switchScreenSpace), 98);self.leftSwitch.frame = frame;self.leftSwitch.on = true;[self.leftSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];[self.view addSubview:self.leftSwitch];NSArray *segments = @[@"right",@"left"];//初始化分段控制器UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segments];CGFloat scWidth = 220;CGFloat scHeight = 29;//29為默認(rèn)高度CGFloat scTopView = 186;frame = CGRectMake((screen.size.width - scWidth)/2, scTopView, scWidth, scHeight);segmentedControl.frame = frame;//添加事件[segmentedControl addTarget:self action:@selector(tounchDow:) forControlEvents:UIControlEventValueChanged];//將分段控制器添加到控制器視圖[self.view addSubview:segmentedControl];CGFloat sliderWidth = 300;CGFloat sliderHeight = 31;///默認(rèn)高度CGFloat sliderTopView = 298;//初始化滑塊控件UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake((screen.size.width - sliderWidth)/2, sliderTopView, sliderWidth, sliderHeight)];slider.minimumValue = 0.0f;//設(shè)置最小值slider.maximumValue = 100.0f;//設(shè)置最大值slider.value = 50.0f;//設(shè)置默認(rèn)值//添加事件[slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];//添加滑塊控件到控制器視圖[self.view addSubview:slider];//添加labelSliderValue標(biāo)簽CGFloat labelSliderValueSliderSpace = 30;//初始化標(biāo)簽UILabel *labelSliderValue = [[UILabel alloc]initWithFrame:CGRectMake(slider.frame.origin.x, slider.frame.origin.y - labelSliderValueSliderSpace, 103, 21)];labelSliderValue.text = @"SliderValue";[self.view addSubview:labelSliderValue];//添加sliderValueself.sliderValue = [[UILabel alloc]initWithFrame:CGRectMake(labelSliderValue.frame.origin.x + 120, labelSliderValue.frame.origin.y, 50, 21)];self.sliderValue.text = @"50";[self.view addSubview:self.sliderValue]; } //用標(biāo)簽顯示滑塊的值 -(void)sliderValueChange:(id)sender{UISlider *slider = (UISlider *) sender;int progressAsInt = (int)(slider.value);NSString * newText = [[NSString alloc]initWithFormat:@"%d",progressAsInt];NSLog(@"滑塊的值 : %@",newText);self.sliderValue.text = newText; } //點(diǎn)擊分段控件控制分段開關(guān)的隱藏和顯示 -(void)tounchDow:(id)sender{UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;NSLog(@"選擇的段 : %li",segmentedControl.selectedSegmentIndex);if(self.rightSwitch.hidden){self.rightSwitch.hidden = false;self.leftSwitch.hidden = false;DemonstrationController *demonstration = [[DemonstrationController alloc]init];//把當(dāng)前控制器作為背景self.definesPresentationContext = YES;//設(shè)置模態(tài)視圖彈出樣式demonstration.modalPresentationStyle = UIModalPresentationOverFullScreen;//模態(tài)切換的方式跳轉(zhuǎn)[self presentViewController:demonstration animated:YES completion:nil];}else{self.rightSwitch.hidden = true;self.leftSwitch.hidden = true;RightController *right = [[RightController alloc]init];//導(dǎo)航控制器入棧的方式跳轉(zhuǎn)[self.navigationController pushViewController:right animated:YES];} } ///使兩個(gè)開關(guān)的值保持一致 -(void)switchValueChanged:(id)sender{UISwitch *witch = (UISwitch *)sender;//判斷開關(guān)狀態(tài)BOOL setting = witch.isOn;[self.rightSwitch setOn:setting animated:true];[self.leftSwitch setOn:setting animated:true]; } @end 復(fù)制代碼// DemonstrationController.h #import <UIKit/UIKit.h> #import "ShadowController.h" @interface DemonstrationController : ShadowController @end 復(fù)制代碼// DemonstrationController.m #import "DemonstrationController.h" #import "RightController.h" #import <objc/runtime.h> static void * KEY_IS_CLOSED = &KEY_IS_CLOSED; @interface DemonstrationController ()@end@implementation DemonstrationController- (void)viewDidLoad {[super viewDidLoad];CGRect screen = [[UIScreen mainScreen]bounds];CGFloat viewWidth = 150.0;CGFloat viewHeight = 150.0;CGFloat buttonWidth = 100;CGFloat buttonHeight = 30;//監(jiān)聽通知[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fun) name:@"tongzhi" object:nil];//初始化UIViewUIView *view = [[UIView alloc]initWithFrame:CGRectMake((screen.size.width - viewWidth)/2, (screen.size.height - viewHeight) / 2, viewWidth, viewHeight)];//設(shè)置背景顏色view.backgroundColor = [UIColor orangeColor];//將UIView添加到控制器視圖[self.view addSubview:view];//初始化按鈕并設(shè)置樣式UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//設(shè)置按鈕標(biāo)題[button setTitle:@"前往右控制器" forState:UIControlStateNormal];//設(shè)置按鈕標(biāo)題顏色[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];//設(shè)置按鈕標(biāo)題字體大小button.titleLabel.font = [UIFont systemFontOfSize:15];//設(shè)置按鈕背景顏色[button setBackgroundColor:[UIColor blueColor]];//設(shè)置按鈕位置button.frame = CGRectMake((viewWidth - buttonWidth)/2, (viewHeight - buttonHeight)/2, buttonWidth, buttonHeight);//添加按鈕到UIView[view addSubview:button];//添加事件[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; } -(void)onClick{RightController *right = [[RightController alloc]init];[self presentViewController:right animated:YES completion:nil]; }-(void)fun{//延時(shí)執(zhí)行方法[self performSelector:@selector(jumpRootController) withObject:nil afterDelay:0.5]; }-(void)jumpRootController{//關(guān)閉當(dāng)前控制器[self closeCurrentViewController]; }#pragma mark - Close current view controller.//關(guān)閉當(dāng)前視圖控制器 - (void)closeCurrentViewController {//注銷當(dāng)前view[self.view endEditing:YES];if (self.presentingViewController) {[self dismissViewControllerAnimated:YES completion:NULL];} else if (self.navigationController) {UINavigationController *navigationController = self.navigationController;NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navigationController.childViewControllers];if ([viewControllers indexOfObject:self] != (viewControllers.count - 1)) {if([self respondsToSelector:@selector(setClosed:)]) {[self setClosed:YES];}} else {[navigationController popViewControllerAnimated:YES];}}} - (void)setClosed:(BOOL)closed {//void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);關(guān)聯(lián)對(duì)象。需要#import <objc/runtime.h>。//應(yīng)用場(chǎng)景:關(guān)聯(lián)對(duì)象相當(dāng)于實(shí)例變量,在類別(也有人管叫分類)里面,不能創(chuàng)建實(shí)例變量, 關(guān)聯(lián)對(duì)象就可以解決這種問題。//相關(guān)參數(shù):key:要保證全局唯一,key與關(guān)聯(lián)的對(duì)象是一一對(duì)應(yīng)關(guān)系,必須全局唯一,通常都是會(huì)采用靜態(tài)變量來作為關(guān)鍵字 可以自己創(chuàng)建 也可以使用"@selector(methodName)"作為key。//value:要關(guān)聯(lián)的對(duì)象。//policy:關(guān)聯(lián)策略。有五種關(guān)聯(lián)策略。//OBJC_ASSOCIATION_ASSIGN 等價(jià)于 @property(assign)。//OBJC_ASSOCIATION_RETAIN_NONATOMIC等價(jià)于 @property(strong, nonatomic)。//OBJC_ASSOCIATION_COPY_NONATOMIC等價(jià)于@property(copy, nonatomic)。//OBJC_ASSOCIATION_RETAIN等價(jià)于@property(strong,atomic)。//OBJC_ASSOCIATION_COPY等價(jià)于@property(copy, atomic)。objc_setAssociatedObject(self, KEY_IS_CLOSED, @(closed), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end 復(fù)制代碼// ShadowController.h //繼承該控制器可以得到一個(gè)帶陰影層的控制器 #import <UIKit/UIKit.h> @interface ShadowController : UIViewController @end 復(fù)制代碼// ShadowController.m #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #import "ShadowController.h"@interface ShadowController () @property (nonatomic, strong) UIView *shadowView; @end@implementation ShadowController- (instancetype)init {self = [super init];if (self) {//設(shè)置模態(tài)轉(zhuǎn)換樣式self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;//設(shè)置模態(tài)表示樣式self.modalPresentationStyle = UIModalPresentationOverCurrentContext;//設(shè)置背景顏色self.view.backgroundColor = [UIColor clearColor];self.shadowView = [[UIView alloc]init];//設(shè)置透明度self.shadowView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];//設(shè)置位置self.shadowView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);//將子控件插入到subviews數(shù)組中index這個(gè)位置[self.view insertSubview:self.shadowView atIndex:0];}return self; } @end 復(fù)制代碼// RightController.h #import <UIKit/UIKit.h> #import "ShadowController.h" @interface RightController : ShadowController @end 復(fù)制代碼// RightController.m#import "RightController.h" #import "ViewController.h" #import <objc/runtime.h> static void * KEY_IS_CLOSED = &KEY_IS_CLOSED; @interface RightController ()@end@implementation RightController- (void)viewDidLoad {[super viewDidLoad];CGRect screen = [[UIScreen mainScreen]bounds];CGFloat viewWidth = 150.0;CGFloat viewHeight = 150.0;CGFloat buttonWidth = 100;CGFloat buttonHeight = 30;UIView *view = [[UIView alloc]initWithFrame:CGRectMake((screen.size.width - viewWidth)/2, (screen.size.height - viewHeight) / 2, viewWidth, viewHeight)];view.backgroundColor = [UIColor redColor];[self.view addSubview:view];UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[button setTitle:@"返回根控制器" forState:UIControlStateNormal];[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];button.titleLabel.font = [UIFont systemFontOfSize:15];[button setBackgroundColor:[UIColor purpleColor]];button.frame = CGRectMake((viewWidth - buttonWidth)/2, (viewHeight - buttonHeight)/2, buttonWidth, buttonHeight);[view addSubview:button];[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; } -(void)onClick{//注冊(cè)通知[[NSNotificationCenter defaultCenter]postNotificationName:@"tongzhi" object:nil];[self closeCurrentViewController]; }#pragma mark - Close current view controller. - (void)closeCurrentViewController {[self.view endEditing:YES];if (self.presentingViewController) {[self dismissViewControllerAnimated:YES completion:NULL];} else if (self.navigationController) {UINavigationController *navigationController = self.navigationController;NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navigationController.childViewControllers];if ([viewControllers indexOfObject:self] != (viewControllers.count - 1)) {if([self respondsToSelector:@selector(setClosed:)]) {[self setClosed:YES];}} else {[navigationController popViewControllerAnimated:YES];}}} - (void)setClosed:(BOOL)closed {objc_setAssociatedObject(self, KEY_IS_CLOSED, @(closed), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end 復(fù)制代碼

轉(zhuǎn)載于:https://juejin.im/post/5cb467895188257ab966d0b9

總結(jié)

以上是生活随笔為你收集整理的UISwitch,UISegmentedControl及UISlider的初步学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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