darkmaterial

Untitled

Oct 6th, 2021
1,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 8.33 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Test3
  4. //
  5. //  Created by Daniel Zollitsch on 27.09.21.
  6. //
  7.  
  8. import Cocoa
  9.  
  10. @objc class buttontest : NSObject{
  11.     let button = NSButton(frame: CGRect(x: 100, y: 100, width: 150, height: 50))
  12.     @objc func printSomething() {
  13.         print("Hello")
  14.      //   print(NSApp.currentEvent)
  15.         if(NSApp.currentEvent?.type==NSEvent.EventType.leftMouseDragged){
  16.             self.button.frame.origin.x=CGFloat(NSApp.currentEvent?.locationInWindow.x ?? 0)-75.0
  17.             self.button.frame.origin.y=CGFloat(NSApp.currentEvent?.locationInWindow.y ?? 0)-25.0
  18.         }
  19.         self.button.title="Test14"
  20.       }
  21.  
  22.     func buttoninit(x: Int, y: Int) -> NSButton{
  23.         self.button.title="Test"
  24.         self.button.frame.origin.x=CGFloat(x)
  25.         self.button.frame.origin.y=CGFloat(y)
  26.         self.button.bezelStyle=NSButton.BezelStyle.rounded
  27.         self.button.target=self;
  28.         //button.action = Selector(ViewController.printSomething)
  29.         self.button.identifier=NSUserInterfaceItemIdentifier.init(rawValue: "TEST");
  30.         self.button.sendAction(on: [NSEvent.EventTypeMask.leftMouseUp,NSEvent.EventTypeMask.leftMouseDragged]);
  31.         self.button.action = #selector(self.printSomething)
  32.        
  33.        
  34.         return self.button
  35.     }
  36. }
  37.  
  38. class ViewController: NSViewController {
  39.    
  40.     var animated = false;
  41.     let scrollView = NSScrollView(frame: NSRect(x: 300, y: 300, width: 600, height: 200))
  42.     @objc func startscroll() {
  43.        print("Scroll Started")
  44.             print(NSApp.currentEvent)
  45.         var val = scrollView.contentView.frame.height
  46.         var val2 = scrollView.documentView?.frame.height ?? 0;
  47.         if(scrollView.contentView.bounds.origin.y>0 && scrollView.contentView.bounds.origin.y<=(val2-val)-1 ){
  48.             //scrollView.contentView.animator().setBoundsOrigin(NSMakePoint(0, scrollView.contentView.bounds.origin.y))
  49.             scrollView.contentView.layer?.removeAllAnimations()
  50.         }
  51.        
  52.        
  53.        //
  54.        
  55.        
  56.        
  57.      }
  58.     @objc func endscroll() {
  59.        print("end")
  60.    
  61.         print(NSApp.currentEvent ?? 0)
  62.         var val = scrollView.contentView.frame.height
  63.         var val2 = scrollView.documentView?.frame.height ?? 0;
  64.  
  65.        
  66.         if(scrollView.contentView.bounds.origin.y>0 && scrollView.contentView.bounds.origin.y<=(val2-val)-1 ){
  67.            
  68.         var offset = scrollView.contentView.bounds.origin.y
  69.              offset = round(offset / 60) * 60;
  70.         NSAnimationContext.runAnimationGroup({ context in
  71.             context.duration = 2
  72.             crollView.contentView.animator().setBoundsOrigin(NSMakePoint(0, offset))
  73.         }) {
  74.            
  75.         }}
  76.      }
  77.     var buttonvar: [buttontest] = []
  78.     private lazy var redBox = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
  79.     override func viewDidLoad() {
  80.         super.viewDidLoad()
  81.         view.addSubview(redBox)
  82.         redBox.wantsLayer = true
  83.         redBox.layer?.backgroundColor = NSColor.red.cgColor
  84.  
  85.         //button.init("Test12",self,Selector(printSomething())
  86.  
  87.         buttonvar.append(buttontest());
  88.         buttonvar.append(buttontest());
  89.      
  90.         self.view.addSubview(buttonvar[0].buttoninit(x:100,y:100))
  91.         self.view.addSubview(buttonvar[1].buttoninit(x:300,y:300))
  92.         let slider = NSSlider(frame: CGRect(x: 10, y: 10, width: 100, height: 200))
  93.         slider.minValue=0;
  94.         slider.maxValue=255;
  95.         slider.sliderType=NSSlider.SliderType.linear;
  96.         slider.action=#selector(printslider)
  97.         slider.target=self;
  98.         slider.floatValue=50.5
  99.         self.view.addSubview(slider)
  100.         //self.view.addSubview(buttontest().buttoninit())
  101.         // Do any additional setup after loading the view.
  102.    
  103.         scrollView.translatesAutoresizingMaskIntoConstraints = false
  104.         scrollView.borderType = .bezelBorder
  105.         scrollView.backgroundColor = NSColor.gray
  106.         scrollView.hasVerticalScroller = true
  107.         scrollView.hasHorizontalScroller = true
  108.         scrollView.frame.origin.x=300;
  109.         scrollView.frame.origin.y=300;
  110.  
  111.         //scrollView.allowsMagnification = true
  112.         self.view.addSubview(scrollView)
  113.    
  114.      
  115.         let clipView = NSClipView()
  116.         scrollView.contentView = clipView
  117.         clipView.backgroundColor = NSColor.blue
  118.  
  119.         // Initial document view
  120.         let documentView = NSView(frame: NSRect(x: 0, y: 0, width: 1200, height: 300))
  121.         documentView.wantsLayer = true
  122.         scrollView.documentView = documentView
  123.         documentView.layer?.backgroundColor = NSColor.red.cgColor
  124.         documentView.layer?.borderWidth = 0
  125.         documentView.layer?.borderColor = NSColor.darkGray.cgColor
  126.         clipView.scaleUnitSquare(to: NSSize(width: 1, height: 1))
  127.         clipView.postsBoundsChangedNotifications = true
  128.         /*NotificationCenter.default.addObserver(self,
  129.                                                selector: #selector(collectionViewDidScroll(notification:)),
  130.                                                name: NSView.boundsDidChangeNotification,
  131.                                                object: clipView)*/
  132.         /*NotificationCenter.default.addObserver(self,
  133.                                                selector: #selector(printslider),
  134.                                                name: NSView.boundsDidChangeNotification,
  135.                                                object: clipView)*/
  136.         NotificationCenter.default.addObserver(self,
  137.                                                selector: #selector(startscroll),
  138.                                                name: NSScrollView.willStartLiveScrollNotification,
  139.                                                object: scrollView)
  140.  
  141.         NotificationCenter.default.addObserver(self,
  142.                                                selector: #selector(endscroll),
  143.                                                name: NSScrollView.didEndLiveScrollNotification,
  144.                                                object: scrollView)
  145.  
  146.                                                                          
  147.         // Subview1
  148.         let view1 = NSView(frame: NSRect(x: 0, y: 0, width: 1200, height: 60))
  149.         view1.translatesAutoresizingMaskIntoConstraints = false
  150.         view1.wantsLayer = true
  151.         view1.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
  152.         view1.layer?.borderWidth = 1
  153.         view1.layer?.borderColor = NSColor.gray.cgColor
  154.         documentView.addSubview(view1)
  155.        
  156.         let view2 = NSView(frame: NSRect(x: 0, y: 60, width: 1200, height: 60))
  157.         view2.translatesAutoresizingMaskIntoConstraints = false
  158.         view2.wantsLayer = true
  159.         view2.layer?.backgroundColor = NSColor.green.cgColor
  160.         view2.layer?.borderWidth = 1
  161.         view2.layer?.borderColor = NSColor.gray.cgColor
  162.         documentView.addSubview(view2)
  163.        
  164.         let view3 = NSView(frame: NSRect(x: 0, y: 120, width: 1200, height: 60))
  165.         view3.translatesAutoresizingMaskIntoConstraints = false
  166.         view3.wantsLayer = true
  167.         view3.layer?.backgroundColor = NSColor.green.cgColor
  168.         view3.layer?.borderWidth = 1
  169.         view3.layer?.borderColor = NSColor.gray.cgColor
  170.         documentView.addSubview(view3)
  171.        
  172.         let view4 = NSView(frame: NSRect(x: 0, y: 180, width: 1200, height: 60))
  173.         view4.translatesAutoresizingMaskIntoConstraints = false
  174.         view4.wantsLayer = true
  175.         view4.layer?.backgroundColor = NSColor.green.cgColor
  176.         view4.layer?.borderWidth = 1
  177.         view4.layer?.borderColor = NSColor.gray.cgColor
  178.         documentView.addSubview(view4)
  179.        
  180.         let view5 = NSView(frame: NSRect(x: 0, y: 240, width: 1200, height: 60))
  181.         view5.translatesAutoresizingMaskIntoConstraints = false
  182.         view5.wantsLayer = true
  183.         view5.layer?.backgroundColor = NSColor.green.cgColor
  184.         view5.layer?.borderWidth = 1
  185.         view5.layer?.borderColor = NSColor.gray.cgColor
  186.         documentView.addSubview(view5)
  187.  
  188.     }
  189.     override func loadView() {
  190.     self.view = NSView(frame: NSRect(x: 0, y: 0, width: NSScreen.main?.frame.width ?? 100, height: NSScreen.main?.frame.height ?? 100))
  191.     }
  192.     override var representedObject: Any? {
  193.         didSet {
  194.         // Update the view, if already loaded.
  195.         }
  196.     }
  197.  
  198.  
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment