location different CGPoint - objective-c

Hi
how do I locate both the cgpoint? he gives me just one.
-(void)gestureLoad {
//GESTURE
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap2:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
}
- (void)numTap2:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
...other actions...
}
thanks a lot!

From the description of -[UIGestureRecognizer numberOfTouches]:
Using the value returned by this
method in a loop, you can ask for the
location of individual touches using
the locationOfTouch:inView: method.
So, call -locationOfTouche:inView: for each touch to get the corresponding location.

Related

Gesture recognizer on multiple UIImageView

I have 10 UIImageViews in the same ViewController, and each one of these images need to be controlled with a Gesture Recognizer; this is my simple code:
- (void)viewDidLoad {
UIImageView *image1 = // image init
UIImageView *image2 = // image init
...
UIRotationGestureRecognizer *rotationGesture1 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
UIRotationGestureRecognizer *rotationGesture2 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
...
...
UIRotationGestureRecognizer *rotationGesture10 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
[image1 addGestureRecognizer:rotationGesture1];
[image2 addGestureRecognizer:rotationGesture2];
...
...
[image10 addGestureRecognizer:rotationGesture10];
}
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
[gestureRecognizer setRotation:0];
}
}
Ok, all right, each image rotates, but I need to write similar code also for UIPanGestureRecognizer and UIPinchGestureRecognizer, obv for each UIImageView: is this the correct way, or there is a simpler method to avoid "redundant" code like this? Thanks!
Here's a possible solution. Make a method like so:
- (void)addRotationGestureForImage:(UIImageView *)image
{
UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
gesture.delegate = self;
[image addGestureRecognizer:gesture];
}
Then in your viewDidLoad method create an array of image views and loop through them calling this method like so:
NSArray *imageViewArray = [NSArray arrayWithObjects:image1,image2,image3,nil];
for(UIImageView *img in imageViewArray) {
[self addRotationGestureForImage:img];
}

UILongPressGestureRecognizer not works with tableview

I had added a UILongPressGestureRecognizer to a tableview in my ViewDidLoad method. I added this to detect long press on table view in my code. But it never works. In ViewDidLoad I added this code :
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.resultTableView addGestureRecognizer:lpgr];
[lpgr release];
I also added this method :
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.resultTableView];
NSIndexPath *indexPath = [self.resultTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(#"long press on table view but not on a row");
}
else {
NSLog(#"long press on table view at row %d", indexPath.row);
}
}
Please help me to resolve this?
Your code is working. I think you have to add UIGestureRecognizerDelegate delegate in .h file or how to declare resultTableView i mean you define programmatically or using .xib file.Check it once.
I have tried like this.
resultTableView = [[UITableView alloc] init];
resultTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStylePlain];
resultTableView.rowHeight = 100.0;
resultTableView.delegate=self;
resultTableView.dataSource=self;
[self.view addSubview:resultTableView];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[resultTableView addGestureRecognizer:lpgr];
[lpgr release];
It looks like you want to add the gesture to the individual cells, but you are adding the gesture to the table. Try adding the gesture to your UITableViewCell instead.
If the gesture recognizer is being blocked by the UITableView panGestureRecognizer, implement the delegate to ensure both can work
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

UITapGestureRecognizer on MKMapView breaks MKAnnotation selection

I've added a UITapGestureRecognizer to an MKMapView, like so:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doStuff:)];
[tapGesture setCancelsTouchesInView:NO];
[tapGesture setDelaysTouchesEnded:NO];
[[self myMap] addGestureRecognizer:tapGesture];
[tapGesture release];
This almost works: tap gestures are recognized and double taps still zoom the map. Unfortunately, the UITapGestureRecognizer interferes with the selection and deselection of MKAnnotationView elements, which are also triggered by tap gestures.
Setting the setCancelsTouchesInView and setDelaysTouchesEnded properties doesn't have any effect. Annotation selection works fine if I don't add the UIGestureRecognizer.
What am I missing?
UPDATE:
As suggested by Anna Karenina below, this problem can be avoided by returning YES in the shouldRecognizeSimultaneouslyWithGestureRecognizer: delegate method.
More details in this answer.
Instead of tap gesture, add long press gesture as below :-
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(longpressToGetLocation:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D location =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
}

How do I prevent a UIImage from being Moved outside of its UIImageView?

I'm trying to create a photo based app and I want to be able to crop, rotate and move the image thats been taken. I've done those things already but the UIImage created moves around the whole UIViewController and I only want it to be moved around within its UIImageView. Could anyone suggest how I might go about trying this? I've tried a few way already but they don't seemed to have worked. Any suggestions would be really helpful. Thanks!
Update
I'm adding in my code to help better explain what I'm trying to do . I'm sorry for not doing this before.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//the 'image' below is the UIImage, it has already been declared before hand
image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//layoutOne is my UIView which holds my UIImageView and UIImage. all of those are declared in my .h file.
layoutOne = [[UIView alloc] initWithFrame:CGRectMake(95.0, 107.0, 578, 682)];
theImageView = [[UIImageView alloc] initWithFrame:[layoutOne frame]];
[theImageView setImage:image];
[layoutOne addSubview:theImageView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:tapRecognizer];
[self.view addSubview:layoutOne];
}
I'm using Interface Builder to create my app and I'm also using the newest version of Xcode. Thought I'd also add this incase it helped. Thanks
I don't think you can move the image within the image view. What you can to is to create a UIView as the frame and move the image within the bounds of the frame. Below is the code snippet (iOS 5 with ARC) that I hope it helps.
#implementation ImageFrame
{
UIImageView *imageView;
CGPoint previousPoint;
}
- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor greenColor];
self.clipsToBounds = YES;
imageView = [[UIImageView alloc] initWithImage:image];
}
return self;
}
- (void)layoutSubviews
{
[self addSubview:imageView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
previousPoint = [[touches anyObject] locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint movePoint = CGPointMake(location.x - previousPoint.x, location.y - previousPoint.y);
CGPoint newCenter = CGPointMake(imageView.center.x + movePoint.x, imageView.center.y + movePoint.y);
imageView.center = newCenter;
previousPoint = location;
}
#end
Not sure what your current setup is, but make sure your UIImageView clipsToBounds property is set to YES.
If this doesn't answer your question, try checking the answers to Cropping an UIImage, which sounds like it may be describing a similar problem.

locationOfTouch and numberOfTouches

hi
I have this recognizer, set with 2 touch, but it returns only one, not two CGPoint
-(void)gestureLoad {
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap2:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
}
- (void)numTap2:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
NSLog(#"x %f y %f",location.x, location.y);
}
as I understand, I cycle the number of touch with these two methods, but I have not figured out how to:
-(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view {
}
-(NSUInteger)numberOfTouches {
}
thanks a lot!
In numTap2, use:
CGPoint location = [recognizer locationOfTouch:touchIndex inView:self.view];
where touchIndex is either 0 or 1.