SwiftUi ගැටලුව අන්ක 02 - mouse scroll එකෙන් zoom කිරීම

tarson

Well-known member
  • Feb 25, 2009
    8,673
    5,872
    113
    පුලුවන් කෙනෙක් මොකක්ද අව්ල කියල කියන්න. මේක මවුස් එකෙන් ස්ක්‍රොල් කරනකොට මැද තියෙන rectangle එක සූම් වෙන එකක්.
    මේකෙ කෝඩ් එක රන් වෙනව errors මුකුත් නෑ, ඒත් මේකෙ එක පාරක් ස්ක්‍රොල් කරාම දිගටම සූම් වෙනව නවත්තන්න බෑ, සූම් අවුට් කලොත් දිගටම සූම් අවුට් වෙනව. මේකත් පුලුවන් තරම් ට්‍රයි කලා හදන්න, ඒත් මොන රෙද්දක් කරලවත් දිගටම ඔටෝ සූම් වෙන්නෙ මොකෝ කියල හිතාගන්න බෑ.

    මවුස් ඉවෙන්ට් එකේදි ` thisClassLocal.thisDownRefreshState.toggle()` ටොග්ල් වෙන්න හැදුවෙ මවුස් එකෙන් එන අන්ක ඒකාකාරි නැති නිසා. ස්ක්‍රොල් කරනකොට ලස්සනට සූම් වෙන්න, සූම් අවුට් වෙන්නයි ඕනෙ.

    Swift:
    import SwiftUI
    import Combine
    
    @main
    struct This4App: App {
     
      @State var subs = Set<AnyCancellable>()
      @StateObject var thisClassLocal = ThisClass(thisIn: 7, thisUpRefreshState: true, thisDownRefreshState: true, thisScaleBy: 1)
    
        var body: some Scene {
        WindowGroup {
         ThisView1()
          .onAppear {ThisFunction()}
        .environmentObject(thisClassLocal)
       } }}
    
    struct ThisView1: View {
    
        @State var scaleby: CGFloat = 2
        @EnvironmentObject var thisClassLocal2 : ThisClass
    
        var body: some View {
        VStack {
             Rectangle()
               .fill(.gray)
              .frame(width: 100, height: 100)
       }
      .padding()
          .scaleEffect(scaleby)
          .onChange(of: thisClassLocal2.thisDownRefreshState) { newValue in
             withAnimation { scaleby -= 2 }
         }
    
            .onChange(of: thisClassLocal2.thisUpRefreshState) { newValue in
             withAnimation { scaleby += 2
    
            } }}
    
    extension This4App {
    
    func ThisFunction() {
    
        NSApp.publisher(for: \.currentEvent)
                    .filter { event in event?.type == .scrollWheel }
                    .throttle(for: .milliseconds(200),
                              scheduler: DispatchQueue.main,
                              latest: true)
                    .sink {
    
                        if let event = $0 {
                            if event.deltaY > 0 {
                               thisClassLocal.thisUpRefreshState.toggle()
                            }
    
                            if event.deltaY < 0 {
                             thisClassLocal.thisDownRefreshState.toggle()
    
                            } } }
             .store(in: &subs)
     }}
    
    class ThisClass: ObservableObject {
    
        @Published var thisUpRefreshState: Bool
        @Published var thisDownRefreshState: Bool
    
        init(thisIn: CGFloat, thisUpRefreshState: Bool, thisDownRefreshState: Bool, thisScaleBy: CGFloat) {
    
        self.thisUpRefreshState = thisUpRefreshState
        self.thisDownRefreshState = thisDownRefreshState
        }
    
    }

    @uia9030
    @Solo Rider

    ප්‍රශ්න තියෙනවනම් අහන්න, දන්න විදිහට කියන්නම්, ප්‍රශ්න නැති අනිත් අය බම්ප් එකක් දාන් යන්න 🍻
     
    Last edited:
    • Like
    Reactions: david345

    david345

    Active member
  • May 20, 2020
    251
    209
    43
    පුලුවන් කෙනෙක් මොකක්ද අව්ල කියල කියන්න. මේක මවුස් එකෙන් ස්ක්‍රොල් කරනකොට මැද තියෙන rectangle එක සූම් වෙන එකක්.
    මේකෙ කෝඩ් එක රන් වෙනව errors මුකුත් නෑ, ඒත් මේකෙ එක පාරක් ස්ක්‍රොල් කරාම දිගටම සූම් වෙනව නවත්තන්න බෑ, සූම් අවුට් කලොත් දිගටම සූම් අවුට් වෙනව. මේකත් පුලුවන් තරම් ට්‍රයි කලා හදන්න, ඒත් මොන රෙද්දක් කරලවත් දිගටම ඔටෝ සූම් වෙන්නෙ මොකෝ කියල හිතාගන්න බෑ.

    මවුස් ඉවෙන්ට් එකේදි ` thisClassLocal.thisDownRefreshState.toggle()` ටොග්ල් වෙන්න හැදුවෙ මවුස් එකෙන් එන අන්ක ඒකාකාරි නැති නිසා. ස්ක්‍රොල් කරනකොට ලස්සනට සූම් වෙන්න, සූම් අවුට් වෙන්නයි ඕනෙ.

    Swift:
    import SwiftUI
    import Combine
    
    @main
    struct This4App: App {
     
      @State var subs = Set<AnyCancellable>()
      @StateObject var thisClassLocal = ThisClass(thisIn: 7, thisUpRefreshState: true, thisDownRefreshState: true, thisScaleBy: 1)
    
        var body: some Scene {
        WindowGroup {
         ThisView1()
          .onAppear {ThisFunction()}
        .environmentObject(thisClassLocal)
       } }}
    
    struct ThisView1: View {
    
        @State var scaleby: CGFloat = 2
        @EnvironmentObject var thisClassLocal2 : ThisClass
    
        var body: some View {
        VStack {
             Rectangle()
               .fill(.gray)
              .frame(width: 100, height: 100)
       }
      .padding()
          .scaleEffect(scaleby)
          .onChange(of: thisClassLocal2.thisDownRefreshState) { newValue in
             withAnimation { scaleby -= 2 }
         }
    
            .onChange(of: thisClassLocal2.thisUpRefreshState) { newValue in
             withAnimation { scaleby += 2
    
            } }}
    
    extension This4App {
    
    func ThisFunction() {
    
        NSApp.publisher(for: \.currentEvent)
                    .filter { event in event?.type == .scrollWheel }
                    .throttle(for: .milliseconds(200),
                              scheduler: DispatchQueue.main,
                              latest: true)
                    .sink {
    
                        if let event = $0 {
                            if event.deltaY > 0 {
                               thisClassLocal.thisUpRefreshState.toggle()
                            }
    
                            if event.deltaY < 0 {
                             thisClassLocal.thisDownRefreshState.toggle()
    
                            } } }
             .store(in: &subs)
     }}
    
    class ThisClass: ObservableObject {
    
        @Published var thisUpRefreshState: Bool
        @Published var thisDownRefreshState: Bool
    
        init(thisIn: CGFloat, thisUpRefreshState: Bool, thisDownRefreshState: Bool, thisScaleBy: CGFloat) {
    
        self.thisUpRefreshState = thisUpRefreshState
        self.thisDownRefreshState = thisDownRefreshState
        }
    
    }

    @uia9030
    @Solo Rider

    ප්‍රශ්න තියෙනවනම් අහන්න, දන්න විදිහට කියන්නම්, ප්‍රශ්න නැති අනිත් අය බම්ප් එකක් දාන් යන්න 🍻

    මාත් SwiftUI ඉගෙන ගන්නවා තාම. මං කරන්නේ chatGPT දාලා error fix කරනවා. මේ code එක chatGPT copy paste කරලා issue එක ගැනයි ඒක හැෙදන්න ඕන විදියයි කියන්න. අනිවා මේක හදාගන්න පුලුවන්.