View goes out of a View Boundary - iphone

I have a Background view (UIImageView) in that i am adding a view(UIView).
this contentView(UIView) contains UIPanGestureRecognizer.
I am giving UIPanGestureRecognizer to contentView by bellow code :
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(movePhoto:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[contentView addGestureRecognizer:panRecognizer];
[panRecognizer release];
and then i am adding this contentView to Background View (UIImageView) by bellow code :
[imgBG addSubview:contentView]; // here imgBG is Background view.
but when i move this contentView it goes out of imgBG.
bellow is the method to move contentView
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
CGFloat finalX = [sender view].center.x;
CGFloat finalY = [sender view].center.y;
[[sender view] setCenter:CGPointMake(finalX, finalY)];
I think, here the contentView should remain inside the imgBG as it is the subview of imgBG.
Please let me know whether there is a problem in my code or its due to UIPanGestureRecognizer.
thanks in advance.

I just had to do :
[imgBG setClipsToBounds:YES];

Related

Choose different UIPanGestureRecognizer touches

Ok so I have a app with a left menu and a right view controller which are controlled by the UIPanGestureRecognizer. So when a user swipes the bezel (20px) of the edge of the screen it navigates.
In a settings view controller I have UISwitches which control various other things, but one of them I want to control how many touches the UIPanGestureRecognizer will take.
The switch is set up properly and sends the user defaults to the other view controller, but on the receiving end of it, it won't read what the user has selected.
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCallback:)];
//Switch for SwipeNav
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
}else{
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
}
[pan setDelegate:self];
[self.view addGestureRecognizer:pan];
How can I make it so when the switch is on, it uses 2 touches to navigate, and off it acts like normal?
Any assistance is always appreciated. Thank you.
UPDATED with settings sync code.
if (swipe){
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:self.swipe.on forKey:#"SwipeNav"];
[userDefaults synchronize];
if ([swipe isOn]) {
[swipe setOn:YES animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
} else {
[swipe setOn:NO animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
}
}
I created a new project and tried to reproduce the problem that you are seeing and everything seems to work as expected. When switch is on the you need two buttons to pan and when switch is off you only need one. Let me know if that points you in the right direction. Your code seems to be correct.
I copy pasted the handlePan function from How to use UIPanGestureRecognizer to move object? iPhone/iPad
Created everything programmatically and copy pasted your code to save the state of the switch. Here is the code from two UIViewControllers
//
// ViewController.m
// StackOverflowSwitch
//
//
//
//
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic,strong) UIButton *myButton;
#property (nonatomic,strong) UISwitch *switchOn;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, 150, 150)];
self.switchOn = [[UISwitch alloc]initWithFrame:CGRectMake(10, 300, 100, 100)];
self.myButton.backgroundColor =[UIColor grayColor];
[self.myButton setTitle:#"Click Me" forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.switchOn];
[self.view addSubview:self.myButton];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)click:(UIButton *)sender
{
[self performSegueWithIdentifier:#"NextViewController" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if(self.switchOn.on){
NSLog(#"switch is on");
}else{
NSLog(#"switch is off");
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:self.switchOn.on forKey:#"SwipeNav"];
[userDefaults synchronize];
}
#end
//////
#import "SecondViewController.h"
#interface SecondViewController() <UIGestureRecognizerDelegate>
#property (strong,nonatomic) UIView *myView;
#end
#implementation SecondViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.myView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
}else{
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
}
[pan setDelegate:self];
[self.view addGestureRecognizer:pan];
[self.myView addGestureRecognizer:pan];
}
-(void)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];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(#"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
#end
///// Update
I am not exactly sure how [[UIApplication sharedApplication] reloadInputViews]; calls the function to update the UIPanGestureRecongizer in your code but i tried to recreate your functionality as close as possible by having the setupView method called every time i click the UISwitch
I created a custom view with UIPanGestureReconginzer in there and a method that I call from the UIViewController to update setNumberOfTouches. The view works as expected. The key here is not to initialize a new UIPanGuestureRecongizer but update the existing one. I assume in your app UIViewController or UIView which has the UIPanGuestureRecongizer is not being fully reloaded.
I created a property UIPanGuestureRecongizer which gets initialized the first time it's being accessed and then gets updated when switch is being pressed. Here is the custom UIView code. Let me know if you have any questions regarding the code.
#import "MyTestView.h"
#interface MyTestView() <UIGestureRecognizerDelegate>
#property(nonatomic,strong) UIPanGestureRecognizer *panGesture;
#end
#implementation MyTestView
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setupView];
}
return self;
}
-(UIPanGestureRecognizer *)panGesture
{
//lazy instantiation
if (!_panGesture) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
}
return _panGesture;
}
-(void)setupView
{
//this gets called everytime the UISwitch is being pressed.
NSLog(#"Setup views");
self.backgroundColor = [UIColor redColor];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[self.panGesture setMinimumNumberOfTouches:2];
[self.panGesture setMaximumNumberOfTouches:2];
}else{
[self.panGesture setMinimumNumberOfTouches:1];
[self.panGesture setMaximumNumberOfTouches:1];
}
[self.panGesture setDelegate:self];
[self addGestureRecognizer:self.panGesture];
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(#"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
#end
Alright, so after much testing/code flipping, I think I have found my solution.
So a little more backstory, I use MMDrawerController, so the method above about placing [self setupView]; in (instancetype)initWithCoder didn't do anything. The setupView or in my case is called "setupGestureRecognizers" is loaded in the viewDidLoad. Anywhere else it wouldn't load the gesture's at all.
With my current switch code and gesture loading I have this:
-(UIPanGestureRecognizer *)panGestureNav {
if (!_panGesture) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCallback:)];
}
return _panGesture;
}
-(void)setupGestureRecognizers{
switchOn = [[NSUserDefaults standardUserDefaults] boolForKey:#"SwipeNav"];
if (switchOn == YES) {
// 2 Finger SwipeNav
[self.panGestureNav setMinimumNumberOfTouches:2];
[self.panGestureNav setMaximumNumberOfTouches:2];
NSLog(#"2 Finger Nav");
}else {
[self.panGestureNav setMinimumNumberOfTouches:1];
[self.panGestureNav setMaximumNumberOfTouches:1];
NSLog(#"Normal Nav");
}
[self.panGestureNav setDelegate:self];
[self.view addGestureRecognizer:self.panGestureNav];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureCallback:)];
[tap setDelegate:self];
[self.view addGestureRecognizer:tap];
}
Which would normally work, but this library does things differently, so I have to look into their gesture calling and what I ended up doing was calling it here:
-(MMOpenDrawerGestureMode)possibleOpenGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
CGPoint point = [touch locationInView:self.childControllerContainerView];
MMOpenDrawerGestureMode possibleOpenGestureModes = MMOpenDrawerGestureModeNone;
if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
[self setupGestureRecognizers]; <------------
So now it loads and functions as I want.
Thank you Yan so all of your assistance. Hope this helps somebody.

Move UIView to another UIView

I have an UIViewController which contains 2 UIScrollViews, scrollView1, scrollView2 for instance.
scrollView1 contains many UIViews, when tapping one of it's UIViews I want it to move into scrollView2
When tapping a UIView that belongs to scrollView1, a method inside the UIViewController is being called and the view is passed as a parameter.
In that method you should write something like:
[view removeFromSuperview];
[scrollView2 addSubview:view];
Edit:
For animated move you should try something like:
CGPoint originalCenter = [self.view convertPoint:view.center fromView:scrollView1];
[view removeFromSuperView];
[self.view addSubview:view];
view.center = originalCenter;
CGPoint destinationPointInSecondScrollView = ; // Set it's value
CGPoint finalCenter = [self.view convertPoint:destinationPointInSecondScrollView fromView:scrollView2];
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
view.center = finalCenter;
} completion:^(BOOL finished) {
[view removeFromSuperView];
[scrollView2 addSubview:view];
view.center = destinationPointInSecondScrollView;
}];
Supposing you have these two scrollViews declared as properties:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)]
for (UIView *view in self.scrollView1.subviews) {
[view addGestureRecognizer:gesture];
}
}
- (void)viewTapped:(UITapGestureRecognizer *)gesture
{
UIView *view = gesture.view;
[self moveToScrollView2:view];
}
- (void)moveToScrollView2:(UIView *)view
{
[view removeFromSuperview];
[self.scrollView2 addSubview:view];
}

how to check multiple CGRectIntersectsRect

based on how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?
i got around of checking CGRectIntersectsRect single uiimageview. how will i need to check multiple uiimageview?
CORRECT
WRONG
-(void)initImagesAndGesture
{
UIImage *img = [UIImage imageNamed:#"beer.png"];
imgView1 = [[UIImageView alloc]initWithImage:img];
[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
[imgView1 setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView1 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView1];
img = [UIImage imageNamed:#"cups.png"];
imgView2 = [[UIImageView alloc]initWithImage:img];
[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
[imgView2 setUserInteractionEnabled:YES];
recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView2 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView2];
}
-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
if (!(CGRectIntersectsRect(imgView1.frame, imgView2.frame)))
{
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
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:recognizer.view];
}
else
{
NSLog(#"intersect!");
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
recognizer.view.center = CGPointMake(pre_moveLocation.x, pre_moveLocation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
//[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
//[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
}
}
-(void)addImgView:(UIPanGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[imgView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView addGestureRecognizer:recognizer1];
[self.view addSubview:imgView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initImagesAndGesture];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
Just iterate through all subviews...
// targetView is the one you are checking...
- (BOOL)isViewIntersectingAnyOtherViews:(UIView*)targetView {
for (UIView *view in self.view.subviews) {
if (view == targetView) continue; // Don't check against your own view
if (CGRectIntersectsRect(view, thisView)) {
return YES;
}
}
return NO;
}

how to add UIPanGestureRecognizer to UIImageView

-(void)initialization
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)] autorelease];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];
}
- (void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = imageview;
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
I add the gesture to the imageview its not calling move action.
how to add the gesture to only UIImageView ..
Try setting imageview.userInteractionEnabled = YES. UIImageViews has userInteractionEnabled set to NO by default.

Draw Line with gesture

I add a view as sub view using the following code
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:cppobject->OutputImage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
[holderView addSubview:imageview];
holderView.contentMode = UIViewContentModeScaleAspectFit ;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[panRecognizer release];
-(void)scale:(id)sender {
}
-(void)rotate:(id)sender {
}
-(void)move:(id)sender {
}
-(void)tapped:(id)sender {
}
I need to draw line when the user use his two fingers to pan and a uilabel (the green one) like in the following image
I had look on How to draw graphics in iphone gesture?
But I couldn't apply it on the subview , specially the DrawInRect method
in panRecognizer function (move) I want to draw line using
UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ;
NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]];
[Points addObject:value];
[holderView setNeedsDisplay];
NSLog(#"End of measuring") ;
and I will use the points in Points to draw line above all the subviews in
-(void)drawRect:(CGRect)rect {
NSLog(#"Entered Draw In Rect");
if (Measuring) {
[[UIColor redColor] setStroke];
UIBezierPath *pathToDraw = [UIBezierPath bezierPath];
for (int n = 1; n < [Points count] - 1 ; n++) {
NSValue * value = [Points objectAtIndex:(NSInteger)n];
CGPoint point = [value CGPointValue];
[pathToDraw moveToPoint:point];
value = [Points objectAtIndex:(NSInteger)n+1];
point = [value CGPointValue];
[pathToDraw addLineToPoint:point];
}
[pathToDraw stroke];
}
}
the problem is [holderView setNeedsDisplay]; never call or fire drawRect any suggestion or help regarding that
any suggestion
The image view being the subview draws on top of the holder view. The imageview is opaque. This means that when the views are drawn there is no part of the holder view that is actually visible, so it's drawRect call is optimised out.
Try ordering the views the other way around, so that the holder view is the subview of the imageview. Then the imageview will draw and the holder view will draw on top of it.
Also, note that you should use the bounds of the parent view as the frame of the subview.
UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];
Edit (add):
See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1
, specifically under the section "View Hierarchies and Subview Management":
"Visually, the content of a subview obscures all or part of the content of its parent view"
So, try make the imageview the parent, do your initialisation like so:
// instance variables:
UIImageView* imageView;
MyHolderView* holderView;
imageView = [[UIImageView alloc] initWithFrame:mainRect];
holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]];
holderView.opaque = NO;
holderView.backgroundColor = [UIColor clearColor];
[imageView addSubview:holderView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
// etc...
Now, the image view is drawn, and the holder view, its subview is drawn on top of it. Now when you call setNeedsDisplay on the holderview it will receive a drawRect: call.
For example, track the gesture like so. This could be in your view controller or in your MyHolderView view subclass; the example here would be in the MyHolderView class so that the location1 and location2 instance variables can be shared easily with the drawRect: method.:
-(void)scale:(id)sender {
if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required
// get position of touches, for example:
NSUInteger num_touches = [pinchRecognizer numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [pinchRecognizer locationOfTouch:0 inView:holderView];
}
if (num_touches >= 2) {
location2 = [pinchRecognizer locationOfTouch:1 inView:holderView];
}
// tell the view to redraw.
[holderView setNeedsDisplay];
}
}
and then in the holder view drawRect routine:
-(void)drawRect:(CGRect)rect {
// use instance variables location1 and location2 to draw the line.
}