我想通过单击和拖动来重新定位子图层。我遇到了困难,并整理了一个简单的演示来说明问题。在GitHub上查看下面的代码和/或this可运行项目,其中包括一个游乐场。
该演示的UI(请参见下图)由三个元素组成:主视图(黑色),子视图(红色)和子视图子层(灰色)。这两个视图均具有向控制台报告的轻击手势识别器。此外,该子视图还具有一个平移手势识别器,可以重新定位该子视图和/或其子层,并向控制台报告。
第一个问题-子图层落后于手势。
第二个问题-我无法抓住靠近其边缘的灰色子层。
如果我点击靠近其边缘的灰色子层,则控制台消息会正确报告灰色子层已被点击。但是,如果我尝试抓住同一位置并将其移动,则红色子视图将移动。
感谢您抽出宝贵的时间对此进行考虑。任何建议将不胜感激。
import UIKit
public class ViewController: UIViewController {
override public func loadView() {
let mainView = UIView(frame: CGRect.zero)
mainView.backgroundColor = .black
mainView.layer.name = "Main View's Layer"
mainView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognized(_:))))
self.view = mainView
}
override public func viewDidLayoutSubviews() {
let subview = UIView(frame: view.bounds.insetBy(dx: 50.0, dy: 100.0))
subview.backgroundColor = .red
subview.layer.name = "Sub View's Layer"
subview.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognized(_:))))
subview.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognized(_:))))
let sublayer = CALayer()
sublayer.bounds = subview.layer.bounds.insetBy(dx: 50, dy: 100)
sublayer.name = "Sub View's Sublayer"
sublayer.position = CGPoint(x: subview.layer.bounds.midX, y: subview.layer.bounds.midY)
sublayer.backgroundColor = UIColor.gray.cgColor
subview.layer.addSublayer(sublayer)
view.addSubview(subview)
}
// **********************************************************************************************************************
private struct TouchedLayer : CustomStringConvertible {
var layer: CALayer
let startingPosition: CGPoint
init(recognizer: UIGestureRecognizer) {
let view = recognizer.view!
let touchLocation = recognizer.location(in: view)
let hitTestLocation = view.layer.superlayer!.convert(touchLocation, from: view.layer)
layer = view.layer.hitTest(hitTestLocation)!
startingPosition = layer.position
}
mutating func updateLayerPosition(translation: CGPoint) {
layer.position = CGPoint(x: startingPosition.x + translation.x, y: startingPosition.y + translation.y)
}
var description: String {
return "\(layer.name ?? "<Unknown>"): position = \(layer.position)"
}
}
@objc func tapGestureRecognized(_ sender: UITapGestureRecognizer) {
guard sender.state == .ended else { return }
let tappedLayer = TouchedLayer(recognizer: sender)
print("\nTapped", tappedLayer)
}
private var panningLayer: TouchedLayer!
@objc func panGestureRecognized(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
panningLayer = TouchedLayer(recognizer: sender)
print("\nPanning Begun - \(panningLayer!)")
case .changed:
panningLayer.updateLayerPosition(translation: sender.translation(in: sender.view))
//print("\tPanning - \(panningLayer!)")
default:
print("Panning Ended - \(panningLayer!)")
}
}
}
答案 0 :(得分:1)
我想通过单击并拖动来重新定位子层
我建议您不要再这样做了。图层没有可触摸性,而视图则有。实际上,已经明智地说过,视图 是一层加上可触摸性。如果使用子视图而不是子层,则可以直接触摸子视图,在其上具有手势识别器,以正常方式点击和平移,依此类推。通过使用图层而不是视图,您只是在给自己增加不必要的生活,(据我所知)没有明显的收获。