[Xcode 实际操作]八、网络与多线程-(20)时间控件Timer定时功能

目录:[Swift]Xcode实际操作

本文将演示时间控件Timer定时功能的使用。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         
 9         //创建一个无限循环的定时动作,参数如下
10         Timer.scheduledTimer(timeInterval: 1,//设置间隔时间(S)
11          target: self, //代理对象
12          selector: #selector(ViewController.timerAction(_:)), //定时执行的方法
13          userInfo: "Strengthen", //传入方法的参数
14          repeats: true)//是否重复执行
15     }
16     
17     //添加一个方法,用来响应定时事件
18     //该方法包含一个参数,为当前正在执行的定时对象
19     @objc func timerAction(_ timer:Timer)
20     {
21         //获得定时对象的属性值,也即刚刚设置的参数值
22         let parameter = timer.userInfo
23         //在控制台打印输出相关日志
24         print("My name is \(parameter!).")
25     }
26 
27     override func didReceiveMemoryWarning() {
28         super.didReceiveMemoryWarning()
29         // Dispose of any resources that can be recreated.
30     }
31 }

 

转载于:https://www.cnblogs.com/strengthen/p/10070204.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在MKMapView地图上显示提示框,可以使用MKAnnotationView来实现。以下是在Swift的示例代码: 首先,需要定义一个实现了MKAnnotation协议的类来表示地图上的标注点,例如: ``` import MapKit class MyAnnotation: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?) { self.coordinate = coordinate self.title = title self.subtitle = subtitle } } ``` 然后,在MKMapViewDelegate的方法创建MKAnnotationView,并设置其属性,例如: ``` func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard let annotation = annotation as? MyAnnotation else { return nil } let identifier = "MyAnnotationView" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView?.canShowCallout = true annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) } else { annotationView?.annotation = annotation } return annotationView } ``` 在这个方法,我们创建了一个MKPinAnnotationView,并设置了它的canShowCallout属性为true,表示可以显示提示框。同时,我们还设置了它的rightCalloutAccessoryView属性为一个UIButton,表示在提示框的右侧添加一个详情按钮。 最后,我们需要在ViewController创建MyAnnotation对象,并将它添加到MKMapView,例如: ``` let annotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118), title: "Apple Inc.", subtitle: "Cupertino") mapView.addAnnotation(annotation) ``` 这样,当我们运行应用程序时,在地图上就会显示一个名为"Apple Inc."的标注点,并且当我们点击这个标注点时,就会显示一个提示框,并且在提示框的右侧会有一个详情按钮。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值