是否可以在 Swift 中添加多个触觉来一个接一个地播放?

问题描述

我一直在尝试根据我有动画的每个视图的时间添加多个触觉事件,但是在测试时它只播放第一个触觉并在那里停止。我试过在网上搜索是否可以在短时间内播放多个触觉(触觉引擎是否准备好快速触发多次?)但找不到任何东西。

有人知道这是否可行,如果可以,为什么我的可能无法正常工作?

我尝试了 2 种不同的添加多个触感的方法

这是我的第一种方法代码,我首先初始化一个新实例,然后在每个动画块中播放相同的触觉,动画之间有 0.25 的延迟:

func animateSunriseAndSunsetViews() {
    let generator = UIImpactFeedbackGenerator(style: .medium)
    generator.prepare() //prepares Taptic Engine for use
    
    sunriseView.alpha = 0
    morningGoldenHourView.alpha = 0
    morningWeatherLabel.alpha = 0
    morningGoldenHourWeatherView.alpha = 0
    eveningGoldenHourView.alpha = 0
    sunsetView.alpha = 0
    eveningWeatherLabel.alpha = 0
    eveningGoldenHourWeatherView.alpha = 0
    nextGoldenHourLabel.alpha = 0
    
    UIView.animate(withDuration: 1,delay: 0,options: .curveEaseInOut) {
        self.sunriseView.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 0.25,options: .curveEaseInOut) {
        self.morningGoldenHourView.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 0.5,options: .curveEaseInOut) {
        self.eveningGoldenHourView.alpha = 100
        generator.impactOccurred()
    }
    UIView.animate(withDuration: 1,delay: 0.75,options: .curveEaseInOut) {
        self.sunsetView.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 1,options: .curveEaseInOut) {
        self.morningWeatherLabel.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 1.25,options: .curveEaseInOut) {
        self.morningGoldenHourWeatherView.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 1.5,options: .curveEaseInOut) {
        self.eveningWeatherLabel.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 1.75,options: .curveEaseInOut) {
        self.eveningGoldenHourWeatherView.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
    UIView.animate(withDuration: 1,delay: 2,options: .curveEaseInOut) {
        self.nextGoldenHourLabel.alpha = 100
        generator.impactOccurred() //plays haptic here
    }
}

这是我尝试过的第二种方法,我在每个动画块中初始化并播放新的触觉:

func animateSunsetViews() {
    
    eveningGoldenHourView.alpha = 0
    sunsetView.alpha = 0
    eveningWeatherLabel.alpha = 0
    eveningGoldenHourWeatherView.alpha = 0
    nextGoldenHourLabel.alpha = 0
    
    UIView.animate(withDuration: 1,options: .curveEaseInOut) {
        self.eveningGoldenHourView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1,options: .curveEaseInOut) {
        self.sunsetView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1,options: .curveEaseInOut) {
        self.eveningWeatherLabel.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1,options: .curveEaseInOut) {
        self.eveningGoldenHourWeatherView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1,options: .curveEaseInOut) {
        self.nextGoldenHourLabel.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
}

到目前为止,两种方法都只播放第一个触觉并在那里停止。如果这有效,有什么想法吗?谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)