Error when using layout anchors with UIStackView - objective-c

Trying to add a stackView and some children to it, but get layout errors. What am I doing wrong?
The problem seem to occur when the trailinAchors constant is lower then the leadingAchors constant.
self.stackview.axis = .vertical
self.stackview.alignment = .fill
self.stackview.distribution = .fill
self.stackview.spacing = 5
self.addSubview(self.stackview)
self.stackview.translatesAutoresizingMaskIntoConstraints = false
self.stackview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
self.stackview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
self.stackview.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
self.stackview.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
// Holder
let holderView = UIView()
holderView.backgroundColor = UIColor.red
self.stackview.addArrangedSubview(holderView)
// Subview
let view3 = UIView()
view3.backgroundColor = UIColor.orange
holderView.addSubview(view3)
view3.translatesAutoresizingMaskIntoConstraints = false
view3.topAnchor.constraint(equalTo: holderView.topAnchor, constant: 0).isActive = true
view3.bottomAnchor.constraint(equalTo: holderView.bottomAnchor, constant: 0).isActive = true
view3.leadingAnchor.constraint(equalTo: holderView.leadingAnchor, constant: 30).isActive = true
view3.trailingAnchor.constraint(equalTo: holderView.trailingAnchor, constant: -30).isActive = true
Error:
2017-06-30 17:38:37.124333+0200 XXXXX[83954:2132661] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x61000028b680 H:|-(30)-[UIView:0x7f8dab565720] (active, names: '|':UIView:0x7f8dab566620 )>",
"<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)>",
"<NSLayoutConstraint:0x61000028b4a0 H:|-(0)-[UIStackView:0x7f8dab569320] (active, names: '|': XXXXX.MultipleBarGraphsView:0x7f8dab561190'What are you doing' )>",
"<NSLayoutConstraint:0x61000028b3b0 UIStackView:0x7f8dab569320.trailing == XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.trailing (active)>",
"<NSLayoutConstraint:0x61000028b900 '_UITemporaryLayoutWidth' XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.width == 0 (active)>",
"<NSLayoutConstraint:0x61000028af00 'UISV-canvas-connection' UIStackView:0x7f8dab569320.leading == UIView:0x7f8dab566620.leading (active)>",
"<NSLayoutConstraint:0x61000028bbd0 'UISV-canvas-connection' H:[UIView:0x7f8dab566620]-(0)-| (active, names: '|':UIStackView:0x7f8dab569320 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

What view is this in? I suspect that the leading and trailing anchors of the tableview's superview may have the same value (same location). Make sure the tableview's superview is properly constrained to its own superview (could be the view controller) so the frame has a width and height > 0.

Realised that I called
layoutIfNeeded()
at the bottom of the function. When I removed this, the errors went away.

Related

How to animate geometry using the primitiveRange property in SceneKit?

I am using Apple's SceneKit to render a tube which consists of 50 triangle strips as illustrated here:
I want to animate a "growing" tube by using the primitiveRange property.
let tube = createTube() // create's tube.geometry which has 50 elements
let tubeNode = SCNNode(geometry: tube.geometry)
...
let animation = CABasicAnimation(keyPath: "geometry.elements[0].primitiveRange")
animation.fromValue = NSValue(range: NSRange(location: 0, length: 0))
animation.toValue = NSValue(range: NSRange(location: 0, length: tube.geometry.elements.count))
animation.duration = 5
animation.repeatCount = Float.infinity
tubeNode.geometry?.addAnimation(animation, forKey: "don't care")
scene.rootNode.addChildNode(tubeNode)
Nothing is animating (I simply see the full tube above) and I am not getting any warnings on the console. If I change the key "geometry.elements.primitiveRange" to "foo" I do get the following warning on the console
ExtrudedTube[98737:24354579] [SceneKit] Error: _C3DModelPathResolverRegistryResolvePathWithClassName unknown path (
foo
)
Perhaps this property is not animatable? I don't see documentation that says it is or isn't. Any idea how to create the "growing tube" animation using this mesh in SceneKit?
I was never able to pull off the animation using the "keypath" (I think there probably is a way to do it, but there is no documentation on the details). I can use SCNAction though it it works well:
if let numPrimitives = tubeNode.geometry?.elements[0].primitiveCount {
tubeNode.geometry?.elements[0].primitiveRange = NSRange(location: 0, length: 0)
let duration : Double = 5.0
let growTube = SCNAction.customAction(duration: duration, action: {node, elapsedTime in
let f = Double(elapsedTime)/duration
let n = Int(f*Double(numPrimitives))
node.geometry?.elements[0].primitiveRange = NSRange(location: 0, length: n)
})
tubeNode.runAction(growTube)
}

How do you get motion blur working in SceneKit?

According to WWDC 2017, motionBlurIntensity was added as a property to SCNCamera. I've tried the following and failed to get SceneKit to blur my scene when the camera is moved:
Set wantsHDR to true
Add SCNDisableWideGamut as a Boolean with the value of YES in every Info.plist in my Xcode project
Move a SCNBox by changing its SCNNode's position in front of the camera with motionBlurIntensity set to 1.0
Move the camera itself by changing its SCNNode's position with motionBlurIntensity set to 1.0
Animate the camera using an SCNTransaction with motionBlurIntensity set to 1.0 instead of changing its position each frame
Do the above with motionBlurIntensity set to 500 or greater
I run the following code every rendered frame like so:
camNode.position = SCNVector3Make(cx, cy, cz);
camNode.eulerAngles = SCNVector3Make(rotx, roty, rotz);
camNode.camera.wantsDepthOfField = enableDOF;
camNode.camera.wantsHDR = enableHDR;
camNode.camera.zNear = camNearVal;
camNode.camera.zFar = camFarVal;
camNode.camera.focalLength = camFocalLength;
camNode.camera.usesOrthographicProjection = usingOrthoProjection;
if(!usingOrthoProjection)
{
camNode.camera.projectionTransform = SCNMatrix4FromGLKMatrix4(GLKMatrix4MakeWithArray(projection));
}
else
{
// Ortho options
camNode.camera.orthographicScale = orthoScale;
if(cam_projectionDir == 1)
camNode.camera.projectionDirection = SCNCameraProjectionDirectionHorizontal;
else
camNode.camera.projectionDirection = SCNCameraProjectionDirectionVertical;
}
// DOF
camNode.camera.sensorHeight = dof_sensorHeight;
camNode.camera.focusDistance = dof_focusDistance;
camNode.camera.fStop = dof_fStop;
camNode.camera.apertureBladeCount = dof_apertureBladeCount;
camNode.camera.focalBlurSampleCount = dof_focalBlurSampleCount;
// Motion blur
camNode.camera.motionBlurIntensity = motionBlurIntensity;
And here is where the SCNRenderer sets its pointOfView to the camera:
mainRenderer.scene = mainScene;
mainRenderer.pointOfView = camNode;
id<MTLCommandBuffer> myCommandBuffer = [_commandQueue commandBuffer];
[mainRenderer updateAtTime: currentFrame];
[mainRenderer renderWithViewport:CGRectMake(0,0,(CGFloat)_viewWidth, (CGFloat)_viewHeight) commandBuffer:myCommandBuffer passDescriptor:_renderPassDescriptor];
[myCommandBuffer commit];
HDR effects like Bloom and SSAO work properly, just not motion blur.
I'm using Xcode Version 10.1 on macOS Mojave.
I ran the Badger sample app and the motion blur in that project works on my computer.
Am I missing something here? Any insight would be greatly appreciated.
The default value of a motionBlurIntensity property is 0.0. It results in no motion blur effect. Higher values (toward a maximum of 1.0) create more pronounced motion blur effects. Motion blur is not supported when wide-gamut color rendering is enabled. Wide-gamut rendering is enabled by default on supported devices; to opt out, set the SCNDisableWideGamut key in your app's Info.plist file.
Test it with my code:
let cameraNode1 = SCNNode()
cameraNode1.camera = SCNCamera()
cameraNode1.camera?.motionBlurIntensity = 3 // MOTION BLUR
scene.rootNode.addChildNode(cameraNode1)
let move = CABasicAnimation(keyPath: "translation")
move.fromValue = NSValue(scnVector3: SCNVector3(x: 7, y: -5, z: 0))
move.toValue = NSValue(scnVector3: SCNVector3(x: 7, y: 5, z: 0))
move.duration = 0.5
move.repeatCount = 20
move.autoreverses = true
move.repeatCount = .infinity
let sphereNode1 = SCNNode()
sphereNode1.geometry = SCNSphere(radius: 2)
sphereNode1.addAnimation(move, forKey: "back and forth")
scene.rootNode.addChildNode(sphereNode1)
Works perfectly.

Creating buttons with unique names through a for loop in Swift 4

Im building an app with a complex UI that requires 45 buttons. For a variety of reasons, I am not using Interface Builder. In order to update properties for specific buttons, I need each of the buttons to have a unique name, (Btn1, Btn2, Btn3...) Here is the code that I am using:
let buttonsArray = ["Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6",
"Btn7", "Btn8", "Btn9", "Btn10", "Btn11", "Btn12",
"Btn13", "Btn14", "Btn15", "Btn16", "Btn17", "Btn18",
"Btn19", "Btn20", "Btn21", "Btn22", "Btn23", "Btn24",
"Btn25", "Btn26", "Btn27", "Btn28", "Btn29", "Btn30",
"Btn31", "Btn32", "Btn33", "Btn34", "Btn35", "Btn36"]
for button in buttonsArray {
let button = UIButton(frame: CGRect(x: nextx, y: nexty, width: btnsiz, height: btnsiz))
button.isUserInteractionEnabled = true
button.alpha = 0.05
button.tag = tagnum
tagnum += 1
button.backgroundColor = UIColor(red: 0.997, green: 0.645, blue: 0.014, alpha: 1.0)
button.setTitle(notesarray[count], for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.clipsToBounds = true
button.backgroundColor = UIColor(red: 0.991, green: 0.607, blue: 0.33, alpha: 1.0)
button.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
self.view.addSubview(button)
btnarray[count] = false
setNextX()
element += 1
count += 1
}
When a button is pressed it is recognized. I have assigned a unique tag number. I want to be able to update properties based upon the button name (Btn1) such as:
Btn1.isUserInteractionEnabled = true. I get the error message "Use of unresovled identifier 'Btn1'. How do I get the name specified as I could in Interface Builder with IBOutlet?
I'm afraid IBOutlets are an Interface Builder only thing - hence the 'IB' in the name 'IBOutlet'.
If you want to get a reference to a subview of view, you can use the UIView tag property. I see that you are already using this tag property, passing it 'tagnum', which looks like it increments for every button, similar to the names in the ButtonsArray.
For example, if you want to get the button with tag 1, you could request it like this:
let Btn1 = self.view.viewWithTag(1)
You could then do what you're suggesting:
Btn1.isUserInteractionEnabled = true
An aside, I would recommend you name your variables beginning with a lower case letter to follow best practices:
Follow case conventions. Names of types and protocols are
UpperCamelCase. Everything else is lowerCamelCase.

NSLayoutConstraint.constraintsWithVisualFormat align bottom right

I want to put a indicator view in a bottom right of container view, i have this code to insert view
var img:UIImageView?
img = UIImageView(frame: CGRect(x: 0, y: 0, width: singleCheckViewWidth, height: singleCheckViewHeight))
img!.image = UIImage(named: "check")
img!.tag = 21
img!.translatesAutoresizingMaskIntoConstraints = false
img!.backgroundColor = UIColor.redColor()
//Aggiungo la vista al fumetto
cell.textView!.addSubview(omg!)
The problem is that with this code i have icon in a top left with 10px margin and it works fine:
let iconVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[indicator(\(singleCheckViewHeight))]",
options: [],
metrics: nil,
views: views)
allConstraints += iconVerticalConstraints
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-10-[indicator(\(singleCheckViewWidth))]",
options: [],
metrics: nil,
views: views)
allConstraints += horizontalConstraints
here is image:
but if i try to move image in a bottom left with this code:
let iconVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[indicator(\(singleCheckViewHeight))]-10-|",
options: [],
metrics: nil,
views: views)
allConstraints += iconVerticalConstraints
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:[indicator(\(singleCheckViewWidth))]-10-|",
options: [],
metrics: nil,
views: views)
allConstraints += horizontalConstraints
i don't see the image anymore, if i try to put image in a top left with this code:
let iconVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[indicator(\(singleCheckViewHeight))]",
options: [],
metrics: nil,
views: views)
allConstraints += iconVerticalConstraints
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:[indicator(\(singleCheckViewWidth))]-10-|",
options: [],
metrics: nil,
views: views)
allConstraints += horizontalConstraints
I obtain image outside the container, like the image below:
Any ideas what's wrong? and how can i move the icon in a bottom right angle?
Thanks!
If all of the parent views haven't been aligned properly, nor the siblings, things could go Awry. On top of that,
cell.textView!.addSubview(omg!)
omg? I assume you meant img? Or am I wrong?
Also, on top of this, there's no mention as to what kind of container you're placing this in?
If it's just an NSView or UIView, then xyz.cell isn't the place to put it. If its a TableiewView, a cell could be, but are the constraints all set up. If its iOS, then is the UITableViewCell, the topmost view "of the cell" i.e., did you add a background view as an image view which could be overshadowing it?
Could you elaborate a little as to what the scenario is so we can get a better picture? (no pun intended)

merge two sprite nodes in sprite kit

I am designing a sprite kit game where I want to merge two sprite nodes (rect shapes) together and then act and move them as one sprite node ?
I've been searching lot and I didn't find results
Is that possible in sprite kit ? and how can I do it ?
I'll appreciate any help, Thanks
Add one node as a child of the other. If you set it's node2.physicsbody.dynamic=NO; it will move as a single node. There will be separate physicsbodies but you can set them to the same category bitmask.
The answer linked here may help with notation: How to detect contact on different areas of a physicsbody
If you have two sprite nodes, you simply do this :
[firstSprite addChild:secondSprite]
Here is an answer with which I have achieved what you are looking for using SpriteKit (Swift)
First apply Physics to your scene
Example:
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
physicsWorld.contactDelegate = self
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 1
self.physicsBody = sceneBody
self.physicsBody?.categoryBitMask = PhysicsCategory.Boarder
self.physicsBody?.contactTestBitMask = PhysicsCategory.None
self.physicsBody?.collisionBitMask = PhysicsCategory.Item
Second, Apply Physics to the two skspritenode's that you want to join together
Example
func mySpritetie1() {
spritetie1 = SKSpriteNode(imageNamed: "img1")
spritetie1.name = kAnimalNodeName
spritetie1.position = CGPoint(x: 170, y: 35)
spritetie1.size = CGSizeMake(36, 100)
spritetie1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(22, 100))
spritetie1.physicsBody?.affectedByGravity = false
spritetie1.physicsBody?.categoryBitMask = PhysicsCategory.Item
spritetie1.physicsBody?.collisionBitMask = PhysicsCategory.Boarder
spritetie1.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
self.addChild(spritetie1)
}
func mySpritetie2() {
spritetie2 = SKSpriteNode(imageNamed: "img2")
spritetie2.name = kAnimalNodeName
spritetie2.position = CGPoint(x: 250, y: 60)
spritetie2.size = CGSizeMake(90, 79)
spritetie2.physicsBody = SKPhysicsBody(circleOfRadius: 25)
spritetie2.physicsBody?.affectedByGravity = false
spritetie2.physicsBody?.categoryBitMask = PhysicsCategory.Item
spritetie2.physicsBody?.collisionBitMask = PhysicsCategory.Boarder
spritetie2.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
self.addChild(spritetie2)
}
Third, now joining them
Example
let joint = SKPhysicsJointFixed.jointWithBodyA(spritetie1.physicsBody!, bodyB: spritetie2.physicsBody!,
anchor: CGPointMake(CGRectGetMidX(spritetie1.frame), CGRectGetMinY(spritetie2.frame)))
self.physicsWorld.addJoint(joint)
This will join the two body at the anchor point you give.
Now you can animate any one of that (spritetie1 or spritetie2) using SKAction and the other will animate as if they are one and the same joined at the anchor point you defined.
If you feel this answer helped please do approve it. Hope it will help someone out there.
Please use this two link to learn about all thats in my code:
https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html
A very basic and good tutorial to learn about simulating physics https://www.youtube.com/playlist?list=PLa41xEuH5n_pwuNm7Z3L5qxDhVE0sOEqb
Thanks