当前位置:  开发笔记 > 后端 > 正文

使用CPBPressureTouchGestureRecognizer检测压力强度

如何解决《使用CPBPressureTouchGestureRecognizer检测压力强度》经验,为你挑选了1个好方法。

它与一个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;
}

我试图使用更多,但只有第二个工作:

- (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;
}

我怎么能让两者都起作用?



1> picciano..:

CPBPressureTouchGestureRecognizer仅支持UIAccelerometer的委托模型的单个实例.这是一个支持多个实例的更好版本.

//  CPBPressureTouchGestureRecognizer.h
//  PressureSensitiveButton

#import 

#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 minimumPressureRequired;
    float maximumPressureRequired;

    @private
    float pressureValues[50];
    uint currentPressureValueIndex;
    uint setNextPressureValue;
}

@property (readonly, assign) float pressure;
@property (readwrite, assign) float minimumPressureRequired;
@property (readwrite, assign) float maximumPressureRequired;

@end

//  CPBPressureTouchGestureRecognizer.m

#import 
#import "CPBPressureTouchGestureRecognizer.h"
#import "CPBAcceleromterDelegate.h"

#define kUpdateFrequency            60.0f
#define KNumberOfPressureSamples    3

@interface CPBPressureTouchGestureRecognizer (private)
    - (void)setup;
@end

@implementation CPBPressureTouchGestureRecognizer
@synthesize pressure, minimumPressureRequired, maximumPressureRequired;

- (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 {
    minimumPressureRequired = CPBPressureNone;
    maximumPressureRequired = CPBPressureInfinite;
    pressure = CPBPressureNone;

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.001f / kUpdateFrequency];
    [[UIAccelerometer sharedAccelerometer] setDelegate:[CPBAcceleromterDelegate sharedCPBAcceleromterDelegate]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accelerateNotification:) name:CPB_ACCELERATION_EVENT object:nil];
}

#pragma -
#pragma UIAccelerometerDelegate methods

- (void)accelerateNotification:(NSNotification *)notification {

    UIAcceleration *acceleration = [[CPBAcceleromterDelegate sharedCPBAcceleromterDelegate] 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= minimumPressureRequired && pressure <= maximumPressureRequired)
                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 
#define CPB_ACCELERATION_EVENT @"accellerationEvent"

@interface CPBAcceleromterDelegate : NSObject 

@property (nonatomic, retain) UIAcceleration *acceleration;

+ (CPBAcceleromterDelegate *)sharedCPBAcceleromterDelegate;

-(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 redistribution (in part or whole) of source code must retain
//  this copyright and permission notice. Attribution in compiled projects is
//  appreciated but not required.
//

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
\
- (void)release \
{ \
} \
\
- (id)autorelease \
{ \
return self; \
}

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有