Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Test3
- //
- // Created by Daniel Zollitsch on 27.09.21.
- //
- import Cocoa
- @objc class buttontest : NSObject{
- let button = NSButton(frame: CGRect(x: 100, y: 100, width: 150, height: 50))
- @objc func printSomething() {
- print("Hello")
- // print(NSApp.currentEvent)
- if(NSApp.currentEvent?.type==NSEvent.EventType.leftMouseDragged){
- self.button.frame.origin.x=CGFloat(NSApp.currentEvent?.locationInWindow.x ?? 0)-75.0
- self.button.frame.origin.y=CGFloat(NSApp.currentEvent?.locationInWindow.y ?? 0)-25.0
- }
- self.button.title="Test14"
- }
- func buttoninit(x: Int, y: Int) -> NSButton{
- self.button.title="Test"
- self.button.frame.origin.x=CGFloat(x)
- self.button.frame.origin.y=CGFloat(y)
- self.button.bezelStyle=NSButton.BezelStyle.rounded
- self.button.target=self;
- //button.action = Selector(ViewController.printSomething)
- self.button.identifier=NSUserInterfaceItemIdentifier.init(rawValue: "TEST");
- self.button.sendAction(on: [NSEvent.EventTypeMask.leftMouseUp,NSEvent.EventTypeMask.leftMouseDragged]);
- self.button.action = #selector(self.printSomething)
- return self.button
- }
- }
- class ViewController: NSViewController {
- var animated = false;
- let scrollView = NSScrollView(frame: NSRect(x: 300, y: 300, width: 600, height: 200))
- @objc func startscroll() {
- print("Scroll Started")
- print(NSApp.currentEvent)
- var val = scrollView.contentView.frame.height
- var val2 = scrollView.documentView?.frame.height ?? 0;
- if(scrollView.contentView.bounds.origin.y>0 && scrollView.contentView.bounds.origin.y<=(val2-val)-1 ){
- //scrollView.contentView.animator().setBoundsOrigin(NSMakePoint(0, scrollView.contentView.bounds.origin.y))
- scrollView.contentView.layer?.removeAllAnimations()
- }
- //
- }
- @objc func endscroll() {
- print("end")
- print(NSApp.currentEvent ?? 0)
- var val = scrollView.contentView.frame.height
- var val2 = scrollView.documentView?.frame.height ?? 0;
- if(scrollView.contentView.bounds.origin.y>0 && scrollView.contentView.bounds.origin.y<=(val2-val)-1 ){
- var offset = scrollView.contentView.bounds.origin.y
- offset = round(offset / 60) * 60;
- NSAnimationContext.runAnimationGroup({ context in
- context.duration = 2
- crollView.contentView.animator().setBoundsOrigin(NSMakePoint(0, offset))
- }) {
- }}
- }
- var buttonvar: [buttontest] = []
- private lazy var redBox = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
- override func viewDidLoad() {
- super.viewDidLoad()
- view.addSubview(redBox)
- redBox.wantsLayer = true
- redBox.layer?.backgroundColor = NSColor.red.cgColor
- //button.init("Test12",self,Selector(printSomething())
- buttonvar.append(buttontest());
- buttonvar.append(buttontest());
- self.view.addSubview(buttonvar[0].buttoninit(x:100,y:100))
- self.view.addSubview(buttonvar[1].buttoninit(x:300,y:300))
- let slider = NSSlider(frame: CGRect(x: 10, y: 10, width: 100, height: 200))
- slider.minValue=0;
- slider.maxValue=255;
- slider.sliderType=NSSlider.SliderType.linear;
- slider.action=#selector(printslider)
- slider.target=self;
- slider.floatValue=50.5
- self.view.addSubview(slider)
- //self.view.addSubview(buttontest().buttoninit())
- // Do any additional setup after loading the view.
- scrollView.translatesAutoresizingMaskIntoConstraints = false
- scrollView.borderType = .bezelBorder
- scrollView.backgroundColor = NSColor.gray
- scrollView.hasVerticalScroller = true
- scrollView.hasHorizontalScroller = true
- scrollView.frame.origin.x=300;
- scrollView.frame.origin.y=300;
- //scrollView.allowsMagnification = true
- self.view.addSubview(scrollView)
- let clipView = NSClipView()
- scrollView.contentView = clipView
- clipView.backgroundColor = NSColor.blue
- // Initial document view
- let documentView = NSView(frame: NSRect(x: 0, y: 0, width: 1200, height: 300))
- documentView.wantsLayer = true
- scrollView.documentView = documentView
- documentView.layer?.backgroundColor = NSColor.red.cgColor
- documentView.layer?.borderWidth = 0
- documentView.layer?.borderColor = NSColor.darkGray.cgColor
- clipView.scaleUnitSquare(to: NSSize(width: 1, height: 1))
- clipView.postsBoundsChangedNotifications = true
- /*NotificationCenter.default.addObserver(self,
- selector: #selector(collectionViewDidScroll(notification:)),
- name: NSView.boundsDidChangeNotification,
- object: clipView)*/
- /*NotificationCenter.default.addObserver(self,
- selector: #selector(printslider),
- name: NSView.boundsDidChangeNotification,
- object: clipView)*/
- NotificationCenter.default.addObserver(self,
- selector: #selector(startscroll),
- name: NSScrollView.willStartLiveScrollNotification,
- object: scrollView)
- NotificationCenter.default.addObserver(self,
- selector: #selector(endscroll),
- name: NSScrollView.didEndLiveScrollNotification,
- object: scrollView)
- // Subview1
- let view1 = NSView(frame: NSRect(x: 0, y: 0, width: 1200, height: 60))
- view1.translatesAutoresizingMaskIntoConstraints = false
- view1.wantsLayer = true
- view1.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
- view1.layer?.borderWidth = 1
- view1.layer?.borderColor = NSColor.gray.cgColor
- documentView.addSubview(view1)
- let view2 = NSView(frame: NSRect(x: 0, y: 60, width: 1200, height: 60))
- view2.translatesAutoresizingMaskIntoConstraints = false
- view2.wantsLayer = true
- view2.layer?.backgroundColor = NSColor.green.cgColor
- view2.layer?.borderWidth = 1
- view2.layer?.borderColor = NSColor.gray.cgColor
- documentView.addSubview(view2)
- let view3 = NSView(frame: NSRect(x: 0, y: 120, width: 1200, height: 60))
- view3.translatesAutoresizingMaskIntoConstraints = false
- view3.wantsLayer = true
- view3.layer?.backgroundColor = NSColor.green.cgColor
- view3.layer?.borderWidth = 1
- view3.layer?.borderColor = NSColor.gray.cgColor
- documentView.addSubview(view3)
- let view4 = NSView(frame: NSRect(x: 0, y: 180, width: 1200, height: 60))
- view4.translatesAutoresizingMaskIntoConstraints = false
- view4.wantsLayer = true
- view4.layer?.backgroundColor = NSColor.green.cgColor
- view4.layer?.borderWidth = 1
- view4.layer?.borderColor = NSColor.gray.cgColor
- documentView.addSubview(view4)
- let view5 = NSView(frame: NSRect(x: 0, y: 240, width: 1200, height: 60))
- view5.translatesAutoresizingMaskIntoConstraints = false
- view5.wantsLayer = true
- view5.layer?.backgroundColor = NSColor.green.cgColor
- view5.layer?.borderWidth = 1
- view5.layer?.borderColor = NSColor.gray.cgColor
- documentView.addSubview(view5)
- }
- override func loadView() {
- self.view = NSView(frame: NSRect(x: 0, y: 0, width: NSScreen.main?.frame.width ?? 100, height: NSScreen.main?.frame.height ?? 100))
- }
- override var representedObject: Any? {
- didSet {
- // Update the view, if already loaded.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment