class YourVIEwController{ ... @IBOutlet weak var mapVIEw:MKMapVIEw ... overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() ... addMapTrackingbutton() } func addMapTrackingbutton(){ let image = UIImage(named: "trackme") as UIImage? let button = UIbutton(type: UIbuttonType.System) as UIbutton button.frame = CGRectMake(5,5,35,35) button.setimage(image,forState: .normal) button.backgroundcolor = .clearcolor() button.addTarget(self,action: #selector(YourVIEwController.centerMapOnUserbuttonClicked),forControlEvents:.touchUpInsIDe) self.mapVIEw.addSubvIEw(button) } func centerMapOnUserbuttonClicked() { self.mapVIEw.setUserTrackingMode( MKUserTrackingMode.Follow,animated: true) } ...}
斯威夫特4:
func addMapTrackingbutton(){ let image = UIImage(named: "trackme") as UIImage? let button = UIbutton(type: UIbuttonType.custom) as UIbutton button.frame = CGRect(origin: CGPoint(x:5,y: 25),size: CGSize(wIDth: 35,height: 35)) button.setimage(image,for: .normal) button.backgroundcolor = .clear button.addTarget(self,action: #selector(VIEwController.centerMapOnUserbuttonClicked),for:.touchUpInsIDe) mapVIEw.addSubvIEw(button)}@objc func centerMapOnUserbuttonClicked() { mapVIEw.setUserTrackingMode(MKUserTrackingMode.follow,animated: true)}总结
以上是内存溢出为你收集整理的ios – 使用MapKit将视图居中显示到用户位置的按钮全部内容,希望文章能够帮你解决ios – 使用MapKit将视图居中显示到用户位置的按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)