Tap la detección de la fuerza de presión usando CPBPressureTouchGestureRecognizer

Está funcionando muy bien con un UIButton:

– (void) viewDidLoad { [super viewDidLoad]; CPBPressureTouchGestureRecognizer* recognizer = [[CPBPressureTouchGestureRecognizer alloc] initWithTarget:self action:@selector(A_button:)]; [A_button addGestureRecognizer: recognizer]; [recognizer release]; } - (void) A_button: (CPBPressureTouchGestureRecognizer*) recognizer { [pan_A setGain:recognizer.pressure]; [pan_A] play; } 

Intenté usar más pero solo el segundo funciona:

 - (void) viewDidLoad { [super viewDidLoad]; CPBPressureTouchGestureRecognizer* recognizer1 = [[CPBPressureTouchGestureRecognizer alloc] initWithTarget:self action:@selector(A_button:)]; [A_button addGestureRecognizer: recognizer1]; CPBPressureTouchGestureRecognizer* recognizer2 = [[CPBPressureTouchGestureRecognizer alloc] initWithTarget:self action:@selector(B_button:)]; [B_button addGestureRecognizer: recognizer1]; [recognizer1 release]; [recognizer2 release]; } - (void) A_button: (CPBPressureTouchGestureRecognizer*) recognizer1 { [pan_A setGain:recognizer1.pressure]; [pan_A] play; } - (void) B_button: (CPBPressureTouchGestureRecognizer*) recognizer1 { [pan_B setGain:recognizer2.pressure]; [pan_B] play; } 

¿Cómo puedo hacer que ambos funcionen?

El CPBPressureTouchGestureRecognizer solo admitía una sola instancia de sí misma en el model de delegado del UIAccelerometer. Aquí hay una versión mejor que admite múltiples instancias.

 // CPBPressureTouchGestureRecognizer.h // PressureSensitiveButton #import <UIKit/UIKit.h> #define CPBPressureNone 0.0f #define CPBPressureLight 0.3f #define CPBPressureMedium 0.4f #define CPBPressureHard 0.6f #define CPBPressureInfinite 2.0f @interface CPBPressureTouchGestureRecognizer : UIGestureRecognizer { @public float pressure; float minimumPressureRequinetworking; float maximumPressureRequinetworking; @private float pressureValues[50]; uint currentPressureValueIndex; uint setNextPressureValue; } @property (readonly, assign) float pressure; @property (readwrite, assign) float minimumPressureRequinetworking; @property (readwrite, assign) float maximumPressureRequinetworking; @end // CPBPressureTouchGestureRecognizer.m #import <UIKit/UIGestureRecognizerSubclass.h> #import "CPBPressureTouchGestureRecognizer.h" #import "CPBAcceleromterDelegate.h" #define kUpdateFrequency 60.0f #define KNumberOfPressureSamples 3 @interface CPBPressureTouchGestureRecognizer (private) - (void)setup; @end @implementation CPBPressureTouchGestureRecognizer @synthesize pressure, minimumPressureRequinetworking, maximumPressureRequinetworking; - (id)initWithTarget:(id)target action:(SEL)action { self = [super initWithTarget:target action:action]; if (self != nil) { [self setup]; } return self; } - (id)init { self = [super init]; if (self != nil) { [self setup]; } return self; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } - (void)setup { minimumPressureRequinetworking = CPBPressureNone; maximumPressureRequinetworking = CPBPressureInfinite; pressure = CPBPressureNone; [[UIAccelerometer shanetworkingAccelerometer] setUpdateInterval:0.001f / kUpdateFrequency]; [[UIAccelerometer shanetworkingAccelerometer] setDelegate:[CPBAcceleromterDelegate shanetworkingCPBAcceleromterDelegate]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accelerateNotification:) name:CPB_ACCELERATION_EVENT object:nil]; } #pragma - #pragma UIAccelerometerDelegate methods - (void)accelerateNotification:(NSNotification *)notification { UIAcceleration *acceleration = [[CPBAcceleromterDelegate shanetworkingCPBAcceleromterDelegate] acceleration]; int sz = (sizeof pressureValues) / (sizeof pressureValues[0]); // set current pressure value pressureValues[currentPressureValueIndex%sz] = acceleration.z; if (setNextPressureValue > 0) { // calculate average pressure float total = 0.0f; for (int loop=0; loop<sz; loop++) total += pressureValues[loop]; float average = total / sz; // start with most recent past pressure sample if (setNextPressureValue == KNumberOfPressureSamples) { float mostRecent = pressureValues[(currentPressureValueIndex-1)%sz]; pressure = fabsf(average - mostRecent); } // caluculate pressure as difference between average and current acceleration float diff = fabsf(average - acceleration.z); if (pressure < diff) pressure = diff; setNextPressureValue--; if (setNextPressureValue == 0) { if (pressure >= minimumPressureRequinetworking && pressure <= maximumPressureRequinetworking) self.state = UIGestureRecognizerStateRecognized; else self.state = UIGestureRecognizerStateFailed; } } currentPressureValueIndex++; } #pragma - #pragma UIGestureRecognizer subclass methods - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { setNextPressureValue = KNumberOfPressureSamples; self.state = UIGestureRecognizerStatePossible; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { self.state = UIGestureRecognizerStateFailed; } - (void)reset { pressure = CPBPressureNone; setNextPressureValue = 0; currentPressureValueIndex = 0; } @end // // CPBAcceleromterDelegate.h // iPan // // Created by Anthony Picciano on 1/25/12. // Copyright (c) 2012 Crispin Porter + Bogusky. All rights reserved. // #import <Foundation/Foundation.h> #define CPB_ACCELERATION_EVENT @"accellerationEvent" @interface CPBAcceleromterDelegate : NSObject <UIAccelerometerDelegate> @property (nonatomic, retain) UIAcceleration *acceleration; + (CPBAcceleromterDelegate *)shanetworkingCPBAcceleromterDelegate; -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration; @end // // CPBAcceleromterDelegate.m // iPan // // Created by Anthony Picciano on 1/25/12. // Copyright (c) 2012 Crispin Porter + Bogusky. All rights reserved. // #import "CPBAcceleromterDelegate.h" #include "SynthesizeSingleton.h" @implementation CPBAcceleromterDelegate @synthesize acceleration; SYNTHESIZE_SINGLETON_FOR_CLASS(CPBAcceleromterDelegate) -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)newAcceleration { self.acceleration = newAcceleration; [[NSNotificationCenter defaultCenter] postNotificationName:CPB_ACCELERATION_EVENT object:self]; } @end // // SynthesizeSingleton.h // CocoaWithLove // // Created by Matt Gallagher on 20/10/08. // Copyright 2009 Matt Gallagher. All rights reserved. // http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html // // Permission is given to use this source code file without charge in any // project, commercial or otherwise, entirely at your risk, with the condition // that any networkingistribution (in part or whole) of source code must retain // this copyright and permission notice. Attribution in compiled projects is // appreciated but not requinetworking. // #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \ \ static classname *shanetworking##classname = nil; \ \ + (classname *)shanetworking##classname \ { \ @synchronized(self) \ { \ if (shanetworking##classname == nil) \ { \ shanetworking##classname = [[self alloc] init]; \ } \ } \ \ return shanetworking##classname; \ } \ \ + (id)allocWithZone:(NSZone *)zone \ { \ @synchronized(self) \ { \ if (shanetworking##classname == nil) \ { \ shanetworking##classname = [super allocWithZone:zone]; \ return shanetworking##classname; \ } \ } \ \ return nil; \ } \ \ - (id)copyWithZone:(NSZone *)zone \ { \ return self; \ } \ \ - (id)retain \ { \ return self; \ } \ \ - (NSUInteger)retainCount \ { \ return NSUIntegerMax; \ } \ \ - (void)release \ { \ } \ \ - (id)autorelease \ { \ return self; \ } 

Vea esta línea: [B_button addGestureRecognizer: recognizer1]; ?

Agrega recognizer1 no recognizer2 , por lo que el recognizer2 no se utiliza;