【问题标题】:iOS 3D Touch in Objective CObjective C 中的 iOS 3D Touch
【发布时间】:2015-09-14 09:08:23
【问题描述】:

任何人都知道我们如何在objective C 中实现3d touch 功能?我已经检查了应用程序文档,但没有示例与目标 C 代码相关。

任何示例代码或示例,如何实现?提前致谢。

【问题讨论】:

  • UITouch 通过force 属性(iOS9+)为您提供有关触摸力度的信息,您可以通过任何UIResponser 类(iOS2+)获取当前UITouch 事件);其余的或如何使用它仅受您的想象力限制。
  • 如果有人需要 Swift 的答案,请在此处查看我的答案stackoverflow.com/a/32593044/4106559

标签: ios


【解决方案1】:

我这样做的方法是使用我提供的DFContinuousForceTouchGestureRecognizer。这是一个手势识别器扩展,可检测强制触摸并与其他手势识别器一起工作。

DFContinuousForceTouchGestureRecognizer 提供有关压力变化的持续更新,因此您可以做一些很好的事情,例如在用户改变压力时增加视图,而不是仅仅触发单个事件。如果您只想要一个事件,您可以忽略 DFContinuousForceTouchDelegate 中的所有内容,除了 - (void) forceTouchRecognized 回调。

https://github.com/foggzilla/DFContinuousForceTouchGestureRecognizer

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

在你的 UIViewController 实现中:

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

    [self.imageView addGestureRecognizer:_forceTouchRecognizer];
}

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

#pragma DFContinuousForceTouchDelegate

- (void) forceTouchRecognized:(DFContinuousForceTouchGestureRecognizer*)recognizer {
    self.imageView.transform = CGAffineTransformIdentity;
    [self.imageView setNeedsDisplay];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[[UIAlertView alloc] initWithTitle:@"Force Touch" 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.imageView.transform = CGAffineTransformMakeScale(transformDelta, transformDelta);
    [self.imageView setNeedsDisplay];
}

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

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

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

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

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

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

如果你在 iOS8 上添加它会崩溃,所以如果你支持 iOS9 之前的版本,请根据你运行的 iOS 版本有条件地添加这个识别器。

【讨论】:

    【解决方案2】:

    这是一个改编自 github 上 yeungkaho 示例的示例

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self handleTouches:touches];
    }
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self handleTouches:touches];
    }
    -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self setForcePercentage:0];
    }
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self setForcePercentage:0];
    }
    -(void)handleTouches:(NSSet<UITouch *> *)touches{
    
        for (UITouch *touch in touches) {
            CGFloat force = touch.force;
            CGFloat percentage = force/touch.maximumPossibleForce;
            [self setForcePercentage:percentage];
            break;
        }
    }
    
    -(void)setForcePercentage:(CGFloat)percentage{
    
        NSLog(@"LEVEL = %f",percentage*100);
    }
    

    【讨论】:

      【解决方案3】:

      我正在分享按应用图标填充列表中的 4 项的源代码

      第 1 步:- 在 appDelegate.m 中导入类

      导入 sys/utsname.h

      第 2 步:-

      #pragma MARK for Get Machine Name
      
      -  (NSString *) machineName {
          struct utsname systemInfo;
          uname(&systemInfo);
          return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
      }
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      
          if([[self machineName] isEqualToString:@"iPhone8,2"]|| [[self machineName] isEqualToString:@"iPhone8,1"]) {
              [self addEventsFor3DTouchEvents];
          }
          return YES;
      }
      

      为三个 D Touch 事件添加动作的 pragma MARK

      - (void) addEventsFor3DTouchEvents {
          if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
              UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_1 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_1] userInfo:nil];
      
              UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_2 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_2] userInfo:nil];
      
              UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_3 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_3] userInfo:nil];
      
              UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_4 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_4] userInfo:nil];
      
              [[UIApplication sharedApplication] setShortcutItems: @[ item1, item2, item3, item4 ]];
          }
      }
      
      
      #pragma mark - 3DTouch Delegate Methods
      
      - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
          [self moveThrough3DTouch:shortcutItem];
      }
      
      #pragma MARK for Handling Action for Three D Touch Events
      
      - (void)moveThrough3DTouch:(UIApplicationShortcutItem *)temp {
      
          if ([temp.localizedTitle isEqualToString:TITLE_NAME_1])  {
      
                  [self.tabBarController setSelectedIndex:0];
          } else if([temp.localizedTitle isEqualToString:TITLE_NAME_2]) {
      
                  [self.tabBarController setSelectedIndex:1];
          } else if([temp.localizedTitle isEqualToString:TITLE_NAME_3]) {
      
                  [self.tabBarController setSelectedIndex:2];
          } else if([temp.localizedTitle isEqualToString:TITLE_NAME_4]) {
      
                  [self.tabBarController setSelectedIndex:3];
          }
      }
      

      **我在我的应用程序中使用标签栏控制器,如果想在视图控制中移动

      - (void) moveToControllerScene {
              UIStoryboard *storyboard              = [UIStoryboard storyboardWithName:STORY_BOARD_IDENTIFIER bundle:nil];
              YOUR_CONTROLLER_OBJECT *obj           = [storyboard instantiateViewControllerWithIdentifier:@"YOUR_CONTROLLER_OBJECT"];
              [navController pushViewController:obj animated:YES];
      }
      

      【讨论】:

        猜你喜欢
        • 2018-08-31
        • 1970-01-01
        • 2017-10-05
        • 2016-06-22
        • 1970-01-01
        • 2017-07-16
        • 2019-09-24
        • 1970-01-01
        相关资源
        最近更新 更多