Можно ли использовать Uidynamicanimator с элементами в Scroll/Stackview, а не для представления коллекции?IOS

Программируем под IOS
Ответить Пред. темаСлед. тема
Anonymous
 Можно ли использовать Uidynamicanimator с элементами в Scroll/Stackview, а не для представления коллекции?

Сообщение Anonymous »

Скажите, что у вас есть вертикальный View , вы можете добавить типичную «пружинную» физику, включив Uidynamicanimator в uicollectionviewlayout .
Простой пример ниже. Uiscrollview
Боюсь, что я абсолютно понятия не имею, как добавить это динамическое поведение, к представлениям в прокрутке stack view .
Это возможно, или, существует ли uidynamicanimator только для элементов в uiCollectionView ?? Можно ли сделать это?// typical bouncy layout for UICollectionView

import UIKit

public class BouncyLayout: UICollectionViewFlowLayout {

var damping: CGFloat = 0.75
var frequency: CGFloat = 1.5

private lazy var animator: UIDynamicAnimator = UIDynamicAnimator(collectionViewLayout: self)

public override func prepare() {
super.prepare()
guard let view = collectionView, let attributes = super.layoutAttributesForElements(in: view.bounds)?.flatMap({ $0.copy() as? UICollectionViewLayoutAttributes }) else { return }

oldBehaviors(for: attributes).forEach { animator.removeBehavior($0) }
newBehaviors(for: attributes).forEach { animator.addBehavior($0, damping, frequency) }
}

private func oldBehaviors(for attributes: [UICollectionViewLayoutAttributes]) -> [UIAttachmentBehavior] {
let indexPaths = attributes.map { $0.indexPath }
return animator.behaviors.flatMap {
guard let behavior = $0 as? UIAttachmentBehavior, let itemAttributes = behavior.items.first as? UICollectionViewLayoutAttributes else { return nil }
return indexPaths.contains(itemAttributes.indexPath) ? nil : behavior
}
}

private func newBehaviors(for attributes: [UICollectionViewLayoutAttributes]) -> [UIAttachmentBehavior] {
let indexPaths = animator.behaviors.flatMap { (($0 as? UIAttachmentBehavior)?.items.first as? UICollectionViewLayoutAttributes)?.indexPath }
return attributes.flatMap { return indexPaths.contains($0.indexPath) ? nil : UIAttachmentBehavior(item: $0, attachedToAnchor: $0.center) }
}

public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return animator.items(in: rect) as? [UICollectionViewLayoutAttributes]
}

public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return animator.layoutAttributesForCell(at: indexPath) ?? super.layoutAttributesForItem(at: indexPath)
}

public override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
guard let view = collectionView else { return false }

animator.behaviors.forEach {
guard let behavior = $0 as? UIAttachmentBehavior, let item = behavior.items.first else { return }
update(behavior: behavior, and: item, in: view, for: newBounds)
animator.updateItem(usingCurrentState: item)
}
return view.bounds.width != newBounds.width
}

private func update(behavior: UIAttachmentBehavior, and item: UIDynamicItem, in view: UICollectionView, for bounds: CGRect) {
let delta = CGVector(dx: bounds.origin.x - view.bounds.origin.x, dy: bounds.origin.y - view.bounds.origin.y)
let resistance = CGVector(dx: fabs(view.panGestureRecognizer.location(in: view).x - behavior.anchorPoint.x) / 1000, dy: fabs(view.panGestureRecognizer.location(in: view).y - behavior.anchorPoint.y) / 1000)
item.center.y += delta.dy < 0 ? max(delta.dy, delta.dy * resistance.dy) : min(delta.dy, delta.dy * resistance.dy)
item.center.x += delta.dx < 0 ? max(delta.dx, delta.dx * resistance.dx) : min(delta.dx, delta.dx * resistance.dx)
}
}

extension UIDynamicAnimator {

open func addBehavior(_ behavior: UIAttachmentBehavior, _ damping: CGFloat, _ frequency: CGFloat) {
behavior.damping = damping
behavior.frequency = frequency
addBehavior(behavior)
}
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... iew-rather
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «IOS»