Swift for&;中的UIScrollView子类;感动
我用的是Swift 1.2 我试着用这个和接受答案的第二条评论中的链接 我正在使用自动布局的故事板,我在自定义类中为UIScrollView设置了自定义类 在包含自定义UIScrollView的UIViewController中,我没有接收到这些触摸事件 编辑:用@Victor Sigler的答案更新了我的代码以了解如何使用它。 自定义滚动视图代码:Swift for&;中的UIScrollView子类;感动,swift,uiscrollview,touchesbegan,Swift,Uiscrollview,Touchesbegan,我用的是Swift 1.2 我试着用这个和接受答案的第二条评论中的链接 我正在使用自动布局的故事板,我在自定义类中为UIScrollView设置了自定义类 在包含自定义UIScrollView的UIViewController中,我没有接收到这些触摸事件 编辑:用@Victor Sigler的答案更新了我的代码以了解如何使用它。 自定义滚动视图代码: import UIKit protocol PassTouchesScrollViewDelegate { func scrollTouchB
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)
}
}
导入UIKit
协议passToucheScrollViewDelegate{
func ScrollTouchStart(触摸:设置,withEvent事件:UIEvent)
func scrollTouchMoved(触摸:设置,withEvent事件:UIEvent)
}
类passToucheScrollView:UIScrollView{
var delegatePass:PassToucheScrollViewDelegate?
重写初始化(帧:CGRect){
super.init(frame:frame)
}
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
self.delegatePass?.ScrollTouchStart(触摸,withEvent:event)
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
self.delegatePass?.scrollTouchMoved(触摸,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)")
}
}
导入UIKit
类ViewController:UIViewController、PassToucheScrollViewDelegate{
@iBCROLLVIEW:UIScrollView!
重写func viewDidLoad(){
super.viewDidLoad()
scrollView.delegatePass=self
}
func ScrollTouchStart(触摸:设置,withEvent事件:UIEvent){
println(“开始\(触摸)”)
}
func scrollTouchMoved(触摸:设置,withEvent事件:UIEvent){
println(“移动\(触摸)”)
}
}
我试图让用户在UIImage上画一条线,我使用PanGesture识别器进行了工作,但性能非常差,尤其是在旧硬件上,我遵循Ray Wenderlich教程,使用Touchs开始,性能要好得多,但它是在UIView而不是ScrollView上。我需要一个UIScrollView,因为在用户在图像上绘图之前,他们可以放大和环绕图像
touchsbegind
仅适用于UIView
的子类。将其从UIViewController中取出以使其工作
你想得不对。如果您想知道
UIScrollView
何时移动,则无需对其进行子类化。iOS已经在UIScrollViewDelegate
中设置了您需要的所有方法
您必须实现,以便在UIScrollView
中获得有关操作的通知,并通过界面生成器或在代码中设置委托
例如,请参见以下示例了解如何执行此操作:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self
}
}
但是,如果您想知道它是如何以您上面解释的方式被触动的,您必须遵循以下步骤:
您必须像在类PassTouchesScrollView
中那样,从UIScrollView
类中进行子类化,并以以下方式实现代理模式,以获得有关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: 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")
}
}
您必须在界面生成器中,在类部分的标识检查器中选择您的UIScrollView
,并将其类设置为您的类PassTouchesScrollView
您应该在控制台中看到以下内容:
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{
覆盖open func ToucheSBegind(Touchs:Set,带有事件:UIEvent?){
self.next?触摸开始(触摸,带有:事件)
打印(“触摸开始”)
}
覆盖打开的功能触摸移动(touch:Set,带有事件:UIEvent?){
self.next?.touchesMoved(触摸,带有:事件)
打印(“触摸移动”)
}
覆盖open func touchesEnded(Touchs:Set,带有事件:UIEvent?){
self.next?.touchesend(触摸,带有:事件)
打印(“触摸已粘贴”)
}
}
我被这个答案弄糊涂了,当我触摸UIScrollView外部屏幕上的某个区域时,会调用touchesBegind和touchesMoved,你的意思是我应该将UIScrollView放在UIView内部吗?我是说,委托方法touchesBegind应该只在UIView子类内部触发。将它们放置在UIViewController中不应触发这些方法。是的,如果您将这些方法放在UIScrollView中,也会起作用。只是不在控制器中。UIViewController确实包含它自己的UIView,这就是为什么当我触摸UIScrollView外部的屏幕时它会做出响应(它的框架是600x200),在UIScrollView的自定义子类中,我试图通过使用super.touchesBegind和super.touchesMoved将我放入其中的触摸事件传递到UIViewController的视图UIViewController确实包含自己的UIView,这就是为什么当我触摸UIScrollView外部的屏幕时它会做出响应(它的大小为600x400),在UIScrollView的自定义子类中,我试图通过使用super.touchesBegind和super.touchesMoved将我放入其中的触摸事件传递到UIViewController的视图。我在问题中添加了一个屏幕截图。当我触摸UIScrollView上方和下方的白色区域时,我会在TouchsBegind和TouchsMoved上获得println输出。我不想知道它何时移动,我想知道它何时被触摸,遗憾的是委托方法没有提供,当用户触摸滚动视图时,它的scrollEnabled属性被设置为false(以确保正确的缩放)。我不知道为什么,但这对我不起作用,这就是我所拥有的。@您在界面生成器中为UIScrollView
设置类的元素??非常重要的是,我要在Github中与上面解释的用户共享一个项目。我将向您发送链接以供您查看。经过几个小时的测试,我知道了解决方案:您忘记在ViewController中链接“PassToucheSCrollViewDelegate”。所以它应该是:“类ViewController:UIViewController,UIScrollViewDele