Currently I can change the background color of my window like so:
tab_view.layer?.backgroundColor = NSColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1).CGColor
Which is great and all, but I want the user to be able to change the color.
This is when I found out about NSColorWell.
I tried applying the color like so:
tab_view.layer?.backgroundColor = NSColorWell
but then it says "Can't assign value of type 'NSColorWell.Type' to type 'CGColor'?"
I tried looking it up, but everything I found (which was barely any) was written in Objective - C
Someone please explain to me how I can make the view background color a color from the NSColorWell.
You need to pass NSColorWell .color .CGColor property value. Apple docs
Related
In the Java source code of my class, I saw that color is integrated as a method call with the input color as a word. So it seems that you can't implement any code, because color is defined with shape.setFillColor().
Is there a way around this? Otherwise a more complicated coloring would not be possible.
I have attached an example here in this screenshot how I could imagine it, however I get some errors because semicolons are missing and I have not declared the variables. So my problem is that i don't know how and if i can query if-else as well as embed switch case. So how can I write various functions for a color setting?
Can someone help me?
Example code for a color change of a rectangle
You could define a function that returns Color type, e.g. chooseColor(<your parameters>) and you could call shape.setFillColor(chooseColor(<your parameters>)). The body of the chooseColor() function can be based on the example code you posted.
Or you could call the chooseColor() function directly at the Fill color property of your shape if it is not too computation heavy, otherwise it could slow down the simulation.
Not quite sure what you need, tbh. You can make coloring as complex as you like. But once you decide on a color, you must call shape.setFillColor(theColorYouChose) to actually do it...
You can, however, create any possible color using Color myNewColor = new Color(x,y,z,a) where x, y and z are RGB values (0-255) and the last is the transparency you want (also 0-255)
To use this with switch/if statements, you can create a local Color variable and use it at the end for the shape:
Color myColor = new Color(0,0,0,0);
switch(someThing) {
case myCase:
myColor = yellowGreen;
break;
case myOtherCase:
if (someCondition) {
myColor = new Color(12, 222,45,122);
} else {
myColor = blue;
}
break;
}
shape.setFillColor(myColor);
I'm learning how to implement great CollectionViewPagingLayout templates in my project.
This one: https://github.com/amirdew/CollectionViewPagingLayout
First I specify which template to use (now I use "invertedCylinder") - and this part works well:
extension MovieCollectionViewCell: ScaleTransformView {
var scaleOptions: ScaleTransformViewOptions {
.layout(.invertedCylinder)
}
}
The problem appears when I try to modify the template. There is an extension, written by the creator of the Layout:
extension YourCell: ScaleTransformView {
var scaleOptions = ScaleTransformViewOptions(
minScale: 0.6,
scaleRatio: 0.4,
translationRatio: CGPoint(x: 0.66, y: 0.2),
maxTranslationRatio: CGPoint(x: 2, y: 0
)
}
I have tried to get rid of stored properties error and modify the code:
extension MovieCollectionViewCell: ScaleTransformView {
var scaleOptionsDetailed: ScaleTransformViewOptions {
minScale: 0.6,
scaleRatio: 0.4,
translationRatio: CGPoint(x: 0.66, y: 0.2),
maxTranslationRatio: CGPoint(x: 2, y: 0)
}
}
But this gives me more errors:
Redundant conformance of 'MovieCollectionViewCell' to protocol 'ScaleTransformView'
Cannot find 'minScale' in scope
Consecutive statements on a line must be separated by ';'
I understand that this is a question about basics. But it is already the second day im trying to solve the issue and would be very grateful for a guidance.
I don’t know why that repo includes stored properties on an extension. That is illegal and will likely always be illegal.
You could convert your variable scaleOptions to a computed property. To do that get rid of the equals sign. Then every time you reference that property it will run the code in the closure and generate a new value.
It is also possible to fake stored properties for extensions using associated values from the Objective-C runtime, but that is considered a hack and probably not a great idea.
In WebGL (OpenGL), write as follows
// alpha blending
gl.blendFuncSeparate (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE);
gl.bindFramebuffer (gl.FRAMEBUFFER, buffers [0] .framebuffer);
gl.useProgram (firstProgram);
webgl.enableAttribute (planeVBO, attLocation, attStride);
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, planeIBO);
gl.uniform2fv (resolution, [width, height]);
gl.drawElements (gl.TRIANGLES, plane.index.length, gl.UNSIGNED_SHORT, 0);
I am worried when I want to realize with Metal. In WebGL, if you read (bind) the created frame buffer and draw it with Shader, it will add it, but in the case of Metal
Is it more appropriate to send the framebuffer as a texture to Shader and add it on the Shader side rather than reading it? Or does it exist as a mechanism in the same way?
Or
renderToTextureRenderPassDescriptor = MTLRenderPassDescriptor()
renderToTextureRenderPassDescriptor.colorAttachments[0].texture = renderTargetTexture
renderToTextureRenderPassDescriptor.colorAttachments[0].loadAction = .clear
renderToTextureRenderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
renderToTextureRenderPassDescriptor.colorAttachments[0].storeAction = .store
Is it possible to continue adding in the same way as long as it is not cleared in such a place?
Or the earlier pipline
renderTargetTexture = mtlDevice.makeTexture(descriptor: textureDescriptor)
Since it has been initialized at that point, is there any need for change?
The code template itself is as follows.
https://sgaworks.com/metalsample/MetalSample2.zip
Swift 4 has a new NSRect.fill() function to replace NSRectFill(). When attempting to clear a bitmap using NSColor.clear.setFill(), the bitmap remains unchanged using NSRect.fill().
A workaround is to use NSRect.fill(using:) and specifying .copy.
Here is what the code does in Playgrounds in Swift 4:
Sample code:
// Duplicating a bug with Xcode 9 and Swift 4
import Cocoa
var image = NSImage(size: NSSize(width: 32, height: 32))
let rect = NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
image.lockFocus()
NSColor.red.setFill()
rect.fill()
image.unlockFocus()
image.lockFocus()
NSColor.clear.setFill()
// calling fill() with a clear color does not work
rect.fill()
image.unlockFocus()
// Image above should not be red
image.lockFocus()
NSColor.clear.setFill()
// using fill(using:) does work
rect.fill(using: .copy)
image.unlockFocus()
// image above is properly cleared
Is this a bug that I should file with Apple or am I missing something? This sequence worked in Swift 3 using NSRectFill().
Here is Swift 3 using NSRectFill:
They have simply different default logic:
Old syntax was always .copy.
New syntax is context if present or .sourceOver.
void NSRectFill(NSRect rect);
Fills aRect with the current color using the compositing mode NSCompositingOperationCopy
(source: https://developer.apple.com/documentation/appkit/1473652-nsrectfill)
/// Fills this rect in the current NSGraphicsContext in the context's fill
/// color.
/// The compositing operation of the fill defaults to the context's
/// compositing operation, not necessarily using `.copy` like `NSRectFill()`.
/// - precondition: There must be a set current NSGraphicsContext.
#available(swift 4)
public func fill(using operation: NSCompositingOperation = NSGraphicsContext.current?.compositingOperation ?? .sourceOver)
(source: Xcode comments for https://developer.apple.com/documentation/corefoundation/cgrect/2903486-fill/)
So if you're converting old code to newer format, then rect.fill(using: .copy) is the syntax to keep identical behaviour.
The following code runs once and it doesn't even animate..it just appears 20 pixels higher. Any idea why? What I want is for the button to move up and down forever.
scrollButton.frame = CGRectMake(CGRectGetMidX(self.view.frame)-20, (self.view.frame.size.height-64)*1-80, 40, 16)
scrollButton.setImage(UIImage(named: "ARROW.png"), forState: .Normal)
scrollButton.tintColor = UIColor.whiteColor()
self.scrollView.addSubview(scrollButton)
UIView.animateWithDuration(1.0, delay: 0, options: UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: {
self.scrollButton.frame.origin.y -= 20
}, completion: nil)
Although this question is quite old I wanted to add some light to new users.
UIView Animations doesn't work well when called from viewDidLoad try calling it instead from viewWillAppear.
Hope it helps.
I am not sure if that is going to work well , you can make two functions and each one has to move the body and then call the other one . You can use a boolean variable to stop the functions work.