8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-CでForce Touch (3D Touch)の値を参照する方法

Last updated at Posted at 2015-09-25

至って単純なコードですが、情報少なめだったのでメモ。
サンプルコード内で使用しているUIDeviceのカテゴリクラスはこちら。
https://github.com/erichoracek/UIDevice-Hardware

objective-c

#import "UIDevice+Hardware.h"

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // デバイス情報を取得
    NSString *platform = [UIDevice currentDevice].platform;
    NSLog(@"platform: %@", platform);
    
    // iPhone 6sかiPhone 6s Plusであれば実行
    if ([platform hasPrefix:@"iPhone8"] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        UITouch *touch = touches.anyObject;
        NSLog(@"touch.force: %@ touch.maximumPossibleForce: %@", @(touch.force), @(touch.maximumPossibleForce));
        if (touch.force >= touch.maximumPossibleForce * 0.5) {
            // 50%の強さを超えたら処理を実行
            [self doSomething];
        }
    }
}

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?