Error message given: Generic parameter 'Data' could not be inferred - swiftui

I have tried many things, but seem to be getting continuous error messages which I cannot remove. The code is listed below, and I have the following questions:
Where is the error? The second ForEach statement has no errors, but the first results in "Generic parameter 'Data' could not be inferred".
Why can't I use a "for" statement within this block? When I do so, I get an error message
It's strange not to define i and j upfront, but when I do so I get error messages. Why?
I would like to make the various (8) circle segments which are drawn buttons. I have found references in older Swift versions (see commented lines below), but how should I create those in SwiftUI?
Any help to this beginner would be much appreciated.
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
let r = CGFloat(75.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var arc_width = CGFloat(25.0)
let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))
ZStack {
ForEach(1..<ContentView.circleCount){ j in
r = CGFloat(25.0 + CGFloat((j) - 1.0) * 25.0)
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.closeSubpath()
// let shapeLayer = CAShapeLayer()
// shapeLayer.strokeColor = UIColor.red.cgColor
// shapeLayer.fillColor = UIColor.blue.cgColor
// shapeLayer.path = path.cgPath
}
}
}
}
}
}

Your code won't compile, because you are adding lets and vars in body closure, where compiler expects some View.
You can simply move your vars inside of Path closure (or you can make some functions for computing it):
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
ZStack {
ForEach(1..<ContentView.circleCount){ j in
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
let r = CGFloat(25.0 + (CGFloat(j) - 1.0) * 25.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var arc_width = CGFloat(25.0)
let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.closeSubpath()
}
}
}
}
}
}

Related

I want to draw many shapes, but don't know how to use the class array

paint shape 1, but paint shape 2 , shape 1 is clear ..
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Ismousedown = true;
LocationXY = e.Location;
mousedownR = (int)(Math.Round(Math.Sqrt(Math.Pow(LocationXY.X - 250, 2) + Math.Pow(LocationXY.Y - 250, 2)), 2));
// label2.Text = mousedownstarAngle.ToString();
if (e.X >= 250)
{
gocanphal = (int)(270 - Math.Round(180 * -Math.Asin((e.Y - 250) / Math.Sqrt(Math.Pow(250 - e.X, 2) + Math.Pow(e.Y - 250, 2))) / Math.PI, 2));
}
if (e.X < 250)
{
gocanphal = (int)(90 + Math.Round(180 * (-Math.Asin((e.Y - 250) / Math.Sqrt(Math.Pow(250 - e.X, 2) + Math.Pow(e.Y - 250, 2)))) / Math.PI, 2));
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Ismousedown == true)
{
LocationX1Y1 = e.Location;
if (e.X >= 250)
{
gocmo = (int)(270 - Math.Round(180 * -Math.Asin((LocationX1Y1.Y - 250) / Math.Sqrt(Math.Pow(250 - LocationX1Y1.X, 2) + Math.Pow(LocationX1Y1.Y - 250, 2))) / Math.PI, 2));
}
if (e.X < 250)
{
gocmo = (int)(90 + Math.Round(180 * -Math.Asin((LocationX1Y1.Y - 250) / Math.Sqrt(Math.Pow(250 - LocationX1Y1.X, 2) + Math.Pow(LocationX1Y1.Y - 250, 2))) / Math.PI, 2));
}
Refresh();
mouseupR = (int)(Math.Round(Math.Sqrt(Math.Pow(LocationX1Y1.X - 250, 2) + Math.Pow(LocationX1Y1.Y - 250, 2)), 2));
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (Ismousedown == true)
{
LocationX1Y1 = e.Location;
Ismousedown = false;
mouseupR = (int)(Math.Round(Math.Sqrt(Math.Pow(e.X - 250, 2) + Math.Pow(e.Y - 250, 2)), 2));
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(penn, 250, 250, 250, 0);
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var center = new Point(250, 250);
var innerR = mouseupR;// xa or gan diem dau
//var thickness = 30;// do day
var startAngle1 = gocanphal + 90;// diem bat dau
var arcLength1 = gocmo - gocanphal;// diem end
var outerR = mousedownR;
// var innerR = mousedownR - mouseupR;
var outerRect = new Rectangle
(center.X - outerR, center.Y - outerR, 2 * outerR, 2 * outerR);//center.Y - outerR
var innerRect = new Rectangle
(center.X - innerR, center.Y - innerR, 2 * innerR, 2 * innerR); // center.X - innerR
// using (var p = new GraphicsPath())
e.Graphics.FillEllipse(Brushes.White, 150, 150, 5, 5);
GraphicsPath p = new GraphicsPath();
p.AddArc(outerRect, startAngle1, arcLength1);
p.AddArc(innerRect, startAngle1 + arcLength1, -arcLength1);
p.CloseFigure();
g.DrawPath(Pens.Blue, p);
}
}
}
Want to draw more pictures and save

emberjs -ember-cli-chart extend doughnut chart to create rounded edge

how do i extend chart object in ember-cli-cart ?? i want to extend dooughnut to have smooth edges and some other features too. something like the below code ,but i want to create as a reusable ember component
Chart.types.doughnut.extend({
draw: function (ease) {
var ctx = this.chart.chart.ctx;
var easingDecimal = ease || 1;
Chart.helpers.each(this.getDataset().metaData, function (arc, index) {
arc.transition(easingDecimal).draw();
var vm = arc._view;
var radius = (vm.outerRadius + vm.innerRadius) / 2;
var thickness = (vm.outerRadius - vm.innerRadius) / 2;
var angle = Math.PI - vm.endAngle - Math.PI / 2;
ctx.save();
ctx.fillStyle = vm.backgroundColor;
ctx.translate(vm.x, vm.y);
ctx.beginPath();
ctx.arc(radius * Math.sin(angle), radius * Math.cos(angle), thickness, 0, 2 * Math.PI);
ctx.arc(radius * Math.sin(Math.PI), radius * Math.cos(Math.PI), thickness, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.restore();
});
},
});

Swift3 - Spritekit Sprite not full size

I want to have a game over screen pop up, using SKSpriteNodes and it scales correctly on first try but from the second, the whole game over is small.
func spawnPlayAgainBTN() {
playAgain = SKSpriteNode(imageNamed: "buttonPlay")
playAgain.position = CGPoint(x: self.frame.midX + 54, y: self.frame.midY - 10)
playAgain.zPosition = 8
playAgain.setScale(0)
self.addChild(playAgain)
playAgain.run(SKAction.scale(to: 0.4, duration: 0.4))
}
func spawnMainMenuBTN() {
mainMenu = SKSpriteNode(imageNamed: "buttonMainMenu ")
mainMenu.position = CGPoint(x: self.frame.midX - 54, y: self.frame.midY - 10)
mainMenu.zPosition = 8
mainMenu.setScale(0)
self.addChild(mainMenu)
mainMenu.run(SKAction.scale(to: 1.0, duration: 0.4))
}
func spawnGameOverScreen() {
gameOverBG = SKSpriteNode(imageNamed: "gameOverBG")
gameOverBG.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
gameOverBG.zPosition = 7
gameOverBG.setScale(0)
self.addChild(gameOverBG)
gameOverBG.run(SKAction.scale(to: 1.0, duration: 0.4))
spawnPlayAgainBTN()
spawnMainMenuBTN()
isRunningGame = false
isShowingGameOver = true
}

CAShapeLayer dashed line clipping at end of view

Okay so I'm trying to draw a dashed line inside of a view, which should be fairly simple but no matter what I've tried I keep getting the dashes at both ends clipping and I would like to have the dash not have any clipped segments. Here's the extension I made (the purple background color is just there so I can see the frame)
extension UIView {
public func addDashedLineToView(_ color : UIColor) {
self.backgroundColor = UIColor.purple
let cgColor = color.cgColor
let shapeLayer: CAShapeLayer = CAShapeLayer()
let frameSize = self.frame.size
let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)
shapeLayer.bounds = shapeRect
shapeLayer.position = CGPoint(x: frameSize.width / 2, y: frameSize.height)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = cgColor
shapeLayer.lineWidth = 6
shapeLayer.lineCap = kCALineCapRound
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineDashPattern = [8, 10]
let path: CGMutablePath = CGMutablePath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
shapeLayer.path = path
self.layer.addSublayer(shapeLayer)
self.layer.masksToBounds = true
}
and here's a screenshot of what it ends up looking like.
not sure where I'm messing up here, but I know that I must be doing something wrong since it shouldn't be this hard to draw a dashed line in swift (I hope).
You can adjust the start position of the pattern with lineDashPhase. Try
shapeLayer.lineDashPhase = -4

Why is friction not applied when two nodes come into contact? Swift

This is the code I have for the two nodes. Declarations not included.
//paddle node and its physicsbody
paddle = childNode(withName: "paddle") as! SKSpriteNode
paddle.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: paddle.size.width,
height: paddle.size.height))
paddle.physicsBody?.isDynamic = false
paddle.physicsBody?.affectedByGravity = false;
paddle.physicsBody?.friction = 100
paddle.physicsBody?.restitution = 0
//paddle.physicsBody?.linearDamping = 1
//paddle.physicsBody?.angularDamping = 1
//Second node
rect.position = CGPoint(x: 100, y:500)
rect.fillColor = UIColor(hue: 215/360, saturation: 49/100, brightness: 54/100, alpha: 1.0)
rect.strokeColor = UIColor(hue: 215/360, saturation: 49/100, brightness: 54/100, alpha: 1.0)
rect.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: rect.frame.width, height: rect.frame.height))
rect.physicsBody?.friction = 0.4
rect.physicsBody?.restitution = 0
rect.physicsBody?.mass = 100
rect.physicsBody?.allowsRotation = true
rect.physicsBody?.isDynamic = true
rect.physicsBody?.linearDamping = 0
rect.physicsBody?.angularDamping = 0
//rect.physicsBody?.velocity = CGVector(dx: 0, dy: -700)
rect.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
rect.lineWidth = 10
addChild(rect)
I am not sure if I am asking the right question but I have the rectangular node falling down unto a paddle that can be moved side to side by touch. Once the rect node is on top of the paddle I want it to stay on it while the I move the paddle but it stays pinned and does not move although everything else (gravity, rotation, etc.) works.
Here are the pictures to clarify
https://i.stack.imgur.com/LIqYT.png
https://i.stack.imgur.com/ap6Yy.png