【问题标题】:Button touchDown event and CPBPressureTouchGestureRecognizer按钮 touchDown 事件和 CPBPressureTouchGestureRecognizer
【发布时间】:2012-02-22 21:45:29
【问题描述】:

我的代码:

-(void)viewDidLoad {
[super viewDidLoad];

CPBPressureTouchGestureRecognizer* recognizer = [[CPBPressureTouchGestureRecognizer alloc] initWithTarget:self action:@selector(A_button:)];
[A_button addGestureRecognizer: recognizer];
[recognizer release];


[P2_button addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];

[P2_button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
}

- (void) A_button: (CPBPressureTouchGestureRecognizer*) recognizer {
[self.presLabel setText:[NSString stringWithFormat:@"%f",recognizer.pressure]];
}

最后

-(IBAction)touchUp :(id)sender{
[myTimer invalidate];
myTimer = nil;
NSLog(@"up");
}

-(IBAction)touchDown :(id)sender{

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                                           target:self
                                         selector:@selector(A_button:) 
                                         userInfo:nil 
                                          repeats:YES]; 


}

但不工作,如果在计时器选择器中我写 @selector(A_button) 什么都不做,如果我写 @selector(A_button:) 返回错误:

2012-02-22 22:38:24.837 TestPres[19686:707] -[__NSCFTimer pressure]:无法识别的选择器发送到实例 0x143570 2012-02-22 22:38:24.847 TestPres[19686:707] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFTimer 压力]:无法识别的选择器发送到实例 0x143570”

问题出在哪里?按下m键怎么能显示0.1秒的压力值?

谢谢

【问题讨论】:

    标签: iphone xcode uibutton selector pressure


    【解决方案1】:

    您的A_button: 方法期望CPBPressureTouchGestureRecognizer 作为参数传入。当您的计时器触发时,它会调用该方法,但会传递 NSTimer 对象。

    此外,压力手势识别器的工作方式与点击手势识别器非常相似。即使您保留对它的引用,它也不会持续监控施加的压力。它仅近似于识别水龙头时的压力。

    【讨论】:

    • 另外请注意,混合手势识别器和 UIControlEvents 可能会有点棘手。您必须真正考虑它们如何相互作用和/或相互冲突。
    • 谢谢,当我按住 UIButton 时,如何连续(每 0.1 秒)查看压力数据?
    • 我还没有找到这样做的方法。您使用的压力手势识别器依赖于加速度计。持续按住按钮不会产生感应压力所需的动作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多