Clipping multiple cclayers inside of a parent cclayer - ios

I have a CCScrollLayer that i would like to set up multiple CCLayers inside of it to swipe pages back and fourth. Some of the content expands over the page CCLayer and into the next page, so while swiping you can see things from the previous page pushing onto the next one. What i would like to achieve is similar to the UIView function where it clips to the bounds, where you will not see anything outside the contentSize of the layer. Does anyone know how to go about doing this? I would like the size of the page to being the size of the devices screen. I've tried doing this but it doesn't give the desired result:
kmGLPushMatrix();
glEnable(GL_SCISSOR_TEST);
CGPoint startingPoint = ccp(0,0);
glScissor(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
glDisable(GL_SCISSOR_TEST);
kmGLPopMatrix();
Any help would be awesome!

Have you looked at a CCClippingNode (cocos2d version 2.+) ... Visit at the visit method to get some inspiration. Here, from the doc:
/** CCClippingNode is a subclass of CCNode.
It draws its content (childs) clipped using a stencil.
The stencil is an other CCNode that will not be drawn.
The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold).
*/

Related

iOS swift - how to draw an ellipse with touch event

currently I am trying to port my old windows phone app to iphone. The UI looks like this:
I want to know how to do the following:
Draw a solid ellipse on a specific position, e.g. at position x = 0, y = 0;
The ellipse change its color upon touch;
Touch another position on screen, the ellipse animated to move to the specific position.
I tried several tutorial on how to override drawRect in UIView, but couldn't get it work, can anyone help provide a simple sample?
Thanks a lot!
Edit: Any sample code is highly appreciated.
There aren't much swift code on the internet and samples are not easy to find...
Animating inside a drawRect is never recommended. Why not just put an UIView on the place where you want it and animate the coordinates of that view?
The UIView could be a custom view responding to touch and then triggering the drawRect with a new color.
The parent view where you place these views on can respond to touches and move the ellipse view to the right position.

Touch sensor for a line drawn in IOS

I have a requirement to draw a line on a UIView in IOS with OpenGL ES. But the line that is drawn could be able to move anywhere on the view when it is touched and dragged. Simply the user can re-position the line on the screen. I have no idea how this can be implemented. The line will be drawn with OpenGL ES.
I request all your helps and suggestions regarding this.
The solution is to use a second transparent view positioned over the OpenGL view, which acts as a proxy to your line.
Create a clear view (or a view with 0.0001 opacity) and add gesture recognizers to it. Initially place that view (say sized to 44 x 320), and centered over your line (I'm assuming its horizontal). When the user drags the clear view around, post the new position you want your line to appear in within the OpenGL view then tell it to refresh.
I seem to recall (but am unsure) if you can use gesture recognizers with completely clear views - you may need to make the view slightly opaque (but no visibly so) to get touches, but again just not sure.

Extending the DrawingView frame has some issues in iOS

I am implementing drawing in iphone, and my drawing works perfectly, I have provided the user a drawingView of size (315x95) and below this drawingView their is a image which of size (320x100). This is the default View which user sees and he can draw only in that View.
Now what I have done is provided a button at one corner of the drawingView , so on click of which the user can increase the drawingView if he/she wants to write more and again their is button at the bottom of this extended View to decrease the view to its original position.
Now , my problems are
1) when I draw in default size and click on the button to extend , the drawn Image gets stretched to extended frame, which I dont want, becuase I am creating extra space for use to right so stretching is of no use
2) when I extend the drawingView and draw something at the bottom, and then click the button to decrease the View, the drawn part at the bottom still remains at the bottom only and is visible.Actually it should get hidden when the drawingView frame is decreased and should be seen when the drawingView frame is increased.
So friends, how can I solve this issue,please help me out
Regards
Ranjit
Number 1 sounds like a content mode issue, but number 2 is the result of your views not clipping their subviews properly (or the layer not being clipped to its bounds). There are properties like clipsToBounds and clipsSubviews that will come in handy.

How can I make one part of view scrollable?

This has probably been asked a bunch, so I'm more looking for direction to somewhere I can research this, unless it is an easy straight-forward answer. I can't think of the propper terminology to search for it; I just keep getting the same standard UIScrollView info.
I understand how to make a scrollable view, but I can't find anything on making say a 310 x 310 pixel part in the center of my view to be scrollable, like a scrollable div in the center of a webpage so it can have a nice image border around the content that would scroll in the center.
Just create a UIScrollView and set its frame to the size and location that you want and then add it to another view.
You can accomplish this with the addSubview method of UIView.
Create a UIImageView that spans the size of the screen (or whatever space you would like it to take up). This will contain the 'image border' you refer to. Let's say that view is named backgroundImageView.
Then create your UIScrollView; let's say it's called myScrollView. Now for this line:
[backgroundImageView addSubview:myScrollView];
That will add myScrollView inside backgroundImageView in the view hierarchy. Now you set myScrollView's position in the coordinate system of backgroundImageView. So to center it, do this:
[myScrollView setFrame:CGRectMake((backgroundImageView.frame.size.width - myScrollView.frame.size.width) / 2, (backgroundImageView.frame.size.height - myScrollView.frame.size.height) / 2, myScrollView.frame.size.width, myScrollView.frame.size.height)];
And voila, you're done :)

Clipping parent view by subview and put content of first one in second one but scaled

the question is...
I have a parent view in which i drew a planimetry composed by a lot of CGPaths.
Well, in this App, i can insert a subview to parent view in which there are drawn others CGPaths (like lines which represent a chair, for example). Now. I would replicate zoom effect like UITextView has got when someone Tap Down, hold Tap, and an hand-glass comes out with content scaled.
I would put a subview in which the background represents parent content clipped in subview rect and scaled of x2 for example. In this way, subview background seems an hand-glass on parent view.
How can i implement this ?
I thought about taking parent context, passing it to subview, and clipping parent context with rect subview. But nothing happens. I red about CGImageContext with witch taking an image representation about parent CGContext, and pass it to subview, but i dont know how implementing it. And, i dont know if it's right way.
Hope someone can help me (with code sample would be awesome)!
Sorry for my english. I tried to do my best :)
Question solved by myself!
In subview drawrect: method i put this code:
- (void)drawRect:(CGRect)rect {
// here we're just doing some transforms on the view we're magnifying,
// and rendering that view directly into this view.
// By 'touchPoint' i can select the exact portion of superview which
// i want render in subview
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(self.frame.size.height*0.5));
CGContextScaleCTM(context, 1.5, 1.5);
CGContextTranslateCTM(context,-1*(touchPoint.x),-1*(touchPoint.y));
[self.superview.layer renderInContext:context];
}
Hope this can help someone else :)

Resources