3D Touch tableview的应用

事过几天来写3DTouch在tableview里面的使用了,用力点击cell触发的事件,
那么我就直接开始写了

   \\\\先创建一个UITableView
var mytableview : UITableView = UITableView()

//把这个View加到当前控制器中
override func viewDidLoad() {

//调用下面的方法设置TableView
 seUI()

//Capability(能力)判断有没有3DTouch的功能
   if self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available
       {
        //如果有设置3DTouch的代理,第一个参数,实现代理的控制器,第二个参数,触发3DTouch的View
        self.registerForPreviewingWithDelegate(self,sourceView:mytableview)
        print("3DTouch可用")
   }else {
        print("3DTouch不可用")
    }}


func setUI()
{
    //添加到当前View
    View.addSubview(mytableview)
    //设置frame
     mytableview.frame = view.frame
    //设置TableView的代理
     mytableview.dataSource = self
    //注册TableView的cell
    mytableview.registerClass(UITableViewCell.self, forCellReuseIdentifier: "mydcell")
}

//实现3DTouch的代理,继承协议UIViewControllerPreviewingDelegate
 extension ViewController : UIViewControllerPreviewingDelegate{

//当你点击cell的时候会出现一个演示会弹出控制器的窗口,再用力按的话会调用这个方法,必须实现
//点击后实现下面方法会进入弹出的控制器
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
     self.showViewController(viewControllerToCommit, sender: self)
    }

//代理必须实现的方法返回一个点击后需要进入的控制器
//location当前点击的点
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
     //创建一个自定义的控制器
     let con = mycontroller()
    
     //测试用,设置一下背景颜色
     con.view.backgroundColor = UIColor.orangeColor()
     //判定如果再有值的cell内
     if iscell(location){
     
     //判断当前点击的点在哪一个cell上
     let indx = mytableview.indexPathForRowAtPoint(location)

     //设置sourceRect这个属性,触发事件后,这个区域不会模糊,会有微微的放大效果
     previewingContext.sourceRect = tab.cellForRowAtIndexPath(indx!)!.frame
      
      //preferredContentSize设置控制器展示的大小(从左上角开始)
      //它会自动放大到能展示的最大尺寸
        con.preferredContentSize = CGSize(width: 375, height: 667)
    }
     //运用三木运算判定是否是有值的cell有就返回控制器没有救返回nil
    return iscell(location) ? con : nil
    }

//判断是否在有值的cell上
func iscell(point : CGPoint) -> Bool {
     let indx : NSIndexPath? = mytableview.indexPathForRowAtPoint(point)
     return indx != nil
}
}






//TableView的代理方法这里就不多讲了
extension ViewController : UITableViewDataSource
{

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 10
     }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("mydcell", forIndexPath: indexPath)
     cell.textLabel?.text = "第\\(indexPath.row)行"
     return cell 
  }
  }





//为了方便演示新建控制器我就写在这里面了


class mycontroller : UIViewController {
override func viewDidLoad() {
}

//重写下面这个方法后,弹出展示窗口时就可以向上滑动了,并且下面会出现 你创建的功能键,这个方法需要返回一个UIPreviewActionItem的数组
override func previewActionItems() -> [UIPreviewActionItem] {
    
   //创建功能键UIPreviewAction,这里的功能键和UIAlertAction的创建方法差不多
   //创建时里面的block会把你点击的这个avtion和当前控制器返回给你
    let avtion = UIPreviewAction(title: "猜猜我是谁", style: UIPreviewActionStyle.Default) { (action, controller) -> Void in
        print("点击了我是")
    }
    let avtion2 = UIPreviewAction(title: "哎哟今天天气不错哦", style: UIPreviewActionStyle.Default) { (action,controller) -> Void in
        print("点击了\(action.title)")
    }
    
   //创建一个功能键组UIPreviewActionGroup,后面的actions时一个UIPreviewAction的数组,点击后下面的按钮会变成actions里面的按钮,这样下面的功能键虽然数量有限但其实可以加很多
    let avg = UIPreviewActionGroup(title: "哎哟不错哦", style: UIPreviewActionStyle.Default, actions: [avtion,avtion2])

    //返回你要显示的功能键
    return [avtion,avtion2,avg]
}
}

你可能感兴趣的:(3D Touch tableview的应用)