Swift中的定时器可可

时间:2014-12-03 23:50:02

标签: multithreading cocoa swift timer uilabel

我正在使用midi控制器在Swift中设置存储属性的值。当我的midi设备上的旋钮被转动时,我想要一个UILabel来显示当前值。值停止更改几秒后,我希望标签再次显示参数名称。任何想法我怎么可能这个???

此时,我可以显示传入的midi值,但不知道在何处或如何返回参数名称

class Knob_1_1 : UIControl, MidiWidgetWrapper {

var value: CGFloat! = 0.5 {
    didSet {
        if(value < minimumValue) {
            value = minimumValue
        } else if(value > maximumValue) {
            value = maximumValue
        }
        if(self.float) {
            valueLabel.text = String(format: "%.2f", Float(value))
        } else {
            valueLabel.text = String(format: "%d", Int(value))
        }
        angle = ((value - minimumValue) / (maximumValue - minimumValue)) * 270.0;
        fakeAngle = angle
        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
        self.setNeedsDisplay()
    }
}

// VIEW
let knobView: UIImageView = UIImageView(image: UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("knob", ofType: "png")!))
var angle:CGFloat = 90.0
var fakeAngle:CGFloat = 90.0
let valueLabel: UILabel = UILabel()

// TRACKING
var lastTouchPoint: CGFloat = 0.0
var longPressGesture: UILongPressGestureRecognizer?

// KNOB SETTINGS
var minimumValue: CGFloat! = 0.0
var maximumValue: CGFloat! = 1.0
var float: Bool = true
var midiCCNumber: Int! = 0

override init() {
    super.init()
    self.name = "knob"
    self.backgroundColor = UIColor.clearColor()

    self.knobView.frame = self.bounds

    self.addSubview(self.knobView)

    self.valueLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.valueLabel.text = self.name
    self.valueLabel.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.0)
    self.valueLabel.textColor = UIColor.grayColor()
    self.valueLabel.font = UIFont(name: "LucidaSansUnicode", size: 13.0)
    self.valueLabel.textAlignment = NSTextAlignment.Center
    self.addSubview(self.valueLabel)
    let views = ["valueLabel":valueLabel]
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[valueLabel(20)]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.valueLabel, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))

    self.longPressGesture = UILongPressGestureRecognizer(target: self, action: "presentSettings")
    self.addGestureRecognizer(self.longPressGesture!)
}

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func drawRect(rect: CGRect) {
    self.knobView.transform = CGAffineTransformMakeRotation(angle*CGFloat(M_PI/180.0) - CGFloat(135.0*M_PI/180.0))
    if (angle >= 360) {
        angle -= 360.0;
    }
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    var point: CGPoint = touches.anyObject()!.locationInView(self.superview)
    lastTouchPoint = point.y
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    var point: CGPoint = touches.anyObject()!.locationInView(self.superview)
    var distance:CGFloat = abs(point.y - lastTouchPoint)
    if(point.y < lastTouchPoint) {
        fakeAngle += fakeAngle < 270 ? 4 : 0
    } else {
        fakeAngle -= fakeAngle > 0 ? 4 : 0
    }
    if(self.float) {
        value = minimumValue + ((fakeAngle/270.0)*(maximumValue - minimumValue))
    } else {
        value = CGFloat(Int(minimumValue + ((fakeAngle/270.0)*(maximumValue - minimumValue))))
    }
    lastTouchPoint = point.y
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    self.valueLabel.text = self.name
}


// Called by the CoreMIDICallback every time
// a CC message is received on this knob's channel.
func setMIDIValue(midiValue: Int32) {
    var percent:CGFloat = CGFloat(midiValue) / 127.0
    dispatch_async(dispatch_get_main_queue(), {
        // DO SOMETHING ON THE MAINTHREAD
        self.value = ((self.maximumValue - self.minimumValue) * percent) + self.minimumValue
        self.setNeedsDisplay()
    })
}

}

0 个答案:

没有答案