0

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

任何人都知道这是否可能,如果可以,为什么我的可能不起作用?

我尝试了 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, delay: 0, options: .curveEaseInOut) {
        self.eveningGoldenHourView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1, delay: 0.25, options: .curveEaseInOut) {
        self.sunsetView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1, delay: 0.5, options: .curveEaseInOut) {
        self.eveningWeatherLabel.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1, delay: 0.75, options: .curveEaseInOut) {
        self.eveningGoldenHourWeatherView.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
    UIView.animate(withDuration: 1, delay: 1, options: .curveEaseInOut) {
        self.nextGoldenHourLabel.alpha = 100
        
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success) //plays haptic here
    }
}

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

4

0 回答 0