Скажите, что у вас есть вертикальный 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
Можно ли использовать Uidynamicanimator с элементами в Scroll/Stackview, а не для представления коллекции? ⇐ IOS
Программируем под IOS
1748433222
Anonymous
Скажите, что у вас есть вертикальный [b] View [/b], вы можете добавить типичную «пружинную» физику, включив Uidynamicanimator в uicollectionviewlayout .
Простой пример ниже. Uiscrollview
Боюсь, что я абсолютно понятия не имею, как добавить это динамическое поведение, к представлениям в прокрутке [b] stack view [/b].
Это возможно, или, существует ли 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)
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79642138/is-it-possible-to-use-uidynamicanimator-with-items-in-a-scroll-stackview-rather[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия