自定义视图在滚动视图内滚动

Posted

技术标签:

【中文标题】自定义视图在滚动视图内滚动【英文标题】:Custom view scrolls inside a scroll view 【发布时间】:2018-01-16 10:00:20 【问题描述】:

我创建了一个名为SignView 的自定义视图,并添加到另一个名为mainSignView 的自定义视图中,该视图位于一个名为getFormView 的滚动视图中,但是当我在signView 上签名时,它会滚动,我无法做任何手势来帮助我克服请这个!

这就是我所拥有的

    @objc func pressedSignViewAction(_ sender: UILabel) 

    self.signViewElement = "signView1"
    uniY += 60
    mainSignView = UIView(frame: CGRect(x: 20, y: uniY, width: 250, height: 250))
    mySignViewName = UILabel(frame: CGRect(x: 20, y: 10, width: 200, height: 30))
    let signLabel = UILabel(frame: CGRect(x: 20, y: 50, width: 200, height: 30))
    signView  = YPDrawSignatureView(frame: CGRect(x: 10, y: 90, width: 230, height: 100))
    signView.backgroundColor = .white

    signLabel.text = "Signature"
    mainSignView.backgroundColor = .green

    mySignViewName.text = self.signViewNameFD
    self.signViewNameElement = self.signViewNameFD

   cancelButton = UIButton(frame : CGRect(x: 20, y: 200, width: 100, height: 25))
    cancelButton.addTarget(self, action: #selector(pressedSignClearAction(_:)), for: .touchUpInside)
    cancelButton.setTitle("Clear",for: .normal)
    cancelButton.setTitleColor(UIColor.red, for: .normal)


    uniY += 170


    self.view.addSubview(getFormView)
    //self.getFormView.addSubview(signLabel)
    self.getFormView.addSubview(mainSignView)
    self.mainSignView.addSubview(signLabel)
    self.mainSignView.addSubview(signView)
    self.mainSignView.addSubview(mySignViewName)
    self.mainSignView.addSubview(cancelButton)

    self.enterSignViewTitle()
    self.updateScrollY()


@objc func pressedSignClearAction(_ sender: UIButton) 

self.signView.clear()


【问题讨论】:

【参考方案1】:

来自YPDrawSignatureView Documentation:

// MARK: - Delegate Methods

// The delegate functions gives feedback to the instanciating class. All functions are optional,
// meaning you just implement the one you need.

// didStart() is called right after the first touch is registered in the view.
// For example, this can be used if the view is embedded in a scroll view, temporary
// stopping it from scrolling while signing.
func didStart() 
    print("Started Drawing")
    yourScroll.isScrollEnabled = false


// didFinish() is called rigth after the last touch of a gesture is registered in the view.
// Can be used to enable scrolling in a scroll view if it has previous been disabled.
func didFinish() 
    print("Finished Drawing")
    yourScroll.isScrollEnabled = true

【讨论】:

您是否已将 YPSignatureDelegate 添加到您的 UIViewController,并为您的 signView 设置它?

以上是关于自定义视图在滚动视图内滚动的主要内容,如果未能解决你的问题,请参考以下文章

onDraw 滚动视图中的自定义视图

自定义滚动谷歌颤动中的水平列表视图

UITableView 中滚动的自定义视图

自定义滚动视图在 Sliver Persistent Header 下滚动

带有自定义表格视图单元格的表格视图在滚动时消失

自定义 UIView 添加到视图后 UICollectionView 不滚动