IOS控件代码

//键盘的打开和关闭
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{
    //注销键盘出现通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
    //注销键盘影藏通知
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [super viewWillAppear:<#animated#>]
    
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    
    
    [super viewWillDisappear:<#animated#>]
}

- (void)keyboardDidshow:(NSNotification *)notif{
    NSLog(@"键盘打开");
    
}
- (void)keyboardDidHide:(NSNotification *)notif{
    NSLog(@"键盘关闭");
    
}

@end
//
//  ViewController.m
//  text20190529
//
//  Created by leo on 2019/5/29.
//  Copyright © 2019年 leo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
//创建rightSwitch控件
//在代码中创建switch控件时使用的默认函数是init,所以frame属性设置不是在实例化时候设置的
//
//
//
//
@property(strong,nonatomic)UISwitch *rightSwitch;
@property(strong,nonatomic)UISwitch *leftSwitch;
@property(strong,nonatomic)UISwitch *sliderValue;



@end
@implementation ViewController
- (void)viewDidLoad{
    [super viewDidLoad];
    CGRect screen = [[UIScreen mainScreen]bounds];
    //添加rightswitch控件,rightswitch与屏幕右边距,leftswitch与屏幕左边距
    CGFloat switchScreenSpace = 39;
    self.rightSwitch = [[UISwitch alloc]init];
    CGRect frame = self.rightSwitch.frame;
    //设置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];
    //2、添加eightswitch控件
    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];
    
    
    
}
//使两个开关的值保持一致

- (void)switchValueChanged:(id)sender{
    UISwitch *witchSwitch = (UISwitch *)sender;
    BOOL setting = witchSwitch.isOn;
    [self.leftSwitch setOn:setting animated:TRUE];
    [self.rightSwitch setOn:setting animated:TRUE];
    
}


@end

在这里插入图片描述

//
//  ViewController.m
//  分段控件
//
//  Created by leo on 2019/5/29.
//  Copyright © 2019年 leo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(strong,nonatomic)UISwitch *rightSwitch;
@property(strong,nonatomic)UISwitch *leftSwitch;
@property(strong,nonatomic)UISwitch *sliderValue;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect screen = [[UIScreen mainScreen]bounds];
    CGRect frame = self.rightSwitch.frame;
    
    // Do any additional setup after loading the view, typically from a nib.
    //添加segmentedcontrol控件
    NSArray* segments = @[@"Right",@"Left"];
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:segments];
    CGFloat scWidth =220;
    CGFloat scHeight = 29;//29为默认高度
    CGFloat scTopView = 186;
    frame = CGRectMake((screen.size.width - scWidth)/2, scTopView, scWidth, scHeight);
    //重新设置控件的位置
    segmentControl.frame = frame;
    //指定事件处理方法
    [segmentControl addTarget:self action:@selector(touchDown:)forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    
}


- (void)touchDown:(id)sender {
    UISegmentedControl *segmentedControl  = (UISegmentedControl *)sender;
    if (self.leftSwitch.hidden) {
        
        self.rightSwitch.hidden = FALSE;
        self.leftSwitch.hidden = FALSE;
        
    }else{
        self.rightSwitch.hidden = TRUE;
        self.leftSwitch.hidden = TRUE;
    }
    // Dispose of any resources that can be recreated.
}


@end

在这里插入图片描述

//
//  ViewController.m
//  分段控件
//
//  Created by leo on 2019/5/29.
//  Copyright © 2019年 leo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(strong,nonatomic)UILabel *rightSwitch;
@property(strong,nonatomic)UILabel *leftSwitch;
@property(strong,nonatomic)UILabel *sliderValue;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加slider控件
    CGFloat sliderWidth =300;
    CGFloat sliderHeight = 31;//29为默认高度
    CGFloat sliderdTopView = 298;
    CGRect screen = [[UIScreen mainScreen]bounds];
    CGRect frame = self.rightSwitch.frame;
    
    UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake((screen.size.width - sliderWidth)/2, sliderdTopView, sliderWidth, sliderHeight)];
    slider.minimumValue = 0.0f;
    slider.maximumValue=100.0f;
    slider.value=50.00f;
    //指定事件的处理方法
    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    //添加sliderValue标签,sliderValue标签
    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;
    
    
    // Dispose of any resources that can be recreated.
}


@end

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值