How to change struct properties in Swift - swift-playground

I’m trying out Swift playgrounds and I cannot find a way to change value of a struct. Below I want to change properties of Shadow from default values.
I’ve tried the initialiser and dot syntax but I get ‘field is inaccessible due to internal protection level.
let circle = Circle()
circle.draggable = true
//var shadow = Shadow(color: #colorLiteral(red: 0.9529411764705882, green: 0.6862745098039216, blue: 0.13333333333333333, alpha: 1.0), offset: Point(3,-3), blurRadius: 5, opacity: 1)
var shadow = Shadow()
shadow.color = .red
circle.dropShadow = shadow

You’re apparently using the “Shapes” playground.
So, view the source by clicking on “...” » “Advanced” » “View Auxiliary Source Files” » “Contents” » “Modules” » “Book.playgroundmodule” » “Sources” » “PlaygroundAPI”.
If you look at the Shadow struct, none of those properties are declared as public. That means that you don’t have access to them outside of that module.
In contrast, if you look at the Circle declaration in contrast, radius is public. And if you look at AbstractDrawable, dropShadow is public, too.
In the absence of an explicit access qualifier (e.g., public), a property gets the internal access qualifier, only accessible within that module. (See The Swift Programming Language: Access Control.) And your code in that playground is not within the same module as where Shadow was defined. Thus you don’t have access to it.
So, bottom line, your warning is just telling you that you cannot access this internal property of the Shadow struct.
This begs the question as to why they declared Shadow such that you can’t customize the nature of the shadow. I suspect it is just an oversight on their part. For example, I opened up this playground workbook in Xcode and replaced the init method for Shadow with the following:
public init(offset: Point = Point(x: 1, y: -1), blurRadius: Double = 1, opacity: Double = 0.3, color: Color = .black) {
self.offset = offset
self.blurRadius = blurRadius
self.opacity = opacity
self.color = color
}
Then I could reopen this playground on my iPad and do things like:
let circle = Circle(radius: 30)
circle.dropShadow = Shadow(opacity: 0.9, color: .green)
And that yielded:

Related

Difference between Color.colorName and Color(.colorName)

The blue color produced by Color.blue is different from the one produced by Color(.blue). Is this a bug in Xcode preview, or do they really present different values? This doesn't apply to blue only but to several colors such as "red".
Yes, the two are different. By command-clicking on the tokens and using Xcode's "Jump to definition" feature, you can see the headers where they are defined.
For Color.blue:
extension Color {
// ...
/// A context-dependent blue color suitable for use in UI elements.
public static let blue: Color
For Color(.blue):
open class UIColor {
// ...
open class var blue: UIColor { get } // 0.0, 0.0, 1.0 RGB
Note that Color.blue says "context-dependent" -- this will change with various system environments, such as light or dark mode.
Also relevant: https://developer.apple.com/documentation/uikit/uicolor/standard_colors
Also see note in the comments about a deprecation warning.

Can I set different type of loading animation in echarts

Currently I am calling an API to populate data in the bar chart made using echarts, I used the following code.
var mychart = echarts.init(document.getElementById("test"));
mychart.showLoading();
Once the data comes then I hide the loading, but in echarts I am getting only the circular shape loading option, can I use vertical bars for showing loading animation in echarts.
Echarts has no another loading spinner but you can make own or take any spinner lib (for example) and place above. Of course you can use the custom series or graphic and draw something but is not quite right spent time because it's loader and its role just to show that chart is not dead and will continue action soon to prevent user leaving.
Try the DOC
https://echarts.apache.org/en/api.html#echartsInstance.showLoading
default: {
text: 'loading',color: '#c23531',
textColor: '#000',
maskColor: 'rgba(255, 255, 255, 0.8)',
zlevel: 0,
// Font size. Available since `v4.8.0`.
fontSize: 12,
// Show an animated "spinner" or not. Available since v4.8.0`.
showSpinner: true,
// Radius of the "spinner". Available since `v4.8.0`.
spinnerRadius: 10,
// Line width of the "spinner". Available since `v4.8.0`.
lineWidth: 5
}

How to create a color picker wheel slider?

Does anyone know how to create the following? I cannot find any tutorials.
I can get the effect without a slider, but it is not as smooth. Or I can incorporate the slider but not update the picker with the value as it expects CGPoints instead of Ints and expects it in the gesture.locationInView(colorWheel) format, which a standard CGPoint will not accept.
You can use Cocoa controller for this. There are so much of controllers which is similar to this interface.
https://www.cocoacontrols.com/controls/hsv-color-picker
hope this link will help you.
I was able to just use the color wheel as a background and used the slider value to change the hue
func changeSet(changeValue: CGFloat) {
var value = (360 - currentAngleGlobal) / 360 // I had to deduct from 360 as my values were inverted
var color = UIColor(hue: CGFloat(value), saturation: 1, brightness: 1, alpha: 1)
self.colorOutput.backgroundColor = color
}

SWIFT OSX NSBezierPath Linewidth property not working

I am using the following code to draw a line graph on a custom NSView
for var index = 0; index < (dataPointsArray.count - 1); index++ {
NSBezierPath().lineWidth = 20.0
NSBezierPath.strokeLineFromPoint(dataPointsArray[index], toPoint: dataPointsArray[index + 1])
}
This snippet is contained within a function that is called by drawRect() within the Custom View.
The line draws correctly within the coordinate system of the view. However, the line is drawn at the same width (one pixel width) regardless of the .lineWidth setting (e.g., 5.0, 10.0, 20.0 etc) which seems to have no impact on the line that is actually drawn).
Is anyone able to advise what might be creating this issue for me. I haven't been able to find a previous question that raises this issue.
NSBezierPath().lineWidth = 20.0
The () means you are initializing a new instance of the class and set its lineWidth to 20.0. You should create a variable and use it to draw your path:
var bezierPath = NSBezierPath()
bezierPath.lineWidth = 20.0
bezierPath.moveToPoint(dataPointsArray[index])
bezierPath.lineToPoint(dataPointsArray[index + 1])

Accelerated canvas context translation: bug, or my mistake?

I found an odd behavior while working on my pet game. I wanted to draw few objects on canvas, some of them required image / icon to be rotated. It is quite common use case, usual solution is to make use of context's rotate method. Going with the blow I used also translate to nicely and consistently put images in desired place.
This worked fine, until I tried Chrome on Windows laptop, with hardware acceleration enabled. Images started to blink and teleport across the screen. I found that it is related to acceleration (turning off accelerated graphics fixes problem) and my best guess is that when updating frame the renderer assumes that drawing calls are independent and can be executed in parallel. When context transforms take place it is not the case because context state changes.
Example of undesired behavior: having a canvas element with ID 'screen' try the following:
var canvas = document.getElementById("screen"),
ctx = canvas.getContext("2d");
var drawrect = function () {
ctx.fillStyle = this.color;
ctx.translate(this.x+10, this.y+10);
ctx.rotate(this.rotation);
ctx.fillRect(-10, -10, 20, 20);
ctx.rotate(-this.rotation);
ctx.translate(-this.x-10, -this.y-10);
};
var red = {
x: 22,
y: 22,
rotation: 0,
color: "#ff0000",
draw: drawrect
};
var blu = {
x: 22,
y: 111,
rotation: 0,
color: "#0000ff",
draw: drawrect
};
function main_loop() {
ctx.clearRect(0, 0, 450, 450);
frameId = requestAnimationFrame(main_loop);
red.draw();
red.x += 1;
red.rotation +=0.1;
blu.draw();
blu.x += 1;
blu.rotation -=0.1;
}
main_loop();
Working example: http://jsfiddle.net/1u2d7uhr/7/ (tested on Chrome, Chromium, Firefox; accelerated Chrome glitches, others do not)
I was able to 'fix' this by removing translations and rendering rotating elements to separate canvas, which is then (after rotations) drawn onto the main one. This seems hackish to me though.
Is it code error on my part?
In this case what is the right way to render rotations (perhaps with this question I should go do codereview, but I'm not sure if this is the case)?
Or is it buggy behavior on browser side? I understand the logic behind it but it can be very much surprising (and cause some confusion) to developers. Or am I only one...

Resources