是否可以通过滚动OSX触控板(Swift)来改变价值?

时间:2015-08-27 18:32:07

标签: macos swift

是否可以根据触控板滚动/魔术鼠标的方向更改Cocoa中的值' wheel'滚动等。

例如

var value = 0

func startsScrolling() {
    if direction == up {
        value += 5
    } else if direction == down {
        value -= 5
    }
}

这将是连续的,因此当您在触控板上向上滚动方向向下时,每次手指向上移动需要5次关闭,反之亦然。

感谢并抱歉这个错误的伪代码。

2 个答案:

答案 0 :(得分:0)

您可以创建自定义窗口并覆盖scrollWheel NSEvent。您可以检查scrollingDeltaX和scrollingDeltaY属性。

class CustomWindow: NSWindow {
    override func scrollWheel(theEvent: NSEvent) {
        println( "Mouse Scroll scrollingDeltaX = \(theEvent.scrollingDeltaX)" )
        println( "Mouse Scroll scrollingDeltaY = \(Int(theEvent.scrollingDeltaY))" )

    }
}

如果您使用的是故事板,则必须覆盖viewController的scrollWheel NSEvent:

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var strDeltaX: NSTextField!
    @IBOutlet weak var strDeltaY: NSTextField!
    @IBOutlet weak var strScrollingDeltaX: NSTextField!
    @IBOutlet weak var strScrollingDeltaY: NSTextField!
    @IBOutlet weak var strValueX: NSTextField!
    @IBOutlet weak var strValueY: NSTextField!

    var xValue:CGFloat = 0
    var yValue:CGFloat = 0


    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override var representedObject: AnyObject? {
        didSet {
        }
    }
    override func scrollWheel(theEvent: NSEvent) {
        strDeltaX.stringValue = "deltaX: \(theEvent.deltaX)"
        strDeltaY.stringValue = "deltaY: \(theEvent.deltaY)"
        strScrollingDeltaX.stringValue = "scrollingDeltaX: \(theEvent.scrollingDeltaX)"
        strScrollingDeltaY.stringValue = "scrollingDeltaY: \(theEvent.scrollingDeltaY)"

        xValue += theEvent.scrollingDeltaX
        yValue += theEvent.scrollingDeltaY
        strValueX.stringValue = "xValue: \(xValue)"
        strValueY.stringValue = "yValue: \(yValue)"
    }
}

答案 1 :(得分:-2)

首先,您需要向ViewController添加滚动视图并将其链接。如果您不希望它显示,您可以使其透明。如果ViewController上有按钮,则可以将其发送回去。我转换了代码from objective-c。给你更大的力量:))

    var pointNow: CGPoint?
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
     pointNow = scrollView.contentOffset
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y < pointNow!.y {
        println("down")
    }
    else {
        if scrollView.contentOffset.y > pointNow!.y {
            println("up")
        }
    }
}