RE: CALayer on OSX 10.10
Greetings,
I've created a small video playing app I use for logging footage. I designed my own video progress bar with play buttons etc. The playback line I created using a CALayer so I have excellent control over the appearance of the items in the playback control and progress status.
My issue is that when the user uses a mouse to click in the progess area to move around the video or the user drags the mouse (scrubbing the video) the playback line animates from one spot to another. While the user drags, the layer kind of fades in and out and then moves past the mouse release point.
I think perhaps the CALayer is animating from each mouse point to the next and there are so many of them it's messing something up.
Two questions:
Is there a way to make the layer not animate between each move?
Should I be using Core Graphics for this? Concern here is the graphic changes 24 - 30 times a second.
Any suggestions?
Thanks.
-Cablet
class VideoTimelineProgressView : NSView {
var aLay : CALayer = CALayer.init()
var tickLayer : CALayer = CALayer.init()
var changeTest : Bool = true
var forward : Bool = false
var maxValue : Float = 100
//point to player from mainViewController
var player : AVPlayer?
required init?(coder: NSCoder) {
super.init(coder: coder)
Swift.print("in init?: VideTimelineProgressView")
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
Swift.print("in init: VideTimelineProgressView")
self.wantsLayer = true
self.layer!.backgroundColor = CGColorCreateGenericRGB(0.5, 0.5, 0.5, 0.5)
self.aLay.frame = self.frame
self.aLay.backgroundColor = CGColorCreateGenericRGB(0.0, 0.2, 1.0, 0.5)
self.layer!.addSublayer(aLay)
tickLayer.frame = CGRect(x: 0, y: 0, width: 2, height: self.aLay.frame.height)
tickLayer.backgroundColor = CGColorCreateGenericRGB(0.0, 1.0, 0.0, 1.0)
self.layer!.addSublayer(tickLayer)
}
//move the tick mark to seconds
func updateTick(seconds: Double)
{
let n = CGFloat(self.layer!.frame.width) / CGFloat(maxValue)
tickLayer.frame = CGRect(x: n*CGFloat(seconds), y: 0, width: 2, height: self.aLay.frame.height)
if ( tickLayer.frame.width >= self.layer!.frame.width)
{
tickLayer.frame = CGRect(x: self.layer!.frame.width-4, y: 0, width: 2, height: self.aLay.frame.height)
}
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// test drawing a line
}
override func mouseDragged(theEvent: NSEvent) {
let newPoint : NSPoint = self.convertPoint(theEvent.locationInWindow, fromView: nil)
//Swift.print("mouseDragged: theEvent mouse x = \(theEvent.locationInWindow)")
//Swift.print("mouseDragged newPoint = \(newPoint)")
tickLayer.frame = CGRect(x: newPoint.x, y: 0, width: 2, height: self.aLay.frame.height)
self.player!.seekToTime(CMTimeMakeWithSeconds(Float64((maxValue*Float(newPoint.x))/Float(self.layer!.frame.width)), Int32(1)))
}
override func mouseDown(theEvent: NSEvent) {
let newPoint : NSPoint = self.convertPoint(theEvent.locationInWindow, fromView: superview)
tickLayer.frame = CGRect(x: newPoint.x, y: 0, width: 2, height: self.aLay.frame.height)
self.player!.seekToTime(CMTimeMakeWithSeconds(Float64((maxValue*Float(newPoint.x))/Float(self.layer!.frame.width)), Int32(1)))
}
func player(player: AVPlayer)
{
self.player = player
Swift.print("videoTimelineProgresView player = \(self.player)")
}
}edit: added code