Hello,
I created a subclass of CALayer. But the Software isn't behaving as I expect it, maybe someone can explain to me what I am misunderstanding.
The subclass inherits a function changecolor() which gets called just for test 1s after start within a timer callback. The function currently just sets the background color of the layer to a different value.
If I do this, I get the following error:
Test3/ViewController.swift:62: Fatal error: Use of unimplemented initializer 'init(layer:)' for class 'Test3.timeRuler'
I have to implement
override init(layer: Any) {
super.init(layer: layer)
}
to get it working. Then the Layers color is updated as expected. BUT WHAT I EXPECTED:
I expected that a change in background color does not need a reinitialization of the layer, I expected that the change first does nothing, and the layer gets redrawn if I set the layers setNeedsDisplay().
The draw and display functions are never called somehow. They are however called if I call setNeedsDisplay() but the color changes anyway if override init(layer: Any) is defined.
What am I missing here?
class timeRuler :CALayer{
override init() {
super.init();
frame = NSRect(x: 0, y: 0, width: 300, height: 300);
backgroundColor=NSColor.blue.cgColor;
}
func changecolor(){
backgroundColor=NSColor.purple.cgColor;
//setNeedsDisplay()
}
/* override init(layer: Any) {
super.init(layer: layer)
}*/
override func draw(in ctx: CGContext) {
super.draw(in: ctx);
print("layer draw update");
}
override func display() {
//backgroundColor=NSColor.blue.cgColor;
super.display();
print("layer update");
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Post
Replies
Boosts
Views
Activity
Hello, I am new to macOS development and Swift in general and am trying to animate a NSScrollview to create a pagination like effect.
NSAnimationContext.runAnimationGroup({ context in
context.duration = 2
scrollView.contentView.animator().setBoundsOrigin(NSMakePoint(0, offset))
}) {
}}
If the user now scrolls while the animation is running, the scrollview jumps around.
I want to stop the animation at the current position so there is a callback to NSScrollView.willStartLiveScrollNotification
@objc func scrollstarted() {
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()
}
I tried now to either set a new destination bound or to remove all animations. But both do not change the "jumping" behavior and the animation still continues to its end.
How do I stop a running animation?