UI-分段&开关&滑杆

#pragma  --------UISegmentedContro分段选择控件

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娱乐",@"军事",@"科技"]];//分段选择控件 在初始化的时候 需要给它一个标题数组

    segment.frame = CGRectMake(100, 100, 200, 50);//设置一下坐标

    segment.momentary = YES;

    [segment addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];//触发方法的方式

    [self.view addSubview:segment];

- (void)segmentedAction:(UISegmentedControl *)sender

{

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

    

    switch (sender.selectedSegmentIndex) {

        case 0:

            bgView.backgroundColor = [UIColor brownColor];

            break;

        case 1:

            bgView.backgroundColor = [UIColor whiteColor];

            break;

        case 2:

            bgView.backgroundColor = [UIColor purpleColor];

            break;

        default:

            break;

    }



#pragma  --------UISwitch开关按钮控件

    UISwitch *switchButton = [[UISwitch alloc]initWithFrame:CGRectMake(10, 160, 50, 40)]; // 开关按钮一般需要记录用户设置的状态 1、可以用后台提供的接口设置开关按钮的开关(可以在不同设备之间同步(信息)) 2、在本地保存设置

    switchButton.onTintColor = [UIColor redColor]; // 设置开关按钮 打开时候的轨道颜色

    switchButton.tintColor = [UIColor grayColor]; // 设置开关关闭时候的颜色

    switchButton.thumbTintColor = [UIColor yellowColor]; // 设置开关小圆圈的颜色

    [switchButton addTarget:self action:@selector(switchAction:)

     forControlEvents:UIControlEventValueChanged ];//触发方法的方式

 

   //    设置switch的默认状态

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

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

    [self.view addSubview:switchButton];

  

- (void)switchAction:(UISwitch *)sender

{

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

      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

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

    [userDefaults synchronize];

}


 

#pragma  --------滑杆控件 UISlider  (通过滑竿可以设置动画的速度)

    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 250, 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 = [UIColor orangeColor]; // 设置小圆圈的颜色

    [self.view addSubview:slider];

- (void)sliderAction:(UISlider *)sender

{

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

    animationView.animationDuration = sender.value;

}

#pragma  --------利用下面三个方法写一个动画

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event//当手指触摸到屏幕上的时候 触发这个事件

{

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isonOn"] != YES) {

        UITouch *touch = [touches anyObject]; // 获得触摸时的点的位置

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

        animationView.center = touchPoint;

        if (animationView.isAnimating != YES)

            //        动画没有被执行的时候 调用这个方法

        {

            animationView.alpha = 1.0;

            animationView.center = touchPoint;

            [animationView startAnimating];

        }

    }

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//手指在屏幕上移动 触发这个事件

{

    UITouch *touch = [touches anyObject]; // 获得触摸时的点的位置

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

//    animationView.center = touchPoint;

    

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // 手指离开时候触发的方法

{

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

        [UIView animateWithDuration:5 animations:^{

            animationView.alpha = 0.8;

        } completion:^(BOOL finished) {

            [animationView stopAnimating];

        }];


    }

    

    

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值