OC - 3D Touch(2)
来源:互联网 发布:淘宝客服部门奖罚制度 编辑:程序博客网 时间:2024/05/18 14:13
点击UITableViewCell后弹出详情内容:
代码如下:
ViewController:
#import "ViewController.h"#import "My3DTouchViewController.h"#import "DetailsViewController.h"#import "TableViewCell.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>@property(nonatomic,weak)UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITableView *tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; tableView.dataSource=self; tableView.delegate=self; [tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"tableViewCell"]; [self.view addSubview:tableView]; self.tableView=tableView;}- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{ // 打开详情页触发 if (viewControllerToCommit){ [self showViewController:viewControllerToCommit sender:self]; }}- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ // 1.我们可以通过触控的location获取 当前点击的是第几个元素(位于collectionview) // 2.由于响应3dtouch事件的view是cell的contentView,我们对触控拿到的location要进行一个转换(相对于collectionview(父视图)来说的位置 // 3.这里通过previewingContext中sourceView获取到触摸的view UIView *contentView = previewingContext.sourceView; // 看回"2" 中第一句话,我们要拿到这个触控的location位于collectionview中的位置 CGPoint iLocation = [contentView convertPoint:location toView:self.tableView]; // 将 获取到的位置 通过collectionview获取indexPath NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:iLocation]; DetailsViewController * detailVC = [[DetailsViewController alloc]init]; detailVC.titleStr = [NSString stringWithFormat:@"%ld",indexPath.row]; return detailVC;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 20;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *detailCell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"]; //设置代理 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0){ detailCell.delegate = self; } detailCell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row]; return detailCell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DetailsViewController *detailVC = [[DetailsViewController alloc]init]; detailVC.titleStr = [NSString stringWithFormat:@"%ld",indexPath.row]; [self.navigationController pushViewController:detailVC animated:YES];}@end
//详情控制器DetailsViewController
#import "DetailsViewController.h"@interface DetailsViewController ()@end@implementation DetailsViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor blueColor]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100,30)]; label.center = self.view.center; label.textColor=[UIColor whiteColor]; label.text = [NSString stringWithFormat:@"内容为:%@d",_titleStr]; [self.view addSubview:label];}#pragma mark 代理方法,设置 这个页面可以选择的按钮(3dtouch 呼出预览页后处于下方的按钮)- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{ UIPreviewAction *itemA = [UIPreviewAction actionWithTitle:@"加入购物车" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"加入购物车"); }]; UIPreviewAction *itemA2 = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"取消"); }]; return @[itemA,itemA2];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
TableViewCell
#import <UIKit/UIKit.h>@interface TableViewCell : UITableViewCell// 3dtouch代理@property (weak,nonatomic) id<UIViewControllerPreviewingDelegate> delegate;@end#import "TableViewCell.h"@implementation TableViewCell- (void)awakeFromNib { // Initialization code}-(void)setDelegate:(id<UIViewControllerPreviewingDelegate>)delegate{ if (!_delegate){ _delegate = delegate; UIViewController *controller = (UIViewController *)delegate; [controller registerForPreviewingWithDelegate:delegate sourceView:self.contentView]; }}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
效果图:
1 0
- OC - 3D Touch(2)
- iOS 3D touch OC代码
- OC - 3D Touch(1)
- 关于3D Touch
- 关于3D Touch
- 3D Touch初探
- 3D Touch大法
- 3D Touch
- ios 3D Touch
- 3D touch
- 3D Touch
- 3D Touch
- iphone 3D Touch
- 3D Touch入门
- 3D Touch APIs
- ios9 3D Touch
- 3D Touch详解
- 3D touch功能
- /data/data/和手机拍照
- MachineLearning in Action_KNN
- kafak、flume、elasticsearch
- 使用timer方法生成一个定时器,每隔一段时间输出一个随机数
- internal(C# 参考)
- OC - 3D Touch(2)
- idea-multimarkdown 插件破解小记
- bzoj 1087: [SCOI2005]互不侵犯King 状压dp
- [Struts2教程]利用Filter来做控制器
- mysql 性能优化方案
- RSync实现文件备份同步
- ubuntu下opensips安装配置
- ListView设置EmptyView
- hdu 3038 How Many Answers Are Wrong(并查集变种)