sub CALayer can not be displayed - objective-c

my code is like this:
// layer
highlightLayer = [[CALayer alloc]init];
highlightLayer.frame = CGRectMake(0, 420, 320, 11);
highlightLayer.backgroundColor = [UIColor greenColor].CGColor;
CALayer *contentLayer = [[CALayer alloc]init];
contentLayer.frame = CGRectMake(0, 420, 80, 11);
contentLayer.backgroundColor = [UIColor redColor].CGColor;
[highlightLayer addSublayer:contentLayer];
[contentLayer release];
but this sublayer does not appear. i don't know why.

Because contentLayer's frame is outside highlightLayer's bounds (0, 0, 320, 11). contentLayer's frame is expressed in highlightLayer's coordinate system. I think you should simply adjust contentLayer's frame origin.

Related

Image doesn't scroll with text

Sorry for my english firstly.
I have an image and a text which are both must be scrolled.But they don't.
So here i have a piece of my code,where i've trying to do what i need to:
self.newsBody.text = [self bodyWithoutTags:self.newsContent];
NSData *imgData = [ServerRequest getImgFromServer:self.newsImage];
UIImage *img = [UIImage imageWithData:imgData];
self.scrollView.contentSize = CGSizeMake(320, 225);
self.newsImageView.image = img;
self.newsImageView.contentMode = UIViewContentModeScaleAspectFit;
self.newsImageView.clipsToBounds = YES;;
self.newsImageView.frame = CGRectMake(0, 0, 320, 204);
self.newsBody.frame = CGRectMake(0, 202, 320, 225);
self.newsBody.clipsToBounds = YES;
self.newsBody.autoresizesSubviews = YES;
self.newsBody.scrollEnabled = NO;
[self.scrollView addSubview:self.newsImageView];
[self.scrollView addSubview:self.newsBody];
Thank you for help!
Your real content size ...
self.newsImageView.frame = CGRectMake(0, 0, 320, 204);
self.newsBody.frame = CGRectMake(0, 202, 320, 225);
... is 320 x ( 202 + 225 ), which is 320 x 427. But you do set scroll view's content size to ...
self.scrollView.contentSize = CGSizeMake(320, 225);
... which is actually wrong. Scrolling is done when you do set correct content size and content size is bigger than scroll view itself. Otherwise it's not scrolling, no need to scroll if your content is smaller than scroll view itself.

why I get the converse mask with the "CGMutablePathRef" method

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);

UITextView Shadow Not Working

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.
UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450);
UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0
green: 100/255.0
blue: 150/255.0
alpha: 1.0];
view.backgroundColor = baseColor;
...
UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)];
newCommentField.text = #"New Comment";
newCommentField.tag = 3;
newCommentField.layer.shadowOffset = CGSizeMake(0, 3);
newCommentField.layer.shadowRadius = 5.0;
newCommentField.layer.shadowColor = [UIColor blackColor].CGColor;
newCommentField.layer.shadowOpacity = 0.8;
[view addSubview:newCommentField];
...
self.tableView.tableHeaderView = view;
Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.
You're not setting background color for newCommentField.
Try:
newCommentField.backgroundColor = [UIColor whiteColor];
You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)];
bgView.backgroundColor=[UIColor whiteColor];
bgView.layer.shadowOffset = CGSizeMake(0, 3);
bgView.layer.shadowRadius = 5.0;
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOpacity = 0.8;
[bgView addSubview:newCommentField];
[view addSubview:bgView];

Using a CALayer as a mask to a backing layer of a GLKView blocks touch events?

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!

insertSublayer atIndex puts things on top of other layers

I'm using ColorBGView and calling methods inside of it to make gradients. But if I attach two to my layer, it overlaps everything else I attach afterward, and I don't understand why.
CAGradientLayer *bgLayer = [ColorBGView pinkSky1];
bgLayer.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view.layer insertSublayer:bgLayer atIndex:0];
//If I insert this sublayer (even at a lower index)
//it overlaps everything I attach afterward
CAGradientLayer *bgLayer2 = [ColorBGView darkCave];
bgLayer2.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view.layer insertSublayer:bgLayer2 atIndex:-10];
// add custom view
parallaxView = [[ParallaxView alloc] init];
parallaxView.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view insertSubview:parallaxView atIndex:1];
[parallaxView release];