gpt4 book ai didi

swift - Swift 中的 UIScrollView 子类用于触摸开始和触摸移动

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:41 24 4
gpt4 key购买 nike

enter image description here我正在使用 Swift 1.2

我正在尝试使用这个 UIScrollview touchesbegan,touchesmoved,touchesended actions以及已接受答案的第二条评论中的链接。

我正在使用具有自动布局的 Storyboard,我在自定义类中为 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)

}

}

我的 View Controller :

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,因为在用户绘制图像之前,他们可以放大图像并围绕它放大。

最佳答案

你想错了。如果您想知道 UIScrollView 何时移动,则无需对其进行子类化。 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
}
}

不过,如果您想知道它是如何以您上面解释的方式触及的,您必须按照以下步骤操作:

  1. 您必须像在类 PassTouchesScrollView 中那样从 UIScrollView 类创建子类,并实现委托(delegate)模式以接收有关 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)
    }
    }
    }
  2. 您的类 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")
    }

    }
  3. 您必须在 Interface Builder 中选择您的 UIScrollView 并将其类设置为您的类 PassTouchesScrollView,在类部分的身份检查器中。

您应该会在控制台中看到以下内容:

touch began
touch began
touch began
touch began
touch began
touch began
touch moved
touch move

希望对你有帮助

关于swift - Swift 中的 UIScrollView 子类用于触摸开始和触摸移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com