zoukankan      html  css  js  c++  java
  • 控制控件(滑杆,分段选择控件,开关按钮)

    //

    //  ViewController.m

    //  UI-NO-9

    //

    //  Created by Bruce on 15/7/23.

    //  Copyright (c) 2015年 Bruce. All rights reserved.

    //

     

    #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 = [[UISlider alloc]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 = [UIColor yellowColor];

        

        [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 = [UIColor brownColor];

                break;

            case 1:

                bgView.backgroundColor = [UIColor whiteColor];

                break;

            case 2:

                bgView.backgroundColor = [UIColor lightGrayColor];

                break;

            default:

                break;

        }

        

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

  • 相关阅读:
    【转】ERROR: Cannot uninstall 'llvmlite'. It is a distutils installed project. 此类报错的解决办法
    【转】python setup.py install 报错:error:[WinError 3]系统找不到指定的路径: 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib
    【转】距离相关系数以及python包的安装
    【转】算法总结这是一份全面并且详细的排序算法学习指南
    origin 一键导出文件夹中所有图形
    【转】距离相关系数的python实现
    [转]一文让你通俗理解奇异值分解
    origin 检查是否有重复图片
    【Vegas原创】centos网卡自启动
    【Vegas原创】使用dockercompose运行mysql8
  • 原文地址:https://www.cnblogs.com/wukun16/p/4883970.html
Copyright © 2011-2022 走看看