I am trying to move a SKShapeNode along a UIBezierPath. Here is the code I have so far:
// Set up the circle track
let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2, screenHeight/2, screenWidth/3, screenWidth/3), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = SKColor.whiteColor()
self.addChild(shapeTrack)
// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.position = CGPointMake(screenWidth/2, screenHeight/2)
circle.fillColor = SKColor.whiteColor()
self.addChild(circle)
//Move the circle
circle.runAction(SKAction.repeatActionForever(SKAction.followPath(circleTrack.CGPath, speed: 3.0)))
All the action does is make the shape node disappear. How can I get it so that the circle moves along the bezier path forever?
You are placing the bezier path circleTrack in one position, and the shapeTrack in another position. The origin of circleTrack is at (screenWidth/2,screenHeight/2) and the shapeTrack is centered at (screenWidth/2,screenHeight/2). shapeTrack.position is the position of the center of the SKShapeNode. Try the following code.
let screenWidth = size.width
let screenHeight = size.height
let trackWidth = screenWidth/3
let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2 - trackWidth/2, screenHeight/2 - trackWidth/2, trackWidth, trackWidth), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = UIColor.whiteColor()
self.addChild(shapeTrack)
// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.fillColor = UIColor.whiteColor()
self.addChild(circle)
let followPath = SKAction.followPath(circleTrack.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
//Move the circle
circle.runAction(SKAction.repeatActionForever(followPath))
Related
I want to centre a rectangle in a view, i try using midX or midY but the view still not centre
this is how i setup the rectangle
the overlayView have width = 414 and height = 688
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let midX = overlayView.bounds.midX
let midY = overlayView.bounds.midY
let center = CGPoint(x: midX, y: midY)
let size: CGFloat = 312
// Create the initial layer from the view bounds.
let maskLayer = CAShapeLayer()
maskLayer.frame = overlayView.bounds
// Create the path.
let rect = CGRect(x: center.x, y: center.y, width: size, height: size)
let path = UIBezierPath(rect: overlayView.bounds)
maskLayer.fillRule = .evenOdd
// Append the overlay image to the path so that it is subtracted.
path.append(UIBezierPath(roundedRect: rect, cornerRadius: 20))
maskLayer.path = path.cgPath
// Set the mask of the view.
overlayView.layer.mask = maskLayer
}
I already try to calculate the center but still can't get the rect in the centre.
You have to subtract half-height and width from the center, as you have set start x and y position from the center point
so change your rect code.
let rect = CGRect(x: center.x - size/2, y: center.y - size/2, width: size, height: size)
What I am trying to do that draw a circle node using its radius and origin points.
Is there any way to draw a circle node in SceneKit?
How can I draw that?
First approach
Use SceneKit SCNCylinder's procedural mesh to create a circle.
func circle(origin: SCNVector3, radius: CGFloat) -> SCNNode {
let cylinderNode = SCNNode()
cylinderNode.geometry = SCNCylinder(radius: radius, height: 0.001)
cylinderNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
cylinderNode.geometry?.firstMaterial?.isDoubleSided = true
cylinderNode.position = origin
cylinderNode.eulerAngles.x = .pi/2
return cylinderNode
}
sceneView.scene?.rootNode.addChildNode(circle(
origin: SCNVector3(), radius: 0.5)
)
Second approach
This approach is impractical for your particular case, because you must specify the radius in UIKit units, not in meters like in SceneKit. However, I decided to publish it as option B.
// Path
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0),
radius: 20,
startAngle: 0.0,
endAngle: .pi * 2,
clockwise: true)
circlePath.flatness = 0.1
// Shape
let material = SCNMaterial()
material.diffuse.contents = UIColor.systemRed
material.isDoubleSided = true
let circleShape = SCNShape(path: circlePath, extrusionDepth: 0.001)
circleShape.materials = [material]
// Node
let circleNode = SCNNode()
circleNode.geometry = circleShape
sceneView.scene?.rootNode.addChildNode(circleNode)
You could probably do something like this:
let circle = SCNPlane(width: 1.0, height: 1.0)
circle.cornerRadius = circle.width/2
hope, this is, what you are looking for.
another approch might be the SCNCylinder with a very small height or even SCNTorus with a very small pipe radius.
How can you animate a mask moving down X pixels?
I have a circular mask applied to a black view that creates a spotlight effect. I want to animate the mask transitioning down 100 pixels, but using UIView.animate { mask.transform = CATransform3DTranslate(.init(), 0, 100, 0) } instantly moves it instead of animating the transition.
Here's the mask code:
private lazy var mask: CAShapeLayer = {
let mask = CAShapeLayer()
let path = CGMutablePath()
path.addArc(center: CGPoint(x: 0, y: 0), radius: 40, startAngle: 0, endAngle: 2.0 * .pi, clockwise: false)
path.addRect(CGRect(origin: .zero, size: view.frame.size))
mask.backgroundColor = UIColor.black.cgColor
mask.path = path
mask.fillRule = .evenOdd
return mask
}()
overlayView.layer.mask = mask
How would you animate this moving down 100 pixels?
To get the spotlight effect, you'll want to create paths that modify the arc shape and animate between them.
func spotlightPath(arcCenter: CGPoint) -> CGPath {
let path = CGMutablePath()
// move the spotlight
path.addArc(center: CGPoint(x: arcCenter.x, y: arcCenter.y), radius: 40, startAngle: 0, endAngle: 2.0 * .pi, clockwise: false)
path.addRect(CGRect(origin: .zero, size: view.frame.size))
return path
}
let startPath = spotlightPath(arcCenter: .zero)
And then later
let endPath = spotlightPath(arcCenter: CGPoint(x: 40, y: 100))
let animation = CABasicAnimation(keyPath: "path")
animation.duration = 1
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animation.toValue = endPath
mask.add(animation, forKey: "path")
Try this Core animation block. CALayer animation is not supported by UIView animation block. To animate mask property we use the path key.
let anim = CABasicAnimation(keyPath: "path")
anim.toValue = createPath(center: .init(x: 100, y: 100))
anim.duration = 1
anim.fillMode = .forwards
anim.timingFunction = CAMediaTimingFunction.init(name: .easeInEaseOut)
anim.isRemovedOnCompletion = false
mask.add(anim, forKey: anim.keyPath)
I'm trying to develop a game that moves the circle according the left eye
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let camera = self.sceneView.session.currentFrame?.camera
let pippo = simd_mul((anchor.transform),faceAnchor.leftEyeTransform)
var j: SCNVector3 = SCNVector3(pippo.columns.3.x ,pippo.columns.3.y , -pippo.columns.3.z);
self.circlePath = UIBezierPath(arcCenter: CGPoint(x: CGFloat( j.x * Float(screenHeight/2) ),y:CGFloat( j.y * Float(screenWidth/2) )), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)
self.shapeLayer = CAShapeLayer()
self.shapeLayer.path = self.circlePath.cgPath
self.shapeLayer.fillColor = UIColor.clear.cgColor
self.shapeLayer.strokeColor = UIColor.red.cgColor
self.shapeLayer.lineWidth = 3.0
self.view.layer.addSublayer(self.shapeLayer)
I move a bit the circle, but it is centered on the leftmost part of the screen (instead of being in the middle) and the movement doesn't cover the whole screen, but just a part of it, how can I solve it?
thanks in advance.
As an example, I have a circle which travels to the left and then the right on the screen. I want the circle to only be visible if it is inside two specific squares (maskNodes). I'm using SKCropNode to try and achieve this, but the SKCropNode mask only lets me assign one mask. Does anybody know a way of assigning two or more mask to a SKCropNode, or if it is even possible to do so. Thanks!
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.whiteColor()
let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask1.position.x = -100
let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask2.position.x = 100
let cropNode = SKCropNode()
cropNode.maskNode = mask1 // && mask2
addChild(cropNode)
let circle = SKShapeNode(circleOfRadius: 25)
circle.fillColor = UIColor.blackColor()
cropNode.addChild(circle)
// Move Circle
let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))
circle.runAction(seq)
}
Figured it out, just has to add the two mask to a parent and then assign the parent as the SKCropNode mask.
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.whiteColor()
let maskParent = SKSpriteNode()
let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask1.position.x = -100
maskParent.addChild(mask1)
let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask2.position.x = 100
maskParent.addChild(mask2)
let cropNode = SKCropNode()
cropNode.maskNode = maskParent
addChild(cropNode)
let circle = SKShapeNode(circleOfRadius: 25)
circle.fillColor = UIColor.blackColor()
cropNode.addChild(circle)
// Move Circle
let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))
circle.runAction(seq)
}