如何在自定义uiview中添加scrollview,以便添加uiview将滚动

时间:2016-02-11 06:44:59

标签: ios swift uiview uiscrollview

以下是我从

引用的自定义视图中使用的代码

PeriscommentView

但是我想显示整个单元格并且用户可以滚动,我通过注释一些代码取消隐藏单元格但是我不知道如何向此视图添加滚动视图以便添加到其中的单元格可以滚动。

import UIKit

public class PeriscommentView: UIView {

private var visibleCells: [PeriscommentCell] = []
private var config: PeriscommentConfig

convenience override init(frame: CGRect) {
    let config = PeriscommentConfig()
    self.init(frame: frame, config: config)
}

required public init(coder aDecoder: NSCoder) {
    self.config = PeriscommentConfig()
    super.init(coder: aDecoder)!
}

init(frame: CGRect, config: PeriscommentConfig) {
    self.config = config
    super.init(frame: frame)
}

override public func willMoveToSuperview(newSuperview: UIView?) {
    super.willMoveToSuperview(newSuperview)
    setupView()
}

private func setupView() {
    self.backgroundColor = config.layout.backgroundColor
}

public func addCell(cell: PeriscommentCell) {
    cell.frame = CGRect(origin: CGPoint(x: 0, y: self.frame.height), size: cell.frame.size)
    visibleCells.append(cell)
    self.addSubview(cell)

    UIView.animateWithDuration(self.config.appearDuration, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
        let dy = cell.frame.height + self.config.layout.cellSpace
        for c in self.visibleCells {
            let origin = c.transform
            let transform = CGAffineTransformMakeTranslation(0, -dy)
            c.transform = CGAffineTransformConcat(origin, transform)
        }
    }, completion: nil)

    UIView.animateWithDuration(self.config.disappearDuration, delay: self.config.appearDuration, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
       // cell.alpha = 0.0
    }) { (Bool) -> Void in
       // cell.removeFromSuperview()
       // self.visibleCells.removeAtIndex(self.visibleCells.indexOf(cell)!)
    }
}

public func addCell(profileImage: UIImage, name: String, comment: String) {
    let rect = CGRect.zero
    let cell = PeriscommentCell(frame: rect, profileImage: profileImage, name: name, comment: comment, config: self.config)

    self.addCell(cell)
}

}

我已尝试编辑代码并将其浮动视图但是我不想要它我想要一个正确的滚动。

已编辑的代码

import UIKit

public class PeriscommentView: UIView {

private var visibleCells: [PeriscommentCell] = []
private var config: PeriscommentConfig

var scrollView: UIScrollView!
var containerView = UIView()

convenience override init(frame: CGRect) {
    let config = PeriscommentConfig()
    self.init(frame: frame, config: config)
    self.scrollView = UIScrollView()
    scrollView.frame = CGRectMake(0, 0, 600, self.bounds.height)
    self.scrollView.contentSize = CGSize(width:600, height: 1200)
    self.scrollView.contentInset = UIEdgeInsets(top: self.bounds.height, left:  0.0, bottom: 0.0, right: 0.0)


    containerView = UIView()
    containerView.frame = CGRectMake(0, 0, 600, 1000)

    self.scrollView.userInteractionEnabled = true
    scrollView.addSubview(containerView)
    self.addSubview(scrollView)
}

required public init(coder aDecoder: NSCoder) {
    self.config = PeriscommentConfig()
    super.init(coder: aDecoder)!
    self.scrollView = UIScrollView()
    scrollView.frame = CGRectMake(0, 0, 600, self.bounds.height)
    self.scrollView.contentSize = CGSize(width:600, height: 1200)
    self.scrollView.contentInset = UIEdgeInsets(top: self.bounds.height, left:  0.0, bottom: 0.0, right: 0.0)

    //self.scrollView.contentOffset = CGPoint(x: 0.0, y: -100.0)


    containerView = UIView()
    containerView.frame = CGRectMake(0, 0, 600, 1000)

    self.scrollView.userInteractionEnabled = true
    scrollView.addSubview(containerView)
    self.addSubview(scrollView)
}
init(frame: CGRect, config: PeriscommentConfig) {
    self.config = config
    super.init(frame: frame)
    self.scrollView = UIScrollView()
    scrollView.frame = CGRectMake(0, 0, 600, self.bounds.height)
    self.scrollView.contentSize = CGSize(width:600, height: 1200)
    self.scrollView.contentInset = UIEdgeInsets(top: self.bounds.height, left:  0.0, bottom: 0.0, right: 0.0)


    //self.scrollView.contentOffset = CGPoint(x: 0.0, y: -100.0)


    containerView = UIView()
    containerView.frame = CGRectMake(0, 0, 600, 1000)

    self.scrollView.userInteractionEnabled = true
    scrollView.addSubview(containerView)
    self.addSubview(scrollView)

}

override public func willMoveToSuperview(newSuperview: UIView?) {
    super.willMoveToSuperview(newSuperview)
    setupView()
}

private func setupView() {
    self.backgroundColor = config.layout.backgroundColor
}

public func addCell(cell: PeriscommentCell) {
    cell.frame = CGRect(origin: CGPoint(x: 0, y: self.frame.height), size: cell.frame.size)
    visibleCells.append(cell)
    self.containerView.addSubview(cell)

    UIView.animateWithDuration(self.config.appearDuration, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
        let dy = cell.frame.height + self.config.layout.cellSpace
        for c in self.visibleCells {
            let origin = c.transform
            let transform = CGAffineTransformMakeTranslation(0, -dy)
            c.transform = CGAffineTransformConcat(origin, transform)
        }
        }, completion: nil)

    UIView.animateWithDuration(self.config.disappearDuration, delay: self.config.appearDuration, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
        // cell.alpha = 0.0
        }) { (Bool) -> Void in
            // cell.removeFromSuperview()
            // self.visibleCells.removeAtIndex(self.visibleCells.indexOf(cell)!)
    }
}

public func addCell(profileImage: UIImage, name: String, comment: String) {
    let rect = CGRect.zero
    let cell = PeriscommentCell(frame: rect, profileImage: profileImage, name: name, comment: comment, config: self.config)

    self.addCell(cell)
}

} 

0 个答案:

没有答案