首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在地图上逐渐改变颜色为PIN (标记位置)

在地图上逐渐改变颜色为PIN (标记位置)
EN

Stack Overflow用户
提问于 2020-08-06 13:20:25
回答 1查看 42关注 0票数 0

我有一个使用iOS UIMap地图的应用程序屏幕。在地图上有PIN(苹果的人机界面的标记位置),它指示地震计,每6秒地图更新和其中一些地震计振动和变红。我必须给地图上的PINs一个褪色的效果,从明亮的红色到非常轻的红色。

代码语言:javascript
复制
if annotation.identifier == "redpin" {
   view.pinTintColor = .red
   DispatchQueue.main.asyncAfter(deadline: .now()+2.0 ) {
      view.pinTintColor = UIColor(red: 255/255, green: 110/255, blue: 110/255, alpha: 1)
   }
   DispatchQueue.main.asyncAfter(deadline: .now()+2.0 ) {
      view.pinTintColor = UIColor(red: 255/255, green: 180/255, blue: 180/255, alpha: 1)
   }
   DispatchQueue.main.asyncAfter(deadline: .now()+2.0 ) {
      view.pinTintColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
   }
}

我使用了这个方法,使用了一个计时器来改变每2秒震动一次的PIN的颜色,手动产生这种褪色效果。对于第一次更新时振动的第一次更新,这是很好的,但随后的那些保持最后的颜色(白色),而不是红-红-轻-白色。有谁可以帮我?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-08 14:29:18

DispatchQueue.main.asyncAfter(deadline: .now()+2.0)块的调用不会等到它完成.下一行代码将立即执行。

因此,在您发布的代码中,您实际上是在说:

代码语言:javascript
复制
set the pin to Red
wait 2 seconds
set the pin to Medium Red    
set the pin to Light Red
set the pin to White

您可以使用以下方法来修复这个问题:

代码语言:javascript
复制
.now()+0.25 // wait 1/4 second
.now()+0.50 // wait 1/2 second
.now()+0.75 // wait 3/4 second

因此,每个块将在前一个块之后执行1/4秒。

色彩不能动画,所以你不能在UIView.animate()块中使用它。

然而,红-白-红-红的颜色变化确实看上去有点“淡雅”,所以你可能会对以下内容感到满意:

代码语言:javascript
复制
func animPinColor(_ v: MKPinAnnotationView) -> Void {
    // set pin to white
    v.pinTintColor = .white
    // wait 0.25 seconds ... adjust as desired
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
        // set pin back to red
        v.pinTintColor = .red
    }
}

或者,如果你想要更多的控制:

代码语言:javascript
复制
func animPinColor(_ v: MKPinAnnotationView) -> Void {

    // verbose, for clarity
    let red       = UIColor(red: 255/255, green:       0, blue:       0, alpha: 1)
    let mediumRed = UIColor(red: 255/255, green: 110/255, blue: 110/255, alpha: 1)
    let lightRed  = UIColor(red: 255/255, green: 180/255, blue: 180/255, alpha: 1)
    let white     = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)

    let colors: [UIColor] = [
        mediumRed,
        lightRed,
        white,
        lightRed,
        mediumRed,
        red,
    ]

    // 1/2 second color animation ... adjust as desired
    let totalDuration: Double = 0.5
    // each step will take totalDuration divided by total steps
    let relativeDuration = totalDuration / Double(colors.count)
    for i in 0..<colors.count {
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(i) * relativeDuration) {
            v.pinTintColor = colors[i]
        }
    }
    
}

你可以玩的持续时间/时间,甚至可以添加更多的“红色的阴影”,以获得一个更平滑的褪色。

编辑

根据您发布的代码,我假设您引用了一个MKPinAnnotationView

如果是这样,则使用如下:

代码语言:javascript
复制
if annotation.identifier == "redpin" {
    //view.pinTintColor = .red
    animPinColor(view)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63284522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档