【发布时间】:2015-06-30 05:26:46
【问题描述】:
我正在使用 Swift 1.2
我正在尝试使用此 UIScrollview touchesbegan,touchesmoved,touchesended actions 以及已接受答案的第二条评论中的链接。
我正在使用带有自动布局的情节提要,我在自定义类中为 UIScrollView 设置了我的自定义类。
我没有在包含我的自定义 UIScrollView 的 UIViewController 中接收到这些触摸事件
编辑:用@Victor Sigler 的回答更新了我如何使用它的代码。
自定义ScrollView代码:
import UIKit
protocol PassTouchesScrollViewDelegate {
func scrollTouchBegan(touches: Set<NSObject>, withEvent event: UIEvent)
func scrollTouchMoved(touches: Set<NSObject>, withEvent event: UIEvent)
}
class PassTouchesScrollView: UIScrollView {
var delegatePass : PassTouchesScrollViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.delegatePass?.scrollTouchBegan(touches, withEvent: event)
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
self.delegatePass?.scrollTouchMoved(touches, withEvent: event)
}
}
我的视图控制器:
import UIKit
class ViewController: UIViewController, PassTouchesScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegatePass = self
}
func scrollTouchBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
println("began \(touches)")
}
func scrollTouchMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
println("moved \(touches)")
}
}
我试图让用户在 UIImage 上画一条线,我使用 PanGesture 识别器开始工作,但性能很差,尤其是在旧硬件上,我遵循了 Ray Wenderlich 教程,该教程使用了 touches Began 和性能要好得多,但它是在 UIView 而不是 ScrollView 上。我需要一个 UIScrollView,因为在用户绘制图像之前,他们可以放大和围绕它。
【问题讨论】:
标签: swift uiscrollview touchesbegan