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坐标