iOS-滑竿、分段选择器、开关按钮的简单实现

    滑竿、分段选择器、开关按钮的简单实现,它们都是UIControl的子类、下面是代码示例:


#import "ViewController.h"

 

@interface ViewController ()

{

    UIView *bgView;

    UIImageView *animationView;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    bgView = [[UIView alloc]initWithFrame:self.view.frame];

    [self.view addSubview:bgView];

    

    

    NSMutableArray *images = [NSMutableArray array];

    for (int i=1; i<=6;i++) {

       

        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"niao2%d(被拖移).tiff",i]]];

       

    }

    

    animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    animationView.animationImages= images;

    animationView.animationRepeatCount = -1;

//   animationView.animationDuration = 3;

    [self.view addSubview:animationView];

    

//    1、分段选择控件 使用多个按钮的时候  可以选择 使用  分段选择控件

//    2、开关按钮

//    3、滑杆

    

    

//    今天练习的 都是UIControl的子类 包括之前学的按钮

//    1、分段选择控件

//    分段选择控件 再出始化的时候   需给他一个标题的数组

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娱乐",@"军事",@"科技"]];

    segment.frame = CGRectMake(100, 100, 200, 40);

//    设置是否记忆上一个按钮

    segment.momentary = YES;

    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segment];

    

    

    

//    开关按钮

//    开关按钮  一般需要  记录用户设置的状态  1、可以用后台提供的接口 设置开关按钮的 开关 (可以在不同设备间 同步状态(信息) 2、在本地保存设置

    UISwitch *switchButton = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 50, 40)];

    [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

//    设置Switch的默认状态

   switchButton.on = [userDefaults boolForKey:@"isOn"];

    

//    设置开关按钮  打开的时候  轨道的颜色

   switchButton.onTintColor = [UIColor redColor];

//    设置开关按钮  关闭时候的  轨道颜色

   switchButton.tintColor = [UIColor greenColor];

//    设置开关按钮 小圆圈的颜色

   switchButton.thumbTintColor = [UIColor yellowColor];

    

    [self.view addSubview:switchButton];

    

    

//    滑杆

    UISlider *slider = [[UISlideralloc]initWithFrame:CGRectMake(100,300, 200,10)];

    

    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

    

//    设置滑杆的最小值

    slider.minimumValue = 1;

    

//    设置滑杆的最大值

    slider.maximumValue = 10;

    

//    设置滑杆默认位置

    slider.value = 1;

    

//    设置 滑杆最小值的轨道颜色

    slider.minimumTrackTintColor = [UIColor redColor];

//    滑杆 最大值的 轨道颜色

    slider.maximumTrackTintColor = [UIColor purpleColor];

//    小圆圈的颜色

    slider.thumbTintColor = [UIColoryellowColor];

    

    [self.view addSubview:slider];

    

}

 

// 手指 触摸到屏幕上的时候  触发

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event

{

    

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"]!=NO) {

       

        //    获得 触摸事件

       UITouch *touch = [touches anyObject];

        //    获得触摸的点

       CGPoint touchPoint = [touch locationInView:self.view];

       

        //    动画没有执行的时候 调用 里面的方法

       if (animationView.isAnimating != YES){

           

           animationView.alpha = 1.0;

           

           animationView.center = touchPoint;

           

           [animationView startAnimating];

       }

       

    }

 

    

}

 

//手指  在屏幕上 移动 触发

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event

{

    

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"] != NO) {

       

        //    获得 触摸事件

       UITouch *touch = [touches anyObject];

        //    获得触摸的点

       CGPoint touchPoint = [touch locationInView:self.view];

       

       animationView.center = touchPoint;

       

    }

    

}

 

//手指 离开屏幕的  时候(触摸结束) 触发

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event

{

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"] != NO) {

       

        [UIView animateWithDuration:2 animations:^{

           animationView.alpha = 0.0;

       } completion:^(BOOL finished) {

           [animationView stopAnimating];

       }];

       

    }

}

 

- (void)sliderAction:(UISlider*)sender

{

    NSLog(@"%0.2f",sender.value);

    

    animationView.animationDuration = sender.value;

}

 

- (void)switchAction:(UISwitch*)sender

{

    NSLog(@"%d",sender.isOn);

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

   [userDefaults setBool:sender.isOn forKey:@"isOn"];

   [userDefaults synchronize];

    

}

 

- (void)segmentAction:(UISegmentedControl*)sender

{

    NSLog(@"%ld",sender.selectedSegmentIndex);

    

    switch (sender.selectedSegmentIndex) {

       case 0:

           bgView.backgroundColor = [UIColorbrownColor];

           break;

       case 1:

           bgView.backgroundColor = [UIColorwhiteColor];

           break;

       case 2:

           bgView.backgroundColor = [UIColorlightGrayColor];

           break;

       default:

           break;

    }

    

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值