ios touch坐标_iOS 3D Touch –窥视与流行

ios touch坐标

In this tutorial, we’ll be discussing and implementing the 3D Touch functionality in our iOS Application.

在本教程中,我们将在iOS应用程序中讨论和实现3D Touch功能。

窥视与流行 (Peek and Pop)

Peek and Pop is a functionality that is available in iPhone 6s and above. It occurs when a user presses deeply onto the screen.
we can show a preview of the next screen in a small window along with Quick Actions too.
It gives the feeling of a 3D touch.

Peek and Pop是iPhone 6s及更高版本中可用的功能。 当用户深深按压屏幕时会发生这种情况。
我们还可以在一个小窗口中显示下一个屏幕的预览以及“快速操作”。
它给人以3D触摸的感觉。


Pressing Hard shows you the preview.
按“硬”将显示预览。
Pressing Harder would show the entire screen.
按下更难将显示整个屏幕。

To do it automatically using Storyboard, we can always select the option “Preview & Commit Segues” in the attributes inspector on the segue.

要使用Storyboard自动执行此操作,我们始终可以在segue的属性检查器中选择“预览并提交Segues”选项。

In order to use Peek and Pop feature, UIViewControllerPreviewingDelegate protocol must be added to the ViewController class and the delegate methods need to be implemented.

为了使用Peek and Pop功能,必须将UIViewControllerPreviewingDelegate协议添加到ViewController类,并且需要实现委托方法。

  • previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint)

    previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint)
  • previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)

    previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)

Learn by example!

通过例子学习!

In the next section, we’ll learn by implementing Peek and Pop on a UITableView in our iOS Application.

在下一节中,我们将通过在iOS应用程序的UITableView上实现Peek和Pop来学习。

First the storyboard!

首先是情节提要!

项目情节提要 (Project Storyboard)

Let’s embed the First View Controller in a NavigationController.

让我们将First View Controller嵌入到NavigationController中。

Connect the IBOutlet for all the elements to their respective View Controller.

将所有元素的IBOutlet连接到各自的View Controller。

(Code)

In order to implement Peek and Pop, we need to first register the view on which the 3D interaction is enabled.
We can add the following statement in the viewDidLoad method for that:

为了实现Peek and Pop,我们需要首先注册启用3D交互的视图。
我们可以为此在viewDidLoad方法中添加以下语句:

if( traitCollection.forceTouchCapability == .available){
            registerForPreviewing(with: self, sourceView: myTableView)
        }

The code for the ViewController.swift file is given below:

下面给出了ViewController.swift文件的代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIViewControllerPreviewingDelegate {
    

    @IBOutlet weak var myTableView: UITableView!
    
    var myArray = ["apple","windows","amazon"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        if( traitCollection.forceTouchCapability == .available){
            registerForPreviewing(with: self, sourceView: myTableView)
        }
        
        myTableView.delegate = self
        myTableView.dataSource = self
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = myArray[indexPath.row]
        return cell
    }
    
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = myTableView?.indexPathForRow(at: location) else { return nil }
        
        guard let cell = myTableView?.cellForRow(at: indexPath) else { return nil }
        
        guard let detailVC = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else { return nil }
        
        let text = myArray[indexPath.row]
        detailVC.string = text
        
        detailVC.preferredContentSize = CGSize(width: 0.0, height: 300)
        
        previewingContext.sourceRect = cell.frame
        
        return detailVC
    }
    
    var name: String?
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        name = myArray[indexPath.row]
        
        let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
        
        secondViewController.string = name
        
        self.navigationController?.pushViewController(secondViewController, animated: true)
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        show(viewControllerToCommit, sender: self)
    }

}

We populate the UITableView with an array of strings.
In the previewing protocol delegate method, we launch the detail view controller in a small window.
In the didSelectRow method of the UITableView protocols, we directly take the user to the DetailsViewController alongwith passing the data.

我们用字符串数组填充UITableView。
在预览协议委托方法中,我们在一个小窗口中启动细节视图控制器。
在UITableView协议的didSelectRow方法中,我们将用户直接传递到DetailsViewController并传递数据。

The code for the DetailViewController.swift is given below:

下面给出了DetailViewController.swift的代码:

import UIKit

class DetailViewController : UIViewController{
    
    @IBOutlet weak var myLabel: UILabel!
    
    var string: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let string = string{
            myLabel.text = string
        }
    }
}

The output of the above application in action is given below:

上面应用程序的输出如下:

使用预览动作 (Using Preview Actions)

To show Preview Actions, you need to add the following code in the ViewController that is previewed.
It is DetailsViewController in our case.

要显示预览动作,您需要在预览的ViewController中添加以下代码。
在我们的例子中是DetailsViewController。

import UIKit

class DetailViewController : UIViewController{
    
    @IBOutlet weak var myLabel: UILabel!
    
    var string: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let string = string{
            myLabel.text = string
        }
    }
    
    override var previewActionItems : [UIPreviewActionItem] {
        
        let likeAction = UIPreviewAction(title: "Option 1", style: .default) { (action, viewController) -> Void in
            print("You clicked this")
        }
        
        let deleteAction = UIPreviewAction(title: "Option 2", style: .destructive) { (action, viewController) -> Void in
            print("You clicked this too")
        }
        
        return [likeAction, deleteAction]
        
    }
}

The output of the application with the above actions is given below:

具有上述操作的应用程序输出如下:

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

翻译自: https://www.journaldev.com/22950/ios-3d-touch-peek-and-pop

ios touch坐标

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中的exc_bad_access通常是由于访问了无效的内存地址而触发的错误。要解决这个问题,我们可以遵循以下几个步骤: 1. 检查Crash日志:首先,我们应该查看Crash日志以了解问题的具体原因。Crash日志将显示出错的位置以及相关的堆栈信息,这有助于我们确定问题的根源。 2. 使用断点:如果我们知道大概出错的位置,可以在代码中设置断点来逐步调试。这样,我们可以在错误出现前暂停应用程序的执行,从而更好地分析错误。 3. 检查空指针:空指针访问是常见的exc_bad_access错误。我们应该检查代码中的指针变量是否为空并确保在使用前进行了正确的初始化。 4. 检查内存释放:内存管理是另一个常见的exc_bad_access错误的原因。我们需要确保在释放内存之后不再访问已释放的内存。可以使用工具如Instruments来检测内存泄漏和野指针。 5. 使用ARC(自动引用计数):如果我们的应用程序使用了手动管理内存,那么我们应该考虑迁移到ARC来减少内存管理错误的发生。ARC会自动处理内存释放,从而降低了内存相关的问题。 6. 避免循环引用:循环引用也可能导致exc_bad_access错误。我们应该小心使用强引用和弱引用,以避免循环引用的产生。 7. 更新代码库和依赖项:如果我们使用的是第三方库或依赖项,那么我们应该确保它们是最新的版本并且与我们应用程序的其他部分兼容。有时,exc_bad_access错误可能是由于库或依赖项的错误导致的。 总之,解决exc_bad_access错误需要仔细检查代码和内存管理,并根据具体情况进行调试和修复。通过遵循上述步骤,我们可以更好地理解问题并找到适当的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值