至って単純なコードですが、情報少なめだったのでメモ。
サンプルコード内で使用している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];
}
}
}