ios uistepper

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

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

UIStepper (UIStepper)

UIStepper is a basic UI control element that is used to increment/decrement values. This UI element is commonly used in shopping cart applications to increase or decrease the quantity.

UIStepper是一个基本的UI控件元素,用于增加/减少值。 此UI元素通常在购物车应用程序中用于增加或减少数量。

A UIStepper consists of the following important properties.

UIStepper由以下重要属性组成。

  • minimum/maximum values – The upper and lower limits of the UIStepper.最小值/最大值– UIStepper的上限和下限。
  • stepValue – The increment/decrement value. By default, this is 1. A stepValue property is a Double.stepValue –增量/减量值。 默认情况下,该值为1。stepValue属性为Double。
  • autorepeat – If this is true, pressing and holding the button would keep incrementing/decrementing the value. Otherwise pressing would increment/decrement only once. By default this is true.自动重复–如果为true,则按住该按钮将保持递增/递减值。 否则,按只会增加/减少一次。 默认情况下,这是正确的。
  • continuous – If this is true, the value would be changed immediately as soon as the button is touched. By default it is true.连续–如果为true,则一旦触摸该按钮,该值将立即更改。 默认情况下为true。
  • wrap – When this is true, the UIStepper increments/decrements in cycles. That means that once the minimum value is reached, decrementing further would start from the maximum value and vice-versa.
    When this is false once the UIStepper reaches the min/max you cannot click any further. The buttons get disabled as well.wrap –如果为true,则UIStepper以周期为单位递增/递减。 这意味着一旦达到最小值,则将从最大值开始进一步减小,反之亦然。
    如果为false,则一旦UIStepper达到最小值/最大值,您将无法再单击它。 这些按钮也被禁用。

Some more important properties and functions –

一些更重要的属性和功能–

  • tintColor – Used to set the color of the key elements of the UIStepper.tintColor –用于设置UIStepper的关键元素的颜色。
  • setIncrementImage(image:, for:) – Sets the image for the increment button for the state assigned. A UIImage instance is passed. The states can be normal, focused, highlighted.setIncrementImage(image:, for:) –为分配的状态设置增量按钮的图像。 传递了一个UIImage实例。 状态可以是正常,集中,突出显示。
  • setDecrementImage(image:, for:) – Sets the same for the decrement button.setDecrementImage(image:, for:) –为减量按钮设置相同的值。
  • setDividerImage() – Used to set the divider image.setDividerImage() –用于设置分隔线图像。

The image size must be under 25X25 to fit nicely inside the UIStepper.

图片大小必须小于25X25,才能很好地放入UIStepper中。

We’ll look at the click listener functions in the XCode project that we implement next.

我们将在接下来实现的XCode项目中查看click监听器函数。

实作 (Implementation)

Create a new XCode project. In your Main.storyboard, drag a UIStepper and a Label on the ViewController as shown below:

创建一个新的XCode项目。 在Main.storyboard中,将UIStepper和Label拖到ViewController上,如下所示:

  1. Set the constraints on the UIStepper to center horizontal and vertical in the parent. Set the Label to center horizontal and add a vertical spacing to it from the UIStepper. Link these Outlets to the ViewController.swift将UIStepper上的约束设置为在父级中水平和垂直居中。 将Label设置为水平居中,并从UIStepper向其添加垂直间距。 将这些插座链接到ViewController.swift
  2. In the right pane the attributes can be changed. Or they can be done programmatically.在右窗格中,可以更改属性。 或者可以通过编程来完成。

The ViewController.swift code is given below:

下面给出了ViewController.swift代码:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var label: UILabel!@IBOutlet weak var myStepper: UIStepper!override func viewDidLoad() {super.viewDidLoad()myStepper.addTarget(self, action: #selector(ViewController.stepperValueChanged(_:)), for: .valueChanged)}@objc func stepperValueChanged(_ sender:UIStepper!){label.text = String(sender.value)}
}

Using the addTarget method we set the function to be called when the value of the UIStepper is changed by setting the event to valueChanged.

通过使用addTarget方法,我们通过将事件设置为valueChanged来设置UIStepper的值更改时要调用的函数。

In the stepperValueChanged function, we get the current step value using sender.value and set it on the Label.

stepperValueChanged函数中,我们使用sender.value获取当前步骤值,并将其设置在Label上。

sender.value is of the type Double, so we’ve converted it into a String.

sender.value的类型为Double,因此我们已将其转换为String。

The output when the application is run on a simulator is:

当应用程序在模拟器上运行时,输出为:

The value is displayed in Decimal(Double). When the UIStepper button is pressed, it keeps incrementing and goes over the maximum value which was 20.

该值以Decimal(Double)显示。 当按下UIStepper按钮时,它会不断增加并超过最大值20。

自动重复并包装。 没有连续。 (autorepeat and wrap. No continuous.)

myStepper.isContinuous = false

无自动重复,无环绕,无连续 (No Autorepeat, No wrap, No continuous)

myStepper.isContinuous = false
myStepper.autorepeat = false
myStepper.wraps = false

As you can see the + button is disabled.

如您所见,+按钮已禁用。

添加色调颜色和阶跃值 (Adding Tint Color and Step Value)

In order to display an Integer value, we need to convert the double to Int first and then to a String.

为了显示Integer值,我们需要先将double转换为Int,然后转换为String。

label.text = String(sender.value)

To add a Tint color and step value, set the following inside the viewDidLoad method.

要添加色调颜色和阶跃值,请在viewDidLoad方法内设置以下内容。

myStepper.tintColor = UIColor.brown
myStepper.stepValue = 2.0

The output of the application in action is :

实际应用程序的输出为:

Let’s create another UIStepper in which we’ll set custom images.

让我们创建另一个UIStepper,在其中设置自定义图像。

以编程方式创建UIStepper (Creating UIStepper Programmatically)

In the following ViewController.swift, we’ve updated the viewDidLoad method to add a custom UIStepper with image assets set on the left, right and divider elements. You can find the images in the project source code attached at the end of this tutorial.

在下面的ViewController.swift中,我们更新了viewDidLoad方法,以添加一个自定义UIStepper,并在左,右和除法器元素上设置了图像资源。 您可以在本教程结尾处随附的项目源代码中找到图像。

We’ve added the AutoLayout constraints programmatically as well:

我们还以编程方式添加了AutoLayout约束:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var label: UILabel!@IBOutlet weak var myStepper: UIStepper!override func viewDidLoad() {super.viewDidLoad()myStepper.tintColor = UIColor.brownmyStepper.stepValue = 2.0myStepper.addTarget(self, action: #selector(ViewController.stepperValueChanged(_:)), for: .valueChanged)let customStepper = UIStepper()customStepper.minimumValue = 10customStepper.maximumValue = 100customStepper.tintColor = UIColor.redcustomStepper.setIncrementImage(UIImage(named: "happy.png"), for: .normal)customStepper.setDecrementImage(UIImage(named: "sad.png"), for: .normal)customStepper.setDividerImage(UIImage(named: "divider.png"), forLeftSegmentState: .normal, rightSegmentState: .normal)customStepper.translatesAutoresizingMaskIntoConstraints = falsecustomStepper.addTarget(self, action: #selector(ViewController.stepperValueChanged(_:)), for: .valueChanged)self.view.addSubview(customStepper)[customStepper.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),customStepper.topAnchor.constraint(equalTo: myStepper.bottomAnchor, constant: 20)].forEach{$0.isActive = true}}@objc func stepperValueChanged(_ sender:UIStepper!){label.text = String(Int(sender.value))}
}

The output of the application in action is given below:

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

As you can see the tintColor gets applied on the images as well.

如您所见,tintColor也将应用于图像。

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

本教程到此结束。 您可以从下面的链接下载项目。

iOSUIStepperiOSUIStepper

翻译自: https://www.journaldev.com/22259/ios-uistepper

ios uistepper

ios uistepper_iOS UIStepper相关推荐

  1. iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用

    步进UIStepper.滑动块UISlider:当它们作为事件,被触发时,它们的值会发生改变.正因为如此,触发该事件时,可以一张一张翻阅浏览图片,,,, 步进UIStepper: @property( ...

  2. iOS开篇——UI之UIStepper (计步器)

    UIStepper * stepper = [[UIStepper alloc]initWithFrame:CGRectMake(50, 100, 150, 40)];//********最小值和最大 ...

  3. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

  4. 开源中国iOS客户端学习——(一)Prefix.pch文件

    2019独角兽企业重金招聘Python工程师标准>>> 当我们新建一个工程的时候,在Supporting FIles文件下会看到一个以  -Prefix.pch结尾文件的文件,pch ...

  5. 《iOS 6核心开发手册(第4版)》——2.1节UIControl类

    本节书摘来自异步社区<iOS 6核心开发手册(第4版)>一书中的第2章,第2.1节UIControl类,作者 [美]Erica Sadun,更多章节内容可以访问云栖社区"异步社区 ...

  6. iOS开发③UIView

    UILabel Lable的作用是显示不可编辑的文字. 属性检查器 Text:Label显示的文字 Color:文字的颜色 Font:字体和字号 Alignment:文本的对齐方式 Lines:设置L ...

  7. iOS基本UI控件总结

    包括以下几类: //继承自NSObject:(暂列为控件) UIColor *_color;    //颜色 UIImage *_image;    //图像 //继承自UIView:只能相应手势UI ...

  8. 【iOS 开发】Objective-C 运算符

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41624613 参考文章 : 1.[iOS 开发]Object-C 运算符 2.[iO ...

  9. iOS开发知识点总结

    main文件做了这几件事:1. 创建当前的应用程序2. 根据4个参数的最后为应用程序设置代理类(默认情况下是AppDelegate)3. 将appDelegate 和 应用程序 建立关联(指定代理,) ...

最新文章

  1. Tomcat高级部分-使用特定模块和软件反向代理请求到后端tomcat实现负载均衡和session保持...
  2. Python Number(数字)
  3. 因为征信原因,买房的2万定金没了
  4. SQL的数据定义功能及语句:
  5. linux分配内核,linux 内核分配算法
  6. 学校管理系统有望突破信息瓶颈
  7. java nio 心跳包_请问Java中Socket的心跳包如何实现?
  8. linux跳过文件系统检查,centos 文件系统检测错误
  9. 代码雨代码源复制_p#39;y代码雨怎么做
  10. 系统集成项目管理工程师2021年报名时间
  11. 台式计算机怎么加一个硬盘,台式电脑增加硬盘_台式电脑增加硬盘图解
  12. css属性:hover
  13. 动漫绘画软件优动漫PAINT最近所用文件
  14. 考试系统之选择题评分
  15. 【Python_006】Python爬虫抓取豆瓣电影影评
  16. 为什么王者荣耀总是服务器中断,王者荣耀服务器正在维护中怎么回事 7月4日王者荣耀进不去怎么办...
  17. 教你玩Robocode(5)——调试技巧
  18. 上海交大计算机系07,08就业一栏(硕士生)
  19. 关于在python的tkinter界面中镶嵌mayplotlib动态图
  20. JAVA C++ 经典书箱转让

热门文章

  1. asp.net GridView控件的列属性
  2. 课堂练习--最大子数组和
  3. fenby C语言 P11
  4. 使用annotation配置hibernate(1)
  5. [转载] Python从字符串中删除字符
  6. cocos creator android之微信开放平台修改签名 baseResp.errCode=-6
  7. zbb20180613 Spring MVC实现大文件下载功能
  8. 分享:Python中的位运算符
  9. Yoga安装Ubuntu后,wifi和亮度调节问题
  10. fshc模块fsch2mcu_if理解