I am using a UIScrollView to forward touches to Cocos2D as outlined in http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/
Everything works great after a few days of working with it, except one thing: when the initial view appears on the screen, the background appears to be scrolled to the center. As soon as I try to scroll around, the image jumps to 0,0, and everything works as normal, except the touches are offset by half the width and height of the background image. Am I overlooking something basic? I can't think of a useful portion of the code that illustrates the issue, as I can't track it down, but would be happy to post code if anyone has any ideas.
Thanks in advance,
-Roberto
I just had to localize the touches in respect to the window, by using nil in locationInView. This returns the touch's location on what's currently visible in the display.
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:nil]];
Related
Let me first introduce you to the node hierarchy:
SKView->SKScene->SKNode
I've added a UIPinchGestureRecognizer to the view so that I can zoom in and out my content. Here's the code in SKScene object:
-(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer{
[self runAction:[SKAction scaleBy:pinchRecognizer.scale duration:0]];
pinchRecognizer.scale = 1;
}
Everything's OK except for two things:
In my node's touch events I check the position of the touch and act accordingly. Everything works fine as long as the scale of the scene is untouched. But if I zoom in or out I can no more do this. It's because even if I seem to touch the same point on the screen it's actually different before and after zooming. In Cocos2D I had exactly the same problem. But I could solve this problem by just converting the touch point to node space with convertToNodeSpace method. There should be an equivalent in Sprite Kit. What is it?
When pinching, anchor point is the lower left corner. Is there a way to make it zoom from where the pinching occurs?
I was working through a very simple Ray Wenderlich iOS "dragging" example, where 2 UIImageViews (a monkey and a banana) can be dragged around the screen. It utilizes a UIPanGestureRecognizer in the ViewController implementation file:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
That works fine, and the monkey and banana stay at whatever position you drag them to. However, I wanted to have whichever UIImageView is clicked (touched) to become to top most view. I fooled around a little and came up with this line of code:
[self.view bringSubviewToFront:recognizer.view];
I put that in the handlePan method, and it does bring whatever image clicked on (touched) to the front, but it resets the previous image to its original location on the screen. It doesn't do that if I omit the line of code which brings the touched subview to the front.
What is it about bringing the selected subview to the front that resets the other image to its original location? How would this be prevented in a UIPanGestureRecognizer? Without going the touchesBegan, touchesEnded, etc method route?
Obviously I'm new to ios programming, but I couldn't find an answer to what is probably a simple problem. Thanks guys...any help is appreciated...
I recreated your source here. The problem is the autolayout, you will need to disable it. Because when you bring the view to front, it will apply the rules to the other view to check the position, repositioning it in the original place.
So, why it not happens while you are changing the frames ? When you are only changing frames it does not apply the autolayout rules every time you change the center of one view, it will only apply when your view container hierarchy change or when the view container frame change.
Like you told you are doing a Ray Wenderlich tutorial, I will recomend you to check the tutorial about autolayout. The material in the Ray Wenderlich site is a good starting point.
--
Just a little help to disable the auto layout, at .xib and story boards, select the view and disable the checkbox:
I am implementing a scrollview based on the code of Scrolling Madness found on git and I update it for an infinite image scrolling like a magazzine using three uiimageviews:
- 1st uiimageview: last page
- 2nd uiimageview: first page
- 3rd uiimageview: second page
The code uses the scrollRectToVisible to center in the screen the second uiimageview but when I start zooming it with the fingers on the center, the image moves to the right, leaving a part on screen and the left size on blank. The zoom works fine and everything but the behavior of the image isn't the correct.
I checked the Scrolling Madness project and I replicated the same behavior. The second image moves to the right, and the third one goes off screen.
Any ideas of how to correct this? I have tried to modify the contentoffset and other options of the scrollview but no success.
I found another solution that worked for me after seeing another zoom project on the web. In the Scrolling Madness project, in the setZoomingMode function, set your scrollview's content size the same as your uiview that will be zoomed. Also, in my case, I modified the uiview's frame's origin to make it visible on the screen.
I found a very useful method that solved the problem on this page: http://idevzilla.com/2010/10/04/uiscrollview-and-zoom/
I'm trying to implement tap to focus and tap to exposure in my iPhone app.
I'm using the AVFoundation camera interface and I'm having trouble moving between the 2 co-ord systems.
UIView uses a system that goes from (0,0) in the top left (in portrait mode) to (320,480) in the bottom right.
However the AVCaptureSession uses a system that goes from (0,0) in the top right to (1,1) in the bottom left.
I've had a look at the AVCamDemo but it doesn't seem to do anything to convert these, one minute you have the UIView coords and the next you have the AV coords.
Any help is appreciated!
Thanks
Oliver
OK, I worked out what was going on. I'm writing the app at home though so can't update during the day :(
Anyway, the coord system for the touch is from (0,0) to (320,480) (top left to bottom right in portrait mode).
The coord system for the focal point of interest is (0,0) to (1,1) (top left to bottom right in landscape right mode).
In order to get the tap to focus you'll need an AVCaptureVideoPreviewLayer embedded into a subclass of UIView. (I'm assuming in this that the view is displayed across the entire screen).
To get from a tap to a focal point do the following.
Get the tap CGPoint (e.g. tapPoint).
Then use...
CGPoint focalPoint = CGPointMake(tapPoint.y/480, (320 - tapPoint.x)/320);
There may be a bit of code that does this for you but until then this will suffice.
I know this is an old question, but THERE WAS a function in AVCamDemo to do the conversion in all videoGravity cases (your code only work if videoGravity of the videoPreviewLayer is AVLayerVideoGravityResize)
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.