Can't figure out iPad landscape view coordinates - xcode

Being new to objective-C but tinkering I was trying out dragging with the iPad, and the following code from my viewcontroller.m works okay in portrait, but not in landscape:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:mainView.superview];
testViewToMove.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
mainView being the default view that was created with the app, and testViewToMove being a UIView I made for kicks.
In landscape, the coordinates don't seem to translate. I'm sure this is something obvious, but I don't get it, what's going on here?
I tried setting the view to landscape, and tinkering with some other settings to no avail... I also saw this, but to my own dismay I haven't been able to get it to work.

Seems I just needed to change mainView.superview to self.view in the first method, after applying the fix linked above.

Related

Cocos2d and UISCrollview pass through touches

I have made a CCNode which I dubbed ScrollNode, basically it is a CCNode which crops a certain region of a child node, and this child node is moving on it's x and y position based on the scroll location of a transparent overlaying UIScrollView.
I wanted a ScrollView in Cocos2d which felt native to the iOS platform and it does a good job.
However a problem arises with this way of handeling things. The touches for CCButton on a node which can be scrolled are not called (because there is a transparent UIScrollView over that region) is it possible to pass through the touches from the UIScrollView when it is a "tap" gesture and not a "pan gesture"
I have tried the following:
// Make sure touches are passed trough all views
for(UIGestureRecognizer *recognizer in self.scrollView.gestureRecognizers)
{
recognizer.cancelsTouchesInView = NO;
}
But this doesn't seem to make any difference.
Kind Regards,
The way I prefer to solve this is to set up my own UITapGestureRecognizer on the UIScrollView instance. The handler then just passes the touch location to the code that handles taps in my scene.
The tap and pan gesture recognizers know how to interact with each other so it still provides the native experience.
Seems like I've fixed it. I have subclassed the UIScrollView, and passed the touches* events to it's parent. Which seems to have no effect on the scrolling, but makes sure it does trigger the tap event for CCButtons.
#import "ScrollNodeScrollView.h"
#implementation ScrollNodeScrollView
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesCancelled:touches withEvent:event];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)aTouches withEvent:(UIEvent *)anEvent
{
[self.superview touchesEnded:aTouches withEvent:anEvent];
}
#end

How can I move multiple UIImage views around a view controller?

How can I move multiple UIImage views around a view controller?
I have managed to use this code;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
printf("touch began --------- |n");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
printf("touch moved --------- |n");
UITouch *myTouch = [touches anyObject];
startPoint = [myTouch locationInView:self.view];
ball.center = CGPointMake(startPoint.x, startPoint.y);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
printf("touch end --------- |n");
}
This code above has made it so that I can move one UIImage view around with touch, however, I want to be able to move 100's around. This code also, currently, when you move your finger around an area of the screen the image jumps to your finger.
Is this the best way to do it? Or is a pan gesture better?
Please help me make it so I can move multiple images around my view controller with touch and if you use the code above, please stop the image from jumping!
Please help!
.H FILE FOR ANSWER REFERRAL;
#interface CMViewController : UIViewController {
CGPoint startPoint;
}
#property CGPoint startPoint;
#property (strong, nonatomic) IBOutlet UIImageView *smyImageView;
#property (strong, nonatomic) IBOutlet UIImageView *smyImageView1;
#end
Thanks
You can use below. I used this for multiple images moving on screen. It's working for me.
UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(onsPan:)];
[smyImageView addGestureRecognizer:span];
UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(onsPan1:)];
[smyImageView1 addGestureRecognizer:span1];
Moving (Pan):
- (void)onsPan:(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];
}
- (void)onsPan1:(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];
}
This will teach you everything you need to know for what you want to do.
http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites
This is another answer that may help you

Dragging two images using multi touch

I am fairly new to touch event and i have a problem. I use the code above to drag two images on screen. The code works however when the the second finger touches the screen the first movement stops. So, the problem is related to multi-touch. I also, do not know how to calculate the second touches co-ordinates. I enabled multitouch in the view and in both images. I would be great full if somebody could help me move each image with each finger.
Thanks in advance!
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image) {
image.center = location;
} else if ([touch view] == image2) {
image2.center = location;
}}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];}
I would recommend using a custom UIGestureRecognizer for that. It will give you an nice encapsulated way to manage what you want. Dragging two images at the same time is in essence a gesture.

How to change image within a customised UIImageView class

I'm new to Xcode. This is probably a very simple question to you, but I wonder if anyone can help me.
What I want to do is to load an image in a customised UIImageView, when clicking the image, the imageview will change to another image. When I run the simulator, there are no error messages, but the second image does not load after clicking.
I've declared a UIImageView subclass dragView. In the dragView.m, I have such codes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self changePicture];
}
-(void) changePicture {
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"Red Flower.png"];
self.image = [UIImage imageWithContentsOfFile:path];
}
By the way, I have enabled the interaction. Many thanks!
You probably need to tell the OS to update the view by calling [self setNeedsDisplay:YES].

simple way to recognize/identify a gesture

Hi I implemented this function and i can hand the gestures but how can i recognize what gesture is which?for example simple move to left or right?
my code for handling:
/*this function is made to handel finger gesture and flip the view to other account*/
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
FirstViewController *screen = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
screen.myArray = myArray;
[self presentModalViewController:screen animated:YES];
[screen release];
}
Thanks for any answer
Well, it very much depends on what gesture you want to trap. If it is a simple pinch, swipe tap etc then you should probably use one of Apple new (in 3.2) convenience classes described in this document.
Using these, trapping a gesture is as simple as adding something like the following to your code:
UITapGestureRecognizer *doubleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleDoubleTap:)];
doubleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleFingerDTap];
and then implementing the method to process the gesture when it is found:
- (void)handleDoubleDoubleTap:(UIGestureRecognizer *)sender {
//Do something here
}
This will trap a double tap.

Resources