Reputation: 51
Using xCode 4.3.2, I have the following code in initWithFrame in my view.m:
UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget: self action: @selector(pinch:)
];
oldScale = recognizer.scale;
[self pinch: recognizer];
[self addGestureRecognizer: recognizer];
// code to create label is also in here, works. label displays no problem
[self addSubview: label];
my pinch method is as follows:
- (void) pinch: (UIPinchGestureRecognizer *) recognizer
{
NSLog(@"pinch:");
label.bounds = CGRectMake(
(self.bounds.size.width - width * recognizer.scale) / 2,
(self.bounds.size.height - height * recognizer.scale) / 2,
width * recognizer.scale,
height * recognizer.scale
);
label.font = [UIFont systemFontOfSize: 20 * recognizer.scale];
NSString *verdict;
if (recognizer.scale > oldScale) {
verdict = @"spread";
} else if (recognizer.scale < oldScale) {
verdict = @"pinch";
} else {
verdict = @"neither";
}
oldScale = recognizer.scale;
label.text = [NSString stringWithFormat: @"%@ %g",
verdict, recognizer.scale
];
}
because i actually call pinch in my initWithFrame method, it runs once, but when i perform a pinch in the iphone simulator, it doesn't register at all. is there some setting in xcode 4.3.2 i don't know about? this code works everywhere else i've tried running it - but those versions of xcode are all 4.3.
Upvotes: 0
Views: 1296
Reputation: 7461
Add UIGestureRecognizerDelegate
in .h file
Use following code in .m file in view did load...
// Gesture Reconizer Delegate Methods
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(checkpinch)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.ScrollView addGestureRecognizer:pinch];
[pinch release];
// Gesture Reconizer Methods
-(void)checkpinch {
NSLog(@"YES");
}
>>See this Edited..
Pinch can detect on scroll-view if you want to detect on view than see following reference...
Hope, this will help you...
Upvotes: 1
Reputation: 8741
Maybe I am oversimplifying this, but wouldn't be easier to: 1) In your VC, create an IBoutlet property to an instance of your view 2) connect that IBOutlet to your view 3) setup your UIPinchGetsureRecognizer in the VC by overriding the view setter method and change target to the view and keep the pinch method in the view. This way the UIPinchGetsureRecognizer will load every time the view is loaded and be ready for a pinch. Then you would not need to call the pinch method (which defeats the purpose). KB
Upvotes: 0