Adding two CCLayers in a single CCScene side by side - iphone

I am trying to add two CCLayers side by side (not one over another) on a single scene, with second layer having a table view on it. I have added the table view as the subview to the [[CCDirector sharedDirector] view]. The size of the second layer is that of the screen size & the size of the first layer is some what less than the size of the screen. When this screen appears, first second layer is shown. After clicking a particular button on the second layer, the scene animates to the right along with the table on second layer, showing the full first layer & some part of second layer.
Can anyone help me with it? I appreciate all your help in advance.

This has little or nothing to do with layers. A layer and its children are always drawn either on top of or behind another layer. And UIViews have no understanding of cocos2d layers or nodes at all.
What you want to do is to design the screen, then move the entire screen left or right. While moving, you need to update the table view's position accordingly every frame because it won't move along with cocos2d nodes. The CCUIViewWrapper may help but it's a little overkill if you merely want to update the view's position.
Whether you design your screen on the scene, a single or multiple layers makes no difference. In fact it's easier to use a single scene or layer because then you can animate both sides of the screen at once and in perfect synchronicity.

Related

how to detect touch which appear on the location greater than resolution of device

I'm looking for help. I'm trying to make some puzzle game and I have thin scrollable layer at the bottom of my main layer which contains some puzzle shapes. I can scroll the layer and see every shape but shapes were positioned manually in code on the scrollable layer and the problem is, if I try to detect if I touched on sprite, which position is greater than 1024(in first iPad) it doesn't work. it doesn't work because touch can have position inside 1024X768 and the position of shape is for example 1500x100. to make it clearer, shapes are sprites and i try to detect them using CGRectContainsPoints method. Is there any other way to make it or have you any ideas? thanks in advance :]
What you could do is subclass your sprite, create a delegate for it and assign your main view/class as the delegate.
Implement the appropriate touch method, and send the message of what sprite was selected to your delegate (Main view or desired controller class).
With this every sprite has the same delegate, and sends a message to your controlling class as to what sprite has been selected and continue with desired functionality. No need for CGRectContainsPoint method.
This is cleaner, and more efficient.
Hope this helps!

Can we Add a new layer over CATiledLayer?

I am displaying a big image in CATiledLAyer.
Now i want to draw a line between two points where the user touches on that image.
Would that be possible ?? , if so can you outline me the way to accomplish it ??
Thanks,
Ratna
I did something similar for an app a while back. The strategy I used was to place another view on top of the view with the tiles. Then set your self up as a UIScrollViewDelegate and everytime the tiled view scrolls or zooms, recalculate where the overlayed objects need to be. Just read the contentOffset value and the zoomScale and you should have what you need to correctly figure out where your overlay needs to be positioned. You will also have to make sure that touches are correctly reaching the tile view if they have to pass through your overlay view.

Creating a bounding box for a View

Is there a way to a bounding box for a view so that its subviews cannot leave the view?
I currently have UIImageViews which I move around, scale, rotate etc and they are able to leave the view area.
How does one set the superview to bound/hold the subviews within it?
How else can this be done. I currently detect the origin of the image this works to the point that the image moves until it reaches this origin, when it does, the image is stuck.
I use gestureRecognizers and this origin technique only works for panning/moving an image.
Any suggestions?
If you want to stop a view's subviews from being drawn outside its borders, you need to set its clipsToBounds property to YES. (See the UIView class reference for details.)
If you want to stop your views from being in certain positions, don't move them there! It's your code that's putting them where you don't want them to be. If you're using a gesture recogniser, presumably you have a method that responds to gestures by adjusting the frame of a view - put some conditions on this movement that prevent it from happening when you don't want it.
When you write these conditions, bear in mind that a view's origin is relative to its superview. For example, say you have a view controller with a view that takes up the whole screen of the device, and inside that a box that starts at 0, 100, and inside that some squares and circles and squiggly shapes that the user can move around. If you examine containerBox.bounds.origin, you'll find that it's 0, 100, but if you want to put a square in the top left corner of the box, you need to get its frame and set the origin to 0, 0. Something to look out for.
I didn't follow your explanation of the 'origin technique'. If you paste your code, I might be able to help.

UIScrollView change pages after animation

Ok, I have a complicated scenario here. I have a scrollview which scrolls horizontally and contains tiles, 1 centered on the screen at a time with the user still able to see if there are more to the left or right by way of having the edges of the 2 views visible on either side. I am able to add the views programmatically to the scrollview and have paging work properly, so the user can swipe back and forth between them. Another requirement is to have an initial animation where the first view slides in, then is bounced off to the left by the second view. I accomplished this by using a series of UIView animations, and it works well.
Here is my problem: After the animation completes, you cannot scroll left to get to the first UIView that was created. I suspect that this is because it was animated out to the left of the content area of the scrollview. I have tried to increase the contentSize of the scrollview, but I still get the same behavior.Once the initial scrollview has been moved off to the left, I cannot swipe to page to it.
Is there a common pattern I could use to accomplish this in a better way?
It sounds to me like you're animating the child view's frame to the left so that the x coordinate of that first view's frame is negative, instead of animating the scroll view's contentOffset to the right. If that's the case, is there a reason you aren't just setting the scroll view's contentOffset inside an animation block? If there is a reason, what if after the animation completes you "fix up" the content offset and the frames of the child views so that none of the views are in a negative position.
But, I guess I have more questions than answers right now, so it might help to post the code showing how you do your animations to make it easier to answer your question.

iPhone SDK: disabling cllipping for UIScrollView

I'm working on an iPhone game, and trying to use a UIScrollView to display some icons, which I then want to enable the user to drag off the bar being scrolled, onto another view (basically, dragging them from their hand into play on the game board). The problem is that the UIScrollView clips everything outside it's own bounds.
Here is a picture of what I'm trying to do:
Functionally, it actually works, in that you can drag the icons up to the white board fine...but you can't see them as you are dragging...which is not acceptable.
Does anyone know if you can temporarily disable the clipping that a scroll view does, or is there some way to get around it? Hacky or not, I would really like to make it happen.
Does anyone have any other possible solutions for this? Or maybe any alternate approaches? I've considered if maybe a page view might work, but haven't tried it yet...and it's not at all as good of a solution as the scroll view.
Worst case I can just go back to not having the bar scrollable, but that really puts a damper on some of my game mechanics, and I'm really not too excited about that.
I think you're looking for the clipsToBounds property of UIView. I've used it successfully with UIScrollView:
scrollView.clipsToBounds = NO;
However, the dragging you want to do from the scroll view to the game view may require you to remove the icon view from the scroll view, place it in the superview at a position corresponding to its visible position within the scroll view (calculated using the scroll view's origin and content offset), and have that track the user's finger movements. On a release of the touch, either drop it on the game view at the proper position or return it to the scroll view.
I'm not 100% sure I understand the question but I think you should look into the z order of the scrollview and the whiteboard. It may be that the drag is just going behind the whiteboard.
Failing that, it would be useful to see all the bounds of your view heirarchy.
I also think a better solution allround might be to create a "sprite" to animate underneath the players finger - you could offset the drawpoint of the sprite from the touchlocation so that the player can see what they are dragging.