I'm trying to draw vertical line on the left border of screen, but line is't showed. What is wrong?
CGRect leftRect = CGRectMake(0, 0, 10, self.size.height);
SKShapeNode *leftBorder = [SKShapeNode shapeNodeWithRect:leftRect];
leftBorder.strokeColor = [SKColor redColor];
leftBorder.fillColor = [SKColor redColor];
leftBorder.name = #"leftBorder";
leftBorder.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:leftBorder.frame];
leftBorder.physicsBody.dynamic = NO;
[self addChild:leftBorder];
You must set size gameScene ,when u init it.
_gameScene = [GameScene unarchiveFromFile:#"GameScene"];
// Here
_gameScene.size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
_gameScene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:_gameScene];
Try this out
SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 100.0, 100.0);
CGPathAddLineToPoint(pathToDraw, NULL, 50.0, 50.0);
yourline.path = pathToDraw;
[yourline setStrokeColor:[UIColor redColor]];
[self addChild:yourline];
If it's not working, than it can be self. Be sure, that self is instance of SKScene.
Related
Hi I used the the following code to create a bottom border to my label
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor darkGrayColor].CGColor;
border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _label.frame.size.width, _label.frame.size.height);
border.borderWidth = borderWidth;
[_label.layer addSublayer:border];
_label.layer.masksToBounds = YES;
But it the border is breaks at half of label. How can I fix it. Thanks in advance.
You can do like this :
With the use of CALayer you can create border on UILabel or any other UIControl.
1) Bottom Border
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor blackColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(0, CGRectGetHeight(myLabel.frame)-1, CGRectGetWidth(myLabel.frame), 1);
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:bottomBorder];
2) Top Border
CALayer *topBorder = [CALayer layer];
topBorder.borderColor = [UIColor blackColor].CGColor;
topBorder.borderWidth = 1;
topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(myLabel.frame), 1);
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:topBorder];
3) Left Border
CALayer *leftBorder = [CALayer layer];
leftBorder.borderColor = [UIColor blackColor].CGColor;
leftBorder.borderWidth = 1;
leftBorder.frame = CGRectMake(0, 0, 1, CGRectGetHeight(myLabel.frame));
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:leftBorder];
4) Right Border
CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor blackColor].CGColor;
rightBorder.borderWidth = 1;
rightBorder.frame = CGRectMake(CGRectGetWidth(myLabel.frame)-1, 0, 1, CGRectGetHeight(myLabel.frame));
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:rightBorder];
Try it,
CALayer* layer = [lbl layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1,layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor blackColor].CGColor];
[layer addSublayer:bottomBorder];
You need to calculate width of label based on string and also font which you require.
You can get size like this
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
and use expectedLabelSize while setting frame for your border.
border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _expectedLabelSize.width , _label.frame.size.height);
Hope it helps. Happy Coding !!
Please try with the following code.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
[lbl setText:#"Testo di prova..."];
[lbl setBackgroundColor:[UIColor clearColor]];
[[self view] addSubview:lbl];
[lbl sizeToFit];
CALayer* layer = [lbl layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor blackColor].CGColor];
[layer addSublayer:bottomBorder];
How do you make a circle or another shape in sprite kit I have seen some that use CGPath but i haven't really ever used CGPath ever . I was able to make a square with SKSpritenode but I can't seem to make triangles or circles.
This creates an SKShapeNode and sets its path property to a circle path with radius 16.
SKShapeNode *shape = [SKShapeNode node];
CGRect rect = CGRectMake(0, 0, 32, 32);
shape.path = [self circleInRect:rect];
shape.strokeColor = [SKColor greenColor];
shape.fillColor = [SKColor redColor];
shape.position = CGPointMake(100,100);
[self addChild:shape];
This method returns a CGPath object initialized with a oval path
- (CGPathRef) circleInRect:(CGRect)rect
{
// Adjust position so path is centered in shape
CGRect adjustedRect = CGRectMake(rect.origin.x-rect.size.width/2, rect.origin.y-rect.size.height/2, rect.size.width, rect.size.height);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:adjustedRect];
return bezierPath.CGPath;
}
Here's a triangle path...
- (CGPathRef) triangleInRect:(CGRect)rect
{
CGFloat offsetX = CGRectGetMidX(rect);
CGFloat offsetY = CGRectGetMidY(rect);
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(offsetX, 0)];
[bezierPath addLineToPoint: CGPointMake(-offsetX, offsetY)];
[bezierPath addLineToPoint: CGPointMake(-offsetX, -offsetY)];
[bezierPath closePath];
return bezierPath.CGPath;
}
Code snippet to create Triangle using Sprite Kit
SKShapeNode* leftContainer = [SKShapeNode node];
leftContainer.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGMutablePathRef pathLeftContainer = CGPathCreateMutable();
// Move to position and Draws line to form a triangle
CGPathMoveToPoint(pathLeftContainer, NULL, 60, 100);
CGPathAddLineToPoint(pathLeftContainer, 0, 120, 160);
CGPathAddLineToPoint(pathLeftContainer, 0,180,100);
CGPathAddLineToPoint(pathLeftContainer, 0,60,100);
[leftContainer setPath:pathLeftContainer];
leftContainer.lineWidth = 10.0;
leftContainer.strokeColor = [UIColor redColor];
leftContainer.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:leftContainer.path];
leftContainer.physicsBody.dynamic = FALSE;
[leftContainer.physicsBody setAllowsRotation:FALSE];
[self addChild:leftContainer];
CGPathRelease(pathLeftContainer);
Code snippet to create Circle using Sprite Kit
SKShapeNode* leftContainer = [SKShapeNode node];
leftContainer.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGMutablePathRef pathLeftContainer = CGPathCreateMutable();
// Draw circle with radius 20
CGPathAddArc(pathLeftContainer, NULL, 0, 20, 20, 0, 2*M_PI, true);
[leftContainer setPath:pathLeftContainer];
leftContainer.lineWidth = 10.0;
leftContainer.strokeColor = [UIColor redColor];
leftContainer.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:leftContainer.path];
leftContainer.physicsBody.dynamic = FALSE;
[leftContainer.physicsBody setAllowsRotation:FALSE];
[self addChild:leftContainer];
CGPathRelease(pathLeftContainer);
I am trying to modify SKShapeNode that I already added to SKNode.
This is my code for adding SKNode to the screen and attaching SKShapeNode to it. Now I am trying to modify color of that specific SKShapeNode, but I am not sure how to do it. Any advice?
SKNode *dot = [SKNode node];
SKShapeNode *circle = [SKShapeNode node];
circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
circle.fillColor = [UIColor blueColor];
circle.strokeColor = [UIColor blueColor];
circle.glowWidth = 5;
[dot addChild:circle];
[self addChild:dot];
try to remove all children and readd new child
[dot removeAllChildren];
[dot addChild:circle];
Make SKShapeNode a property of your SKScene:
#interface YourScene()
#property SKShapeNode *circle;
#end
Change the code which creates the circle to this:
self.circle = [SKShapeNode node];
self.circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
self.circle.fillColor = [UIColor blueColor];
self.circle.strokeColor = [UIColor blueColor];
self.circle.glowWidth = 5;
[dot addChild:self.circle];
Now you can access circle node anywhere in the scene:
- (void)changeColor {
self.circle.fillColor = [SKColor redColor];
}
Another option is to give the node a name:
SKShapeNode *circle = [SKShapeNode node];
.....
circle = #"circle";
And access that node by name
- (void)changeColor {
// Assuming the dot node is a child node of the scene
SKShapeNode *circle = (SKShapeNode*)[self.scene childNodeWithName:#"/circle"];
circle.fillColor = [SKColor redColor];
}
I draw a view . The views frame = (0,0,44,44) and the views background color is black.
I want add a mask to make the view looks like this : the mask is a small circle in the middle of the view . but I get a converse result , only the middle of the view is not masked. the wrong result like this
my code is :
aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]];
CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = aView.bounds;
CGMutablePathRef p2 = CGPathCreateMutable();
CGPathAddEllipseInRect(p2, NULL, CGRectInset(mask.bounds, 10, 10));
mask.path = p2;
aView.layer.mask = mask;
CGPathRelease(p2);
[self addSubview:aView];
what is wrong with the code ? thanks.
First, add a rectangle to the masking path.
Second, set the fillRule property of shape layer to kCAFillRuleEvenOdd, so that the center part is left hollowed.
aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]];
CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.fillRule = kCAFillRuleEvenOdd;
mask.frame = aView.bounds;
CGMutablePathRef p2 = CGPathCreateMutable();
CGPathAddRect(p2, &CGAffineTransformIdentity, mask.bounds);
CGPathCloseSubpath(p2);
CGPathAddEllipseInRect(p2, NULL, CGRectInset(mask.bounds, 10, 10));
mask.path = p2;
aView.layer.mask = mask;
CGPathRelease(p2);
I'm trying to use a CAShapeLayer as a mask to the backing layer of a GLKView like so:
CGRect circleFrame = CGRectMake(0, 0, 800, 800);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleFrame];
CGPoint circleAnchor = CGPointMake(CGRectGetMidX(circleFrame) / CGRectGetMaxX(circleFrame),CGRectGetMidY(circleFrame) / CGRectGetMaxY(circleFrame));
shapeLayerMask = [[CAShapeLayer alloc] init];
shapeLayerMask.path = path.CGPath;
shapeLayerMask.anchorPoint = circleAnchor;
shapeLayerMask.frame = CGRectMake(0, 0,800, 800);
shapeLayerMask.fillColor = [UIColor blackColor].CGColor;
shapeLayerMask.backgroundColor = [UIColor clearColor].CGColor;
shapeLayerMask.opacity = 1.0f;
shapeLayerMask.hidden = NO;
self.view.layer.mask = shapeLayerMask;
But as soon as i do this, all my touch events stop firing. No more touchesBegan, touchesEnded, etc. Anyone knows why? Thanks in advance!