第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

快速注釋不同圖像

快速注釋不同圖像

神不在的星期二 2019-11-14 10:14:19
我設(shè)法在Swift中為注解圖釘獲得了一個(gè)自定義圖標(biāo),但是現(xiàn)在我仍然對(duì)2個(gè)不同的注解使用不同的圖像?,F(xiàn)在,一個(gè)按鈕會(huì)向地圖添加注釋。應(yīng)該有另一個(gè)按鈕,該按鈕也添加了注釋,但帶有另一個(gè)圖標(biāo)。有沒(méi)有辦法為此使用復(fù)用ID?class ViewController: UIViewController, MKMapViewDelegate {@IBOutlet weak var Map: MKMapView!@IBAction func btpressed(sender: AnyObject) {    var lat:CLLocationDegrees = 40.748708    var long:CLLocationDegrees = -73.985643    var latDelta:CLLocationDegrees = 0.01    var longDelta:CLLocationDegrees = 0.01    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)    Map.setRegion(region, animated: true)    var information = MKPointAnnotation()    information.coordinate = location    information.title = "Test Title!"    information.subtitle = "Subtitle"    Map.addAnnotation(information)}func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {    if !(annotation is MKPointAnnotation) {        return nil    }    let reuseId = "test"    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)    if anView == nil {        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)        anView.image = UIImage(named:"1.png")        anView.canShowCallout = true    }    else {        anView.annotation = annotation    }    return anView}
查看完整描述

3 回答

?
鴻蒙傳說(shuō)

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

在viewForAnnotation委托方法中,設(shè)置調(diào)用該方法所image依據(jù)的依據(jù)annotation。


一定要做到這一點(diǎn)后視圖被出隊(duì)或創(chuàng)建的(而不是只在if anView == nil部分)。否則,使用出隊(duì)視圖的注釋將顯示以前使用該視圖的注釋的圖像。


基本而言MKPointAnnotation,一種區(qū)分注釋的粗略方法是通過(guò)注釋,title但這不是很靈活。


更好的方法是使用實(shí)現(xiàn)MKAnnotation協(xié)議的自定義注釋類(一種簡(jiǎn)便的方法是子類化MKPointAnnotation),并添加所需的任何屬性以幫助實(shí)現(xiàn)自定義邏輯。


在自定義類中,添加一個(gè)屬性,例如imageName,您可以使用該屬性基于注釋自定義圖像。


此示例子類MKPointAnnotation:


class CustomPointAnnotation: MKPointAnnotation {

    var imageName: String!

}

創(chuàng)建類型的注釋CustomPointAnnotation并設(shè)置其imageName:


var info1 = CustomPointAnnotation()

info1.coordinate = CLLocationCoordinate2DMake(42, -84)

info1.title = "Info1"

info1.subtitle = "Subtitle"

info1.imageName = "1.png"


var info2 = CustomPointAnnotation()

info2.coordinate = CLLocationCoordinate2DMake(32, -95)

info2.title = "Info2"

info2.subtitle = "Subtitle"

info2.imageName = "2.png"

在中viewForAnnotation,使用imageName屬性設(shè)置視圖的image:


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if !(annotation is CustomPointAnnotation) {

        return nil

    }


    let reuseId = "test"


    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

    if anView == nil {

        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

        anView.canShowCallout = true

    }

    else {

        anView.annotation = annotation

    }


    //Set annotation-specific properties **AFTER**

    //the view is dequeued or created...


    let cpa = annotation as CustomPointAnnotation

    anView.image = UIImage(named:cpa.imageName)


    return anView

}


查看完整回答
反對(duì) 回復(fù) 2019-11-14
?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊

借助Anna和Fabian Boulegue的iOS Swift代碼:


import UIKit

import MapKit


class ViewController: UIViewController, MKMapViewDelegate {


    @IBOutlet weak var mapView: MKMapView!


    override func viewDidLoad() {

        super.viewDidLoad()


        self.mapView.delegate = self


        var info1 = CustomPointAnnotation()

        info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        info1.title = "Info1"

        info1.subtitle = "Subtitle"

        info1.imageName = "flag.png"


        var info2 = CustomPointAnnotation()

        info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)

        info2.title = "Info2"

        info2.subtitle = "Subtitle"

        info2.imageName = "flag.png"


        mapView.addAnnotation(info1)

        mapView.addAnnotation(info2)

    }


    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {


        println("delegate called")


        if !(annotation is CustomPointAnnotation) {

            return nil

        }


        let reuseId = "test"


        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

        if anView == nil {

            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

            anView.canShowCallout = true

        }

        else {

            anView.annotation = annotation

        }


        //Set annotation-specific properties **AFTER**

        //the view is dequeued or created...


        let cpa = annotation as CustomPointAnnotation

        anView.image = UIImage(named:cpa.imageName)


        return anView

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


class CustomPointAnnotation: MKPointAnnotation {

    var imageName: String!

}


查看完整回答
反對(duì) 回復(fù) 2019-11-14
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

需要將注釋向下轉(zhuǎn)換為CustomPointAnnotation。更改為let pickupCustomAnno = annotation as! CustomPointAnnotation

查看完整回答
反對(duì) 回復(fù) 2019-11-14
  • 3 回答
  • 0 關(guān)注
  • 627 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)