Swift 中的 UIScrollView 子类,用于触摸开始 &感动 [英] Subclass UIScrollView in Swift for touches Began & touches Moved

查看:16
本文介绍了Swift 中的 UIScrollView 子类,用于触摸开始 &感动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Swift 1.2

I'm using Swift 1.2

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

I'm trying to use this UIScrollview touchesbegan,touchesmoved,touchesended actions and the link in the 2nd comment of the accepted answer.

我正在使用具有自动布局的故事板,我在自定义类中为 UIScrollView 设置了自定义类.

I'm using a storyboard with auto layout, I set my custom class for the UIScrollView in Custom Class.

我没有在包含我的自定义 UIScrollView 的 UIViewController 中收到这些触摸事件

I'm not receiving either of these touch events in the UIViewController that contains my custom UIScrollView

更新了我的代码,了解我如何使用 @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)

}

}

我的视图控制器:

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 教程,该教程使用了触摸开始和性能要好得多,但它是在 UIView 而不是 ScrollView 上.我需要一个 UIScrollView ,因为在用户绘制图像之前,他们可以放大和围绕它.

I'm trying to let a user draw a line over a UIImage, which I got working using a PanGesture Recognizer but the performance was very poor, especially on older hardware, I followed a Ray Wenderlich tutorial that used touches Began and the performance was much better, however it was on a UIView and not a ScrollView. I need a UIScrollView since prior to a user drawing on an image they can zoom in and around it.

推荐答案

您的想法是错误的.如果您想知道 UIScrollView 何时移动,则无需对其进行子类化.iOS 已经在 UIScrollViewDelegate 中设置了你需要的所有方法.

You're thinking about this wrong. If you want to know when a UIScrollView is moving, there's no need to subclass it. iOS has set up all the methods you need inside of the UIScrollViewDelegate.

您必须实现 UISCrollViewDelegate 通知 UIScrollView 中的操作并通过 Interface Builder 或在代码中设置 delegate 由您决定.

You must implement the UISCrollViewDelegate to be notified about the actions in the UIScrollView and set the delegate by Interface Builder or in code, is up to you.

查看以下示例以了解如何执行示例:

See the following example to know how to do it for example :

class ViewController: UIViewController, UIScrollViewDelegate {

     @IBOutlet weak var scrollView: UIScrollView!

     override func viewDidLoad() {
       super.viewDidLoad()
       self.scrollView.delegate = self
     }
}

如果你想知道它是如何按照你上面解释的方式进行的,你必须按照以下步骤操作:

Still if you want to know how its touched in the way you explained above you must follow the following steps :

  1. 您必须像在您的类 PassTouchesScrollView 中一样从 UIScrollView 类中继承子类,并实现委托模式以接收有关 UIScrollView的方式如下:

  1. You must subclass from the UIScrollView class as you did in your class PassTouchesScrollView and implement the delegate pattern to be notified about the UIScrollView in the following way:

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)
     }
  }   
}

  • 你的类 ViewController 必须按以下方式实现:

  • Your class ViewController must be implemented in the following way:

    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")
      }
    
    }
    

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

  • You must be in the Interface Builder select your UIScrollView and set the class for it as your class PassTouchesScrollView, in the Identity Inspector in the class part.

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

    And you should see in your console the following:

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

    希望对您有所帮助.

    这篇关于Swift 中的 UIScrollView 子类,用于触摸开始 &amp;感动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆