Explorar el Código

1.修改Apex Mobile Result 3D Touch实现方式并添加震动反馈。

Pen Li hace 8 años
padre
commit
3f0e9a05ae

+ 24 - 0
common/customUI/ResultCell.h

@@ -0,0 +1,24 @@
+//
+//  ResultCell.h
+//  Apex Mobile
+//
+//  Created by Jack on 2018/2/14.
+//  Copyright © 2018年 United Software Applications, Inc. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class ResultCell;
+@protocol ResultCellTouchDelegate <NSObject>
+
+@optional
+- (void)touchedCell:(ResultCell *)cell withForce:(float)force;
+
+@end
+
+@interface ResultCell : UITableViewCell
+
+@property (nonatomic,weak) id<ResultCellTouchDelegate> touchDelegate;
+@property (nonatomic,assign) BOOL handlingTouch;
+
+@end

+ 51 - 0
common/customUI/ResultCell.m

@@ -0,0 +1,51 @@
+//
+//  ResultCell.m
+//  Apex Mobile
+//
+//  Created by Jack on 2018/2/14.
+//  Copyright © 2018年 United Software Applications, Inc. All rights reserved.
+//
+
+#import "ResultCell.h"
+
+@implementation ResultCell
+
+- (void)awakeFromNib {
+    [super awakeFromNib];
+    // Initialization code
+}
+
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
+    [super setSelected:selected animated:animated];
+
+    // Configure the view for the selected state
+}
+
+- (void)touch:(NSSet<UITouch *> *)touches {
+    if (self.handlingTouch) {
+        return;
+    }
+    UITouch *touch = [[touches allObjects] lastObject];
+    if (self.touchDelegate && [self.touchDelegate respondsToSelector:@selector(touchedCell:withForce:)]) {
+        [self.touchDelegate touchedCell:self withForce:touch.force];
+    }
+}
+
+- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
+    self.handlingTouch = NO;
+    [self touch:touches];
+}
+
+- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
+    [self touch:touches];
+}
+
+- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
+    [self touch:touches];
+}
+
+- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
+    [self touch:touches];
+}
+
+@end

+ 3 - 3
common/customUI/ResultCell.xib

@@ -1,17 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
     <device id="retina4_7" orientation="portrait">
         <adaptation id="fullscreen"/>
     </device>
     <dependencies>
         <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
         <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
-        <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="tCell" id="Lic-Ng-p4U">
+        <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="tCell" id="Lic-Ng-p4U" customClass="ResultCell">
             <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
             <autoresizingMask key="autoresizingMask"/>
             <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Lic-Ng-p4U" id="wsG-8E-9L2">

+ 37 - 15
common/customUI/ResultViewController.m

@@ -20,11 +20,13 @@
 #import "RAConvertor.h"
 #import "RAUtils.h"
 //#import "RTLabel.h"
+#import "ResultCell.h"
+#import <AudioToolbox/AudioToolbox.h>
 
 
 const int delta = 25;
 
-@interface ResultViewController () <JLRefreshDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIViewControllerPreviewingDelegate>
+@interface ResultViewController () <JLRefreshDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIViewControllerPreviewingDelegate,ResultCellTouchDelegate>
 
 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *w_constraint;
 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *h_constraint;
@@ -105,6 +107,10 @@ const int delta = 25;
 //    self.tableview.tableHeaderView = [self get_tableHeader];
 }
 
+- (void)viewWillAppear:(BOOL)animated {
+    [super viewWillAppear:animated];
+}
+
 - (void)presses3DTouchForIndexPath:(NSIndexPath *)indexPath {
     
 }
@@ -553,8 +559,8 @@ const int delta = 25;
     
     {
         NSString *CellIdentifier = @"tCell";
-        UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
-        
+        ResultCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
+        cell.touchDelegate = self;
         for(UIGestureRecognizer* g in cell.gestureRecognizers)
            [ cell removeGestureRecognizer:g];
         //        cell gesture
@@ -578,13 +584,13 @@ const int delta = 25;
         [cell addGestureRecognizer:cellDoubleTap];
         
         // 3D Touch
-        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
-            DebugLog(@"3D Touch  可用!");
-            //给cell注册3DTouch的peek(预览)和pop功能
-            [self registerForPreviewingWithDelegate:self sourceView:cell];
-        } else {
-            DebugLog(@"3D Touch 无效");
-        }
+//        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
+//            DebugLog(@"3D Touch  可用!");
+//            //给cell注册3DTouch的peek(预览)和pop功能
+//            [self registerForPreviewingWithDelegate:self sourceView:cell];
+//        } else {
+//            DebugLog(@"3D Touch 无效");
+//        }
         
         
         NSArray* arr_col=self.content_layout[@"header"][@"col"];
@@ -1126,7 +1132,7 @@ const int delta = 25;
 // peek (预览)
 - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
     // [previewingContext sourceView]即为所按压的视图
-    UITableViewCell *view = (UITableViewCell *)[previewingContext sourceView];
+//    UITableViewCell *view = (UITableViewCell *)[previewingContext sourceView];
 //
 //    // 设置不被虚化的范围
 //    CGRect rect = view.bounds;
@@ -1139,9 +1145,9 @@ const int delta = 25;
 //    tvc.preferredContentSize = CGSizeMake(0, 480); // 预览图的大小
 //
 //    return tvc;
-    
-    NSIndexPath *indexPath = [self.tableview indexPathForCell:view];
-    [self presses3DTouchForIndexPath:indexPath];
+
+//    NSIndexPath *indexPath = [self.tableview indexPathForCell:view];
+//    [self presses3DTouchForIndexPath:indexPath];
     
     return nil;
 }
@@ -1150,7 +1156,23 @@ const int delta = 25;
 - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
     
 //    [self showViewController:viewControllerToCommit sender:self];
-    
+
 }
 
+#pragma mark - Cell Touch Delegate
+
+- (void)touchedCell:(ResultCell *)cell withForce:(float)force {
+    
+    if (force > 4.0f) {
+        cell.handlingTouch = YES;
+        // 1519: 普通短震,3D Touch 中 Peek 震动反馈
+        // 1520: 普通短震,3D Touch 中 Pop 震动反馈
+        // 1521: 连续三次短震
+        // kSystemSoundID_Vibrate: 震动很暴力,不适合震动反馈
+        AudioServicesPlaySystemSound(1519); // 支持震动的设备上会震动,不支持就什么都不做 kSystemSoundID_Vibrate
+        NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
+        [self presses3DTouchForIndexPath:indexPath];
+    }
+    NSLog(@"force %f",force);
+}
 @end