In this tutorial, we’ll be discussing and implementing the UISlider in our iOS Application.

在本教程中,我们将在iOS应用程序中讨论和实现UISlider。

UISlider (UISlider)

A UISlider is a control that allows the user to select a value from a range of values horizontally by dragging the thumb to the position desired.

UISlider是一种控件,它允许用户通过将拇指拖动到所需位置来从水平值范围中选择一个值。

Some use cases where a UISlider is used:

使用UISlider的一些用例:

  • Changing the Volume改变音量
  • Changing the Brightness改变亮度
  • Changing the current seek of the Video更改视频的当前搜索

To create a new UISlider UI in your application, most importantly you need to specify the range of values.
The isContinous property is used to set whether the slider is continous through the range of values or not.

要在应用程序中创建新的UISlider UI,最重要的是,您需要指定值的范围。
isContinous属性用于设置滑块在值范围内是否连续。

That means, when the isContinous is set to false, the value change would be notified to the target action method only when you release the thumb.

这意味着,当isContinous设置为false时,仅当释放拇指时,值更改才会通知给目标操作方法。

We can set UIImage at either end of the UISlider.

我们可以在UISlider的任一端设置UIImage。

  • minimumValueImage and maximumValueImage are used to set the image at either end.minimumValueImagemaximumValueImage用于在任一端设置图像。
  • minimumTrackTintColor is used to set the color of the track till the thumb’s current position.minimumTrackTintColor用于设置轨迹的颜色,直到拇指的当前位置为止。
  • maximumTrackTintColor is used to set the color of the track from the current thumb position till the end of the track.maximumTrackTintColor用于设置从当前拇指位置到轨道末端的轨道颜色。
  • thumbTintColor is used to set the tint color property on the thumb of the UISliderthumbTintColor用于在UISlider的拇指上设置色调颜色属性
  • currentThumbImage is used to set the image to be rendered on the thumb.currentThumbImage用于设置要在拇指上渲染的图像。
  • setThumbImage is used to set the image for a specific event.setThumbImage用于设置特定事件的图像。

获取价值 (Accessing the Values)

To access the value of the UISlider we can do the following:

要访问UISlider的值,我们可以执行以下操作:

  • var value: Float: The slider’s current value.var value: Float :滑块的当前值。
  • func setValue(Float, animated: Bool): Sets the slider’s current value, allowing you to animate the change visually.func setValue(Float, animated: Bool) animation func setValue(Float, animated: Bool) :设置滑块的当前值,使您可以在视觉上对更改进行动画处理。
  • minimumValue maximumValue is used to get and set the min and max values in Float for the UISlider.minimumValue maximumValue用于获取和设置Float中UISlider的最小值和最大值。

In the following section, we’ll be creating UISlider. Both programmatically and using the Interface Builder in our Storyboard.

在下一节中,我们将创建UISlider。 在我们的情节提要中以编程方式和使用Interface Builder均可。

Let’s get started!

让我们开始吧!

项目情节提要 (Project Storyboard)

The Main.storyboard of our XCode project looks like this:

我们的XCode项目的Main.storyboard如下所示:

The IBAction for the event type Value changed can be created as:

事件类型“值已更改”的IBAction可以创建为:

码 (Code)

The code for the ViewController.swift is given below:

下面给出了ViewController.swift的代码:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var myLabel: UILabel!@IBOutlet weak var mySlider: UISlider!override func viewDidLoad() {super.viewDidLoad()createSliderProgrammatically()}@IBAction func mySliderValueChanged(_ sender: Any) {if (sender as! UISlider).tag == 101{myLabel.text = ("Programmatic Slider value is: \((sender as! UISlider).value)")}else{myLabel.text = ("Slider value is: \((sender as! UISlider).value)")}}func createSliderProgrammatically(){let mySlider = UISlider(frame:CGRect(x: 10, y: 100, width: 300, height: 20))mySlider.minimumValue = 0mySlider.maximumValue = 100mySlider.isContinuous = falsemySlider.tag = 101mySlider.tintColor = UIColor.greenmySlider.minimumTrackTintColor = UIColor.blackmySlider.maximumTrackTintColor = UIColor.redmySlider.minimumValueImage = UIImage(named: "ic_launcher")mySlider.maximumValueImage = UIImage(named: "ic_launcher")mySlider.setThumbImage(UIImage(named: "ic_launcher"), for: UIControl.State.normal)mySlider.addTarget(self, action: #selector(ViewController.mySliderValueChanged(_:)), for: .valueChanged)self.view.addSubview(mySlider)}}

In the above code, we’ve added a programmatically created UISlider at the top of our layout.
We’ve set the UIImage from the Assets folder.

在上面的代码中,我们在布局的顶部添加了以编程方式创建的UISlider。
我们已经从Assets文件夹中设置了UIImage。

To differentiate between the two UISliders we’ve set a tag on one of them.
The value displayed on the Label would be of the type.

为了区分两个UISlider,我们在其中一个上设置了标签。
标签上显示的值将是该类型。

The output of the application in action is given below:

实际应用程序的输出如下:

The values are displayed as a Float.

值显示为浮点数。

In order to make them snap to an integer, we can do the following in our above Swift class.

为了使它们捕捉到整数,我们可以在上面的Swift类中执行以下操作。

@IBAction func mySliderValueChanged(_ sender: Any) {let uiSlider = sender as! UISliderlet step:Float = 1let roundedStepValue = round(uiSlider.value / step) * stepuiSlider.value = roundedStepValueif (sender as! UISlider).tag == 101{myLabel.text = ("Programmatic Slider value is: \((uiSlider).value)")}else{myLabel.text = ("Slider value is: \((uiSlider).value)")}}

The output now looks like:

现在的输出如下所示:

That brings an end to this tutorial. You can download the project from the link below:

这样就结束了本教程。 您可以从下面的链接下载项目:

iOSUISlideriOSUI滑块

翻译自: https://www.journaldev.com/22833/ios-uislider

iOS UISlider相关推荐

  1. iOS UISlider数值与滑块联动

    级别:★☆☆☆☆ 标签:「UISlider」「QiSlider」「UISlider自定义」 作者: Xs·H 项目中有个需求是在滑动slider的时候要在滑块上方实时显示数值,而且数值要跟着滑块动.实 ...

  2. iOS UIslider 设定滑块的大小

    一般情况下,我们在用UIslider的时候,会发现默认的滑块不符合我们的UI需要,这个时候,我们需要修改它的大小,可以通过如下的方法 /*对原来的图片的大小进行处理@param image 要处理的图 ...

  3. iOS UISlider

    文章目录 UISlider常用属性与方法 使用:控制图片透明度 UISlider控件就是我们通常用于调节亮度,透明度,音量时会出现的滑动条.UISlider控件是通过滑块所处的位置来标识数值,它允许用 ...

  4. iOS UIButton之UIEdgeInsets详解

    级别:★★☆☆☆ 标签:「UIButton内偏移量」「titleEdgeInsets」「imageEdgeInsets」 作者: MrLiuQ 审校: QiShare团队 我们先看一下苹果官方对UIE ...

  5. iOS strong和copy的区别

    级别: ★☆☆☆☆ 标签:「iOS」「NSString」「strong和copy」 作者: MrLiuQ 在iOS开发中,几乎每天都会遇到NSString属性的声明, 在ARC内存管理机制下, NSS ...

  6. SwiftUI Bundle Resources Framework 基础教程

    Bundle Resources 位于应用程序,框架或插件包中的资源. 使用教程 捆绑软件是具有标准化层次结构的目录,其中包含可执行代码和该代码使用的资源. 捆绑软件包含可以在运行时访问的资源,例如图 ...

  7. SwiftUI IOSurface 基础教程

    IOSurface 跨多个进程共享硬件加速的缓冲区数据(帧缓冲区和纹理). 更有效地管理图像内存. 使用教程 IOSurface框架提供了适合跨进程边界共享的帧缓冲区对象. 它通常用于允许应用程序移动 ...

  8. SwiftUI Device Management 基础教程

    Device Management 远程管理组织的设备. 使用教程 部署移动设备管理(MDM)解决方案可使管理员安全地和远程地配置已注册的设备. 管理员使用Apple School Manager或A ...

  9. php pdo ttfb慢,接口速度慢问题查找(TTFB时间长)

    前些天自己写了一个网站,但是发现接口的速度按超级慢,业务逻辑并不复杂,原因究竟在哪呢? 首先说一下,我的数据库和项目均在同一台服务器上,按道理来说,接口访问本地的数据库应该会很快才对. 后来我发现线上 ...

最新文章

  1. 注入Attention,精度涨30%!谷歌发表最新多目标“动态抠图”模型
  2. Flutter开发Flutter与原生OC、Java的交互通信-1(47)
  3. ue4是什么意思_恋爱中,男生最喜欢什么相处模式?
  4. CV:Win10下深度学习框架安装之Tensorflow/tensorflow_gpu+Cuda+Cudnn(最清楚/最快捷)之详细攻略(图文教程)
  5. Syzmlw 让子弹飞迅雷下载
  6. 计算机组成原理实验(logisim)
  7. 数字电视 frontend tuner demod
  8. sqlserver200864位下载_microsoft sql server 2008官方下载|Microsoft SQL Server 200832/64位 完整版_ - 极光下载站...
  9. 抖音大数据,教你爬爬爬!
  10. 任务管理器被管理员停用怎么办
  11. chm文件打开出现已取消该网页的导航
  12. 云计算的认识和看法_我对云计算的认识
  13. Draggabilly中文文档
  14. iPad安卓协议是怎么实现功能的
  15. ROS msg 文件修改 报错
  16. Dapper - 微型 ORM 之王 (C#.NET)
  17. 万年历农历程序(抄表法)
  18. 功能强大的微信商城系统,欢迎体验
  19. 如何看待2023届,秋招面临的形势?
  20. python+nodejs+java+vue顾家装修网站

热门文章

  1. iOS开发之保存照片到自己创建的相簿
  2. 通用的linux下安装配置svn独立服务
  3. 软件开发再编写过程中,要尽可能地让开发者再后来可以很容易地找到软件的编写逻辑和思路结构。这才是我们需要追求的。...
  4. Asp.net中Global.asax
  5. [转载] 简单工厂模式和工厂方法模式在Python中的实现
  6. [转载] python.day02笔记
  7. [转载] 【数据处理】 python 极速极简画图——频数(率)分布直方图
  8. 使用Yii 1.1框架搭建第一个web应用程序
  9. 浅谈 Redis 与 MySQL 的耦合性以及利用管道完成 MySQL 到 Redis 的高效迁移
  10. hdu 3255 Farming(扫描线)