- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
是否可以从当前用作 3D Touch 的 previewingContext
View Controller 的 UIViewController
检测触摸并获取触摸位置? (我想在触摸从左向右移动时更改预览 Controller 内的图像)
我已经尝试了 touchesBegan
和 touchesMoved
它们都没有被触发。
class ThreeDTouchPreviewController: UIViewController {
func getLocationFromTouch(touches: Set<UITouch>) -> CGPoint?{
guard let touch = touches.first else { return nil }
return touch.location(in: self.view)
}
//Not fired
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = getLocationFromTouch(touches: touches)
print("LOCATION", location)
}
//Not fired
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = getLocationFromTouch(touches: touches)
print("LOCATION", location)
}
}
甚至尝试添加一个 UIPanGesture。
尝试复制 FaceBook 的 3D Touch 功能,用户可以从左向右移动手指来更改当前显示的图像。上下文视频:https://streamable.com/ilnln
最佳答案
如果您想获得与 FaceBook 的 3D Touch 功能相同的结果,您需要创建自己的 3D Touch 手势类
import UIKit.UIGestureRecognizerSubclass
class ForceTouchGestureRecognizer: UIGestureRecognizer {
var forceValue: CGFloat = 0
var isForceTouch: Bool = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
handleForceWithTouches(touches: touches)
state = .began
self.isForceTouch = false
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
handleForceWithTouches(touches: touches)
if self.forceValue > 6.0 {
state = .changed
self.isForceTouch = true
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
state = .ended
handleForceWithTouches(touches: touches)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesCancelled(touches, with: event)
state = .cancelled
handleForceWithTouches(touches: touches)
}
func handleForceWithTouches(touches: Set<UITouch>) {
if touches.count != 1 {
state = .failed
return
}
guard let touch = touches.first else {
state = .failed
return
}
forceValue = touch.force
}
}
现在您可以在 viewDidLoad
方法中的 ViewController
中添加此手势
override func viewDidLoad() {
super.viewDidLoad()
let gesture = ForceTouchGestureRecognizer(target: self, action: #selector(imagePressed(sender:)))
self.view.addGestureRecognizer(gesture)
}
现在您可以在 Storyboard 中管理您的 Controller UI。在UICollectionView
和UIImageView
上方中间添加cover view
,并在代码中与IBOutlets
连接。
现在您可以为手势添加处理程序方法
func imagePressed(发件人:ForceTouchGestureRecognizer){
let location = sender.location(in: self.view)
guard let indexPath = collectionView?.indexPathForItem(
at: location) else { return }
让 image = self.images[indexPath.row]
switch sender.state {
case .changed:
if sender.isForceTouch {
self.coverView?.isHidden = false
self.selectedImageView?.isHidden = false
self.selectedImageView?.image = image
}
case .ended:
print("force: \(sender.forceValue)")
if sender.isForceTouch {
self.coverView?.isHidden = true
self.selectedImageView?.isHidden = true
self.selectedImageView?.image = nil
} else {
//TODO: handle selecting items of UICollectionView here,
//you can refer to this SO question for more info: https://stackoverflow.com/questions/42372609/collectionview-didnt-call-didselectitematindexpath-when-superview-has-gesture
print("Did select row at indexPath: \(indexPath)")
self.collectionView?.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
}
default: break
}
从这一点开始,您需要自定义您的 View ,使其看起来与 Facebook 相同。
我还在 GitHub 上创建了一个小示例项目 https://github.com/ChernyshenkoTaras/3DTouchExample演示一下
关于ios - 在 UIViewController 显示为 3DTouch 预览时检测 UITouches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44666454/
我希望能够在代码中布局我的 View Controller ,但可以看到界面构建器中显示的布局。 我知道我可以创建一个 UIView 子类,使其 IBDesignable,并将其分配给 View Co
我有一个父 UIViewController(MainVC)。从那里我有 2 个 segue 到 2 个 UIViewControllers:FirstVC(标识符:goFirstVC)和 Secon
我有一个导航 Controller 。第一个viewcontroller 是FirstViewController 的一种。当我在 FirstViewController 中点击一个按钮时,它会将第二
我有一个自定义的动画 UIViewController 过渡,似乎 iOS 中有一个错误会破坏横向布局。在主要动画方法中,我得到了横向和纵向 View 的混合。 (纵向 View 都是纵向的,所以没问
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我有一个 UIViewController,里面有 UITabBar。我正在尝试模仿 UITabBarController。 My question is how do I set or a UIVi
是否有任何方便的方法来确定是否正在从处于后台模式的应用加载 View ? 在 3.X 中,我将依赖 viewDidLoad 进行一些初始化等操作,但是对于 4.X 而言情况并非如此,因为您不能依赖调用
我将主视图 Controller 作为带有 UICollectionView 的 View Controller ,并且填充了照片。 一旦一个单元格被点击,一个新的 View Controller 被
我有一个简单的 UIViewController(单点触控),在 UI 上有一个按钮。现在,我的用例很简单 - 我想在单击按钮时重新加载 UIViewController。在某些线程上,我读到我只需要
我在它上面有一个 UIViewController 1 UIButton(添加为 subview ),在我按下 Button(见下面的图 1)之后,在它上面添加了另一个 UIViewControlle
尝试为 iPad 制作基于 Storyboard的应用程序。在其中,我需要从开始屏幕 (UIViewController) 过渡到主屏幕 (UISplitViewController),然后再到全屏
我想调整 UIViewController 的大小在 Storyboard中,所以我可以使用它的 UIViewController作为弹出 View 。 在大多数网站上,我可以读到这些操作是: 拖一个
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我正在构建一个锻炼应用程序。我的 TableViewController 中有一系列练习,每个单元格显示不同的练习。单元格转到 UIViewController。在 UIViewController
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我有一个 UIViewController,它有一个模式窗口,我想在整个界面上展示它,包括 UITabBar。 我的应用程序层次结构是这样的: UITabBarController (A) ->
我想显示另一个 ViewController 的缩小“预览”在另一个 ViewController .这可能吗?不需要交互,因此它基本上可以是屏幕截图,但按比例缩小。我可以通过手动截屏并将其保存为图像
我正在开发一个应用程序,其中 UIViewController ( firstViewController ) 包含一些 UILabels , 一个 UIButton , 和 UIView ( sub
我的标签栏中有五个标签栏项目。第五项是弹出ViewController。因此,当我单击该按钮时,当前 Controller 中将显示一个弹出窗口。我使用 UIViewController 作为子类来实
我正在开发 iOS 应用程序。 我的应用程序有 6 个按钮,每个按钮都有每个标签栏控件。 so Main screen is [btn1] [btn2] [btn3] [btn4] [btn5] [b
我是一名优秀的程序员,十分优秀!