UISwitch,UISegmentedControl及UISlider的初步学习

//  AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
复制代码
//  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
复制代码
//  ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
复制代码
//  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;
    //初始化开关控件
    self.rightSwitch = [[UISwitch alloc] init];
    CGRect frame = self.rightSwitch.frame;
    //origin:表示控件的坐标原点
    frame.origin = CGPointMake(switchScreenSpace,98);
    //重新设置控件的位置
    self.rightSwitch.frame = frame;
    //设置控件的状态
    self.rightSwitch.on = true;
    //添加事件
    [self.rightSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    //添加开关控件到控制器视图
    [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为默认高度
    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;///默认高度
    CGFloat sliderTopView = 298;
    //初始化滑块控件
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake((screen.size.width - sliderWidth)/2, sliderTopView, sliderWidth, sliderHeight)];
    slider.minimumValue = 0.0f;//设置最小值
    slider.maximumValue = 100.0f;//设置最大值
    slider.value = 50.0f;//设置默认值
    //添加事件
    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    //添加滑块控件到控制器视图
    [self.view addSubview:slider];
    
    //添加labelSliderValue标签
    CGFloat labelSliderValueSliderSpace = 30;
    //初始化标签
    UILabel *labelSliderValue = [[UILabel alloc]initWithFrame:CGRectMake(slider.frame.origin.x, slider.frame.origin.y - labelSliderValueSliderSpace, 103, 21)];
    labelSliderValue.text = @"SliderValue";
    [self.view addSubview:labelSliderValue];
    
    //添加sliderValue
    self.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];
}
//用标签显示滑块的值
-(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;
}
//点击分段控件控制分段开关的隐藏和显示
-(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];
        //把当前控制器作为背景
        self.definesPresentationContext = YES;
        //设置模态视图弹出样式
        demonstration.modalPresentationStyle = UIModalPresentationOverFullScreen;
        //模态切换的方式跳转
        [self presentViewController:demonstration animated:YES completion:nil];
    }else{
        self.rightSwitch.hidden = true;
        self.leftSwitch.hidden = true;
        RightController *right = [[RightController alloc]init];
        //导航控制器入栈的方式跳转
        [self.navigationController pushViewController:right animated:YES];
    }
}
///使两个开关的值保持一致
-(void)switchValueChanged:(id)sender{
    UISwitch *witch = (UISwitch *)sender;
    //判断开关状态
    BOOL setting = witch.isOn;
    [self.rightSwitch setOn:setting animated:true];
    [self.leftSwitch setOn:setting animated:true];
}
@end
复制代码
//  DemonstrationController.h
#import <UIKit/UIKit.h>
#import "ShadowController.h"
@interface DemonstrationController : ShadowController
@end
复制代码
//  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;
    //监听通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fun) name:@"tongzhi" object:nil];
    //初始化UIView
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake((screen.size.width - viewWidth)/2, (screen.size.height - viewHeight) / 2, viewWidth, viewHeight)];
    //设置背景颜色
    view.backgroundColor = [UIColor orangeColor];
    //将UIView添加到控制器视图
    [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 blueColor]];
    //设置按钮位置
    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{
    //延时执行方法
    [self performSelector:@selector(jumpRootController) withObject:nil afterDelay:0.5];
}

-(void)jumpRootController{
    //关闭当前控制器
    [self closeCurrentViewController];
}

#pragma mark - Close current view controller.//关闭当前视图控制器
- (void)closeCurrentViewController {
    //注销当前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);关联对象。需要#import <objc/runtime.h>。
    //应用场景:关联对象相当于实例变量,在类别(也有人管叫分类)里面,不能创建实例变量, 关联对象就可以解决这种问题。
    //相关参数:key:要保证全局唯一,key与关联的对象是一一对应关系,必须全局唯一,通常都是会采用静态变量来作为关键字  可以自己创建 也可以使用"@selector(methodName)"作为key。
    //value:要关联的对象。
    //policy:关联策略。有五种关联策略。
    //OBJC_ASSOCIATION_ASSIGN 等价于 @property(assign)。
    //OBJC_ASSOCIATION_RETAIN_NONATOMIC等价于 @property(strong, nonatomic)。
    //OBJC_ASSOCIATION_COPY_NONATOMIC等价于@property(copy, nonatomic)。
    //OBJC_ASSOCIATION_RETAIN等价于@property(strong,atomic)。
    //OBJC_ASSOCIATION_COPY等价于@property(copy, atomic)。
    objc_setAssociatedObject(self, KEY_IS_CLOSED, @(closed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
复制代码
//  ShadowController.h
//继承该控制器可以得到一个带阴影层的控制器
#import <UIKit/UIKit.h>
@interface ShadowController : UIViewController
@end
复制代码
//  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) {
        //设置模态转换样式
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        //设置模态表示样式
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        //设置背景颜色
        self.view.backgroundColor = [UIColor clearColor];
        self.shadowView = [[UIView alloc]init];
        //设置透明度
        self.shadowView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        //设置位置
        self.shadowView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
        //将子控件插入到subviews数组中index这个位置
        [self.view insertSubview:self.shadowView atIndex:0];
    }
    return self;
}
@end
复制代码
//  RightController.h
#import <UIKit/UIKit.h>
#import "ShadowController.h"
@interface RightController : ShadowController
@end
复制代码
//  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{
    //注册通知
    [[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
复制代码

转载于:https://juejin.im/post/5cb467895188257ab966d0b9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值