Swift 中的 UIScrollView 子类用于触摸开始和触摸移动
Subclass UIScrollView in Swift for touches Began & touches Moved
我正在使用 Swift 1.2
我正在尝试在已接受答案的第二条评论中使用此 UIScrollview touchesbegan,touchesmoved,touchesended actions 和 link。
我正在使用具有自动布局的故事板,我在自定义 Class 中为 UIScrollView 设置了自定义 class。
我在包含自定义 UIScrollView
的 UIViewController 中没有收到任何这些触摸事件
编辑:使用@Victor Sigler 的回答更新了我的代码。
自定义滚动视图代码:
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)
}
}
我的ViewController:
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 Recognizer 开始工作,但性能很差,尤其是在较旧的硬件上,我遵循了 Ray Wenderlich 教程,该教程使用 touches Began并且性能要好得多,但是它是在 UIView 而不是 ScrollView 上。我需要一个 UIScrollView,因为在用户绘制图像之前,他们可以放大图像并围绕它放大。
touchesBegan
仅适用于 UIView
的子类。把它从你的 UIViewController 中拿出来让它工作。
你想错了。如果您想知道 UIScrollView
何时移动,则无需 subclass 它。 iOS 已经在 UIScrollViewDelegate
.
中设置了你需要的所有方法
您必须实现 UISCrollViewDelegate
才能收到有关 UIScrollView
中的操作的通知,并通过 Interface Builder 或在代码中设置 delegate
,由您决定。
请参阅以下示例以了解如何执行此操作:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self
}
}
不过,如果您想知道它是如何按照您上面解释的方式进行操作的,则必须按照以下步骤操作:
您必须像在 class PassTouchesScrollView
中那样从 UIScrollView
class 中继承 class 并实施委托模式通过以下方式收到关于 UIScrollView
的通知:
import UIKit
protocol PassTouchesScrollViewDelegate {
func touchBegan()
func touchMoved()
}
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) {
// Notify it's delegate about touched
self.delegatePass?.touchBegan()
if self.dragging == true {
self.nextResponder()?.touchesBegan(touches, withEvent: event)
} else {
super.touchesBegan(touches, withEvent: event)
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
// Notify it's delegate about touched
self.delegatePass?.touchMoved()
if self.dragging == true {
self.nextResponder()?.touchesMoved(touches, withEvent: event)
} else {
super.touchesMoved(touches, withEvent: event)
}
}
}
你的classViewController
必须按以下方式实现:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: PassTouchesScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.delegatePass = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func touchMoved() {
println("touch moved")
}
func touchBegan() {
println("touch began")
}
}
您必须在界面生成器中 select 您的 UIScrollView
并将 class 设置为您的 class PassTouchesScrollView
, 在 class 部分的 Identity Inspector 中。
您应该会在控制台中看到以下内容:
touch began
touch began
touch began
touch began
touch began
touch began
touch moved
touch move
希望对你有所帮助。
试试这个
extension UIScrollView {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesBegan(touches, with: event)
print("touchesBegan")
}
override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesMoved(touches, with: event)
print("touchesMoved")
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesEnded(touches, with: event)
print("touchesEnded")
}
}
我正在尝试在已接受答案的第二条评论中使用此 UIScrollview touchesbegan,touchesmoved,touchesended actions 和 link。
我正在使用具有自动布局的故事板,我在自定义 Class 中为 UIScrollView 设置了自定义 class。
我在包含自定义 UIScrollView
的 UIViewController 中没有收到任何这些触摸事件编辑:使用@Victor Sigler 的回答更新了我的代码。
自定义滚动视图代码:
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)
}
}
我的ViewController:
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 Recognizer 开始工作,但性能很差,尤其是在较旧的硬件上,我遵循了 Ray Wenderlich 教程,该教程使用 touches Began并且性能要好得多,但是它是在 UIView 而不是 ScrollView 上。我需要一个 UIScrollView,因为在用户绘制图像之前,他们可以放大图像并围绕它放大。
touchesBegan
仅适用于 UIView
的子类。把它从你的 UIViewController 中拿出来让它工作。
你想错了。如果您想知道 UIScrollView
何时移动,则无需 subclass 它。 iOS 已经在 UIScrollViewDelegate
.
您必须实现 UISCrollViewDelegate
才能收到有关 UIScrollView
中的操作的通知,并通过 Interface Builder 或在代码中设置 delegate
,由您决定。
请参阅以下示例以了解如何执行此操作:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self
}
}
不过,如果您想知道它是如何按照您上面解释的方式进行操作的,则必须按照以下步骤操作:
您必须像在 class
PassTouchesScrollView
中那样从UIScrollView
class 中继承 class 并实施委托模式通过以下方式收到关于UIScrollView
的通知:import UIKit protocol PassTouchesScrollViewDelegate { func touchBegan() func touchMoved() } 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) { // Notify it's delegate about touched self.delegatePass?.touchBegan() if self.dragging == true { self.nextResponder()?.touchesBegan(touches, withEvent: event) } else { super.touchesBegan(touches, withEvent: event) } } override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { // Notify it's delegate about touched self.delegatePass?.touchMoved() if self.dragging == true { self.nextResponder()?.touchesMoved(touches, withEvent: event) } else { super.touchesMoved(touches, withEvent: event) } } }
你的class
ViewController
必须按以下方式实现:class ViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var scrollView: PassTouchesScrollView! override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self scrollView.delegatePass = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func touchMoved() { println("touch moved") } func touchBegan() { println("touch began") } }
您必须在界面生成器中 select 您的
UIScrollView
并将 class 设置为您的 classPassTouchesScrollView
, 在 class 部分的 Identity Inspector 中。
您应该会在控制台中看到以下内容:
touch began
touch began
touch began
touch began
touch began
touch began
touch moved
touch move
希望对你有所帮助。
试试这个
extension UIScrollView {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesBegan(touches, with: event)
print("touchesBegan")
}
override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesMoved(touches, with: event)
print("touchesMoved")
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesEnded(touches, with: event)
print("touchesEnded")
}
}