ScrollView content offset freezes compass

31 Views Asked by At

There is a scrollView on the screen, under it is a compass arrow. As soon as I move the contents of the scrollView up or down the screen, the compass stops working. When I release the scrollView, the compass is restored. When I rotate the compass arrow by changing the variable on the timer, shifting the contents of the scrollView does not affect this process. How to fix it?

import SwiftUI

import Foundation
import Combine
import CoreLocation

class CompassHeading: NSObject, ObservableObject, CLLocationManagerDelegate {
var objectWillChange = PassthroughSubject<Void, Never>()
var degrees: Double = .zero {
    didSet {
        objectWillChange.send()
    }
}

private let locationManager: CLLocationManager

override init() {
    self.locationManager = CLLocationManager()
    super.init()
    
    self.locationManager.delegate = self
    self.setup()
}

private func setup() {
    self.locationManager.requestWhenInUseAuthorization()
    
    if CLLocationManager.headingAvailable() {
        self.locationManager.startUpdatingHeading()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    self.degrees = -1 * newHeading.magneticHeading
}
}

struct ContentView: View {

@ObservedObject var compassHeading = CompassHeading()

var body: some View {
    
    VStack {
        ScrollView {
            Text("Some words\nSome words\nSome words\n")
                .padding()
        }
        
        Text("⬆️")
            .font(.system(size: 100))
            .rotationEffect(Angle(degrees: compassHeading.degrees))
    }
    
}

}

0

There are 0 best solutions below