当前位置: 首页 > news >正文

iOS开发Swift-3-UI与按钮Button-摇骰子App

1.创建新项目Dice

 2.图标

 删去AppIcon,将解压后的AppIcon.appiconset文件拖入Assets包。

 3.将素材点数1-6通过网页制作成2x,3x版本并拖入Asset。

 4.设置对应的UI。

 5.拖入Button组件并设置style。

 6.Ctrl加拖拽将Button拖拽到ViewController里,并设置Connection,Name等,并点击Connect。

 同样方式创建两个骰子的代码。

import UIKitclass ViewController: UIViewController {@IBOutlet weak var diceImageView1: UIImageView!@IBOutlet weak var diceImageView2: UIImageView!@IBAction func roll(_ sender: Any) {}override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}
}

 7.继续编写代码创造随机数。

import UIKitclass ViewController: UIViewController {@IBOutlet weak var diceImageView1: UIImageView!@IBOutlet weak var diceImageView2: UIImageView!@IBAction func roll(_ sender: Any) {var index1: Int = Int.random(in: 1...6)var index2: Int = Int.random(in: 1...6)diceImageView1.image = UIImage(named: "dice\(index1)")diceImageView2.image = UIImage(named: "dice\(index2)")    }override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

8.启动运行

9.程序加载时就随机扔骰子

import UIKitclass ViewController: UIViewController {let diceArr = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]@IBOutlet weak var diceImageView1: UIImageView!@IBOutlet weak var diceImageView2: UIImageView!@IBAction func roll(_ sender: Any) {//调用摇骰子函数updateDiceImages()}//当程序加载的时候的活动override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.//调用摇骰子函数updateDiceImages()}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func updateDiceImages(){var index1: Int = Int.random(in: 0...5)var index2: Int = Int.random(in: 0...5)diceImageView1.image = UIImage(named: diceArr[index1])diceImageView2.image = UIImage(named: diceArr[index2])    }}

10.当用户摇晃骰子后调用摇骰子函数。

import UIKitclass ViewController: UIViewController {let diceArr = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]@IBOutlet weak var diceImageView1: UIImageView!@IBOutlet weak var diceImageView2: UIImageView!@IBAction func roll(_ sender: Any) {//调用摇骰子函数updateDiceImages()}//当程序加载的时候的活动override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.//调用摇骰子函数updateDiceImages()}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func updateDiceImages(){var index1: Int = Int.random(in: 0...5)var index2: Int = Int.random(in: 0...5)diceImageView1.image = UIImage(named: diceArr[index1])diceImageView2.image = UIImage(named: diceArr[index2])    }//当用户摇晃手机时触发摇骰子override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {updateDiceImages()}}

http://www.lryc.cn/news/145410.html

相关文章:

  • 1、[春秋云镜]CVE-2022-32991
  • pdf如何删除其中一页?了解一下这几种删除方法
  • PO设计模式是selenium自动化测试中最佳的设计模式之一
  • yolov8使用C++推理的流程及注意事项
  • 深度思考计算机网络面经之二
  • 老年人跌倒智能识别算法 opencv
  • ros2官方文档(基于humble版本)学习笔记
  • 可拖动表格
  • C++语法基础
  • Windi CSS和Tailwind CSS以及UnoCSS
  • c++ opencv将彩色图像按连通域区分
  • 〖程序员的自我修养 - 认知剖析篇⑩〗- 学习编程的高效率方法
  • 前端基础1——HTML标记语言
  • 2.1: Dubbo的基本应用-负载均衡,集群容错,服务降级
  • 正则常见问题及解决方案
  • docker发布项目及使用外部文件的情况处理
  • CSS 中哪些属性可以继承
  • vue cli构建的项目出现 Uncaught runtime errors
  • 透过源码理解Flutter InheritedWidget
  • 天去面试的时候,遇到一个问题。我三个任务,ABC,我怎么让A执行完执行B,B执行完执行C 3个并行线程,如何解决。程池的核心运行原理和参数。
  • 使用finksql方式将mysql数据同步到kafka中,每次只能同步一张表
  • ios开发 swift5 苹果系统自带的图标 SF Symbols
  • Linux内核源码分析 (3)调度器的实现
  • 网络安全法+网络安全等级保护
  • 持续集成对软件项目管理的作用
  • 【Qt QAxObject】使用 QAxObject 高效任意读写 Excel 表
  • java八股文面试[多线程]——自旋锁
  • 分布式系统的多数据库,实现分布式事务回滚(1.7.0 seata整合2.0.4nacos)
  • PDF可以修改内容吗?有什么注意的事项?
  • 自动泊车的自动驾驶控制算法