Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改UICollectionview单元XY位置-swift 3_Ios_Swift_Swift3_Uicollectionview_Xcode8 - Fatal编程技术网

Ios 更改UICollectionview单元XY位置-swift 3

Ios 更改UICollectionview单元XY位置-swift 3,ios,swift,swift3,uicollectionview,xcode8,Ios,Swift,Swift3,Uicollectionview,Xcode8,我正在使用带有XX个单元格的UICollectionview。如何以编程方式更改每个单元格上的XY位置?我可以通过以下方式更改每个单元格的大小: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 是否有其他类似于上述的功能可以更改X

我正在使用带有XX个单元格的UICollectionview。如何以编程方式更改每个单元格上的XY位置?我可以通过以下方式更改每个单元格的大小:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

是否有其他类似于上述的功能可以更改XY定位?

我希望此示例将对您有所帮助。让我解释一下这个代码块。您可以考虑标准UICollectionView和单元设计。没有详细视图。此代码实现将创建视差效果HeaderView

首要的

import UIKit

class DIYLayoutAttributes: UICollectionViewLayoutAttributes {

  var deltaY: CGFloat = 0

  override func copy(with zone: NSZone?) -> Any {
    let copy = super.copy(with: zone) as! DIYLayoutAttributes
    copy.deltaY = deltaY
    return copy
  }

  override func isEqual(_ object: Any?) -> Bool {
    if let attributes = object as? DIYLayoutAttributes {
      if attributes.deltaY == deltaY {
        return super.isEqual(object)
      }
    }
    return false
  }

}

class DIYLayout: UICollectionViewFlowLayout {
    var maximumStretchHeight: CGFloat = 0

    override class var layoutAttributesClass : AnyClass {
        return DIYLayoutAttributes.self
    }


  override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    // Create layout attributes
    let layoutAttributes = super.layoutAttributesForElements(in: rect) as! [DIYLayoutAttributes]

    // How far user scrollview.
    let insets = collectionView!.contentInset
    let offset = collectionView!.contentOffset

    let minY = -insets.top

    // we need the calculate delta.
    if (offset.y < minY) {
      let deltaY = fabs(offset.y - minY)

        // we can loop through all attributes
      for attributes in layoutAttributes {

        //Manipulate attributes
        if let elementKind = attributes.representedElementKind {
          if elementKind == UICollectionElementKindSectionHeader {
            // create frame.
            var frame = attributes.frame
            frame.size.height = max(minY, headerReferenceSize.height + deltaY)

            //We need the change origin for scrolling.
            frame.origin.y = frame.minY - deltaY
            attributes.frame = frame
            attributes.deltaY = deltaY
          }
        }
      }
    }
    return layoutAttributes
  }

    // Layout will scroll.
  override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    return true
  }
第三

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ScheduleHeader", for: indexPath) as! ScheduleHeaderView
    return header
  }
当然我们需要从viewDidLoad下载。

  override func viewDidLoad() {
    super.viewDidLoad()

    //Add layout.

    let width = collectionView!.bounds.width
    let layout = collectionViewLayout as! DIYLayout
    layout.headerReferenceSize = CGSize(width: width, height: 180)
    layout.itemSize = CGSize(width: width, height: 62)
    layout.maximumStretchHeight = width
  }

我希望这个例子能对你有所帮助。让我解释一下这个代码块。您可以考虑标准UICollectionView和单元设计。没有详细视图。此代码实现将创建视差效果HeaderView

首要的

import UIKit

class DIYLayoutAttributes: UICollectionViewLayoutAttributes {

  var deltaY: CGFloat = 0

  override func copy(with zone: NSZone?) -> Any {
    let copy = super.copy(with: zone) as! DIYLayoutAttributes
    copy.deltaY = deltaY
    return copy
  }

  override func isEqual(_ object: Any?) -> Bool {
    if let attributes = object as? DIYLayoutAttributes {
      if attributes.deltaY == deltaY {
        return super.isEqual(object)
      }
    }
    return false
  }

}

class DIYLayout: UICollectionViewFlowLayout {
    var maximumStretchHeight: CGFloat = 0

    override class var layoutAttributesClass : AnyClass {
        return DIYLayoutAttributes.self
    }


  override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    // Create layout attributes
    let layoutAttributes = super.layoutAttributesForElements(in: rect) as! [DIYLayoutAttributes]

    // How far user scrollview.
    let insets = collectionView!.contentInset
    let offset = collectionView!.contentOffset

    let minY = -insets.top

    // we need the calculate delta.
    if (offset.y < minY) {
      let deltaY = fabs(offset.y - minY)

        // we can loop through all attributes
      for attributes in layoutAttributes {

        //Manipulate attributes
        if let elementKind = attributes.representedElementKind {
          if elementKind == UICollectionElementKindSectionHeader {
            // create frame.
            var frame = attributes.frame
            frame.size.height = max(minY, headerReferenceSize.height + deltaY)

            //We need the change origin for scrolling.
            frame.origin.y = frame.minY - deltaY
            attributes.frame = frame
            attributes.deltaY = deltaY
          }
        }
      }
    }
    return layoutAttributes
  }

    // Layout will scroll.
  override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    return true
  }
第三

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ScheduleHeader", for: indexPath) as! ScheduleHeaderView
    return header
  }
当然我们需要从viewDidLoad下载。

  override func viewDidLoad() {
    super.viewDidLoad()

    //Add layout.

    let width = collectionView!.bounds.width
    let layout = collectionViewLayout as! DIYLayout
    layout.headerReferenceSize = CGSize(width: width, height: 180)
    layout.itemSize = CGSize(width: width, height: 62)
    layout.maximumStretchHeight = width
  }

您可能需要将
UICollectionViewLayout
UICollectionViewFlowLayout
子类化,并在其中实现自定义布局。您的确切用例是什么?也许可以通过调整布局参数来实现?你看过文件了吗?我已经看过100遍了。我是swift新手,所以创建任何“自定义”子类都非常耗时。但我愿意学习,如果你能给我指出正确的方向,我将非常感激:)用例:24幅图片,在空白的白色画布/海报上有自定义位置。用户可以更改位置、图像等。啊,好的。我不会为此使用
UICollectionView
——此控件更多地是关于某种结构化布局。我宁愿使用
uigesturecognizer
s添加24个独立的
UIImageView
s,以启用移动/缩放/旋转。这将给你完全的自由来放置你想要的图片。图片可以从2到24张不等。所以我只是以编程方式添加imageviews?您可能需要子类化
UICollectionViewLayout
UICollectionViewFlowLayout
,并在那里实现自定义布局。您的确切用例是什么?也许可以通过调整布局参数来实现?你看过文件了吗?我已经看过100遍了。我是swift新手,所以创建任何“自定义”子类都非常耗时。但我愿意学习,如果你能给我指出正确的方向,我将非常感激:)用例:24幅图片,在空白的白色画布/海报上有自定义位置。用户可以更改位置、图像等。啊,好的。我不会为此使用
UICollectionView
——此控件更多地是关于某种结构化布局。我宁愿使用
uigesturecognizer
s添加24个独立的
UIImageView
s,以启用移动/缩放/旋转。这将给你完全的自由来放置你想要的图片。图片可以从2到24张不等。所以我只是以编程方式添加ImageView?哇,Durul,让我试试,几天后再回来!:-)哇,杜鲁尔,让我试试,几天后再回来!:-)