2

我是 iOS 开发的新手,但我需要在 UIButton 上实现 3d 触摸,当用户在按钮快捷菜单上施加力时会弹出,即使有人可以向我提供 ObjectiveC 中用于 3d 触摸的示例代码,我将不胜感激。提前致谢。

4

2 回答 2

10

是的,我在UIButton下面 github 上的示例应用程序中演示了如何在 a 上检测 3d 触摸。对于菜单弹出部分,我将把它留给你来实现,但这会让你得到力/3d 触摸片。

https://github.com/foggzilla/DFContinuousForceTouchGestureRecognizer

我这样做的方法是使用我在示例项目中提供的DFContinuousForceTouchGestureRecognizer 。

提供有关压力变化的DFContinuousForceTouchGestureRecognizer持续更新,因此您可以做一些不错的事情,例如在用户改变其压力时转换按钮,而不是单个事件。如果您只想要一个事件,您可以忽略回调中的DFContinuousForceTouchDelegate所有内容- (void) forceTouchRecognized

您可以下载它并在支持力压的设备上运行示例应用程序,看看感觉如何。

在您的 UIViewController 实现中:

- (void)viewDidLoad {
    [super viewDidLoad];
    _forceTouchRecognizer = [[DFContinuousForceTouchGestureRecognizer alloc] init];
    _forceTouchRecognizer.forceTouchDelegate = self;

    [self.button addGestureRecognizer:_forceTouchRecognizer];
}

实现强制触摸的委托协议:

#pragma DFContinuousForceTouchDelegate

- (void)forceTouchRecognized:(DFContinuousForceTouchGestureRecognizer *)recognizer {
    self.button.transform = CGAffineTransformIdentity;
    [self.button setNeedsDisplay];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[[UIAlertView alloc] initWithTitle:@"Force Touch Button" message:@"YEAH!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    });
}
- (void)forceTouchRecognizer:(DFContinuousForceTouchGestureRecognizer *)recognizer didStartWithForce:(CGFloat)force maxForce:(CGFloat)maxForce {
    CGFloat transformDelta = 1.0f + ((force/maxForce) / 3.0f);
    self.button.transform = CGAffineTransformMakeScale(transformDelta, transformDelta);
    [self.button setNeedsDisplay];
}

- (void)forceTouchRecognizer:(DFContinuousForceTouchGestureRecognizer *)recognizer didMoveWithForce:(CGFloat)force maxForce:(CGFloat)maxForce {
    CGFloat transformDelta = 1.0f + ((force/maxForce) / 3.0f);
    self.button.transform = CGAffineTransformMakeScale(transformDelta, transformDelta);
    [self.button setNeedsDisplay];
}

- (void)forceTouchRecognizer:(DFContinuousForceTouchGestureRecognizer *)recognizer didCancelWithForce:(CGFloat)force maxForce:(CGFloat)maxForce  {
    self.button.transform = CGAffineTransformIdentity;
    [self.button setNeedsDisplay];
}

- (void)forceTouchRecognizer:(DFContinuousForceTouchGestureRecognizer *)recognizer didEndWithForce:(CGFloat)force maxForce:(CGFloat)maxForce {
    self.button.transform = CGAffineTransformIdentity;
    [self.button setNeedsDisplay];
}

- (void)forceTouchDidTimeout:(DFContinuousForceTouchGestureRecognizer *)recognizer {
    self.button.transform = CGAffineTransformIdentity;
    [self.button setNeedsDisplay];
}

请注意,这仅在支持强制触摸的设备上有用。

此外,DFContinuousForceTouchGestureRecognizer如果您在 iOS8 或更低版本上运行,则不应将其添加到视图中,因为它使用仅在 iOS9 中可用的新force属性。UITouch

如果您在 iOS8 上添加它会崩溃,因此如果您支持的版本早于 iOS 9,请根据您正在运行的 iOS 版本有条件地添加此识别器。

于 2015-09-30T19:01:25.633 回答
9

您可以重载UIButton并检查es 中的force属性UITouch

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;

根据此处发生的情况,您可以发送自己的强制触摸自定义操作,其中:

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;

于 2015-09-25T01:12:41.257 回答