Like the title
is like this
The effect I want to achieve is the process of
sliding down, such as the picture below
This is made with tableview. Now slide it here and it will not move. What I want to achieve is that when the blue color does not reach half of the screen (approximately that position will work), it will automatically return to the first row, that is, only The red line, if it exceeds the limit, is as shown below
will automatically slide to the blue row. How to implement this change is more convenient? Does tableview or scrollview have direct attribute control? Please answer. Thank you
欧阳克2017-06-20 10:08:07
You are talking about card layout, you can use UICollectionView and custom UICollectionViewFlowLayout to achieve it, Demo address
// 修改cell滑动时大小
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attrs = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:self.collectionView.bounds] copyItems:YES];
CGFloat scale;
CGFloat offset;
for (UICollectionViewLayoutAttributes *attr in attrs) {
// 设置collection在滑动时, cell的大小
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
offset = fabs(attr.center.x - self.collectionView.contentOffset.x - self.collectionView.bounds.size.width * 0.5);
scale = 1 - offset / (self.collectionView.bounds.size.width * 0.5) * 0.25;
attr.transform = CGAffineTransformMakeScale(scale, scale);
} else {
offset = fabs(attr.center.y - self.collectionView.contentOffset.y - self.collectionView.bounds.size.height * 0.5);
scale = 1 - offset / (self.collectionView.bounds.size.height * 0.5) * 0.25;
}
attr.transform = CGAffineTransformMakeScale(scale, scale);
}
return attrs;
}
// 返回collection滑动手指松开后, collection最终的contentOffset
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
// cell 宽度
CGFloat itemW = self.itemSize.width;
// cell边框距离(未设置collection的contentInset, 默认为0)
CGFloat margin = self.collectionView.contentInset.left;
// collection在手指松开后,collection滑动到停止时, collectioncontentOffset
CGFloat index = roundf((proposedContentOffset.x + margin) / itemW);
// 修改collection最终的contentOffset, 使collection滑动到cell中间
proposedContentOffset.x = index * self.collectionView.bounds.size.width - (index + 1) * margin;
} else {
CGFloat itemH = self.itemSize.height;
CGFloat margin = self.collectionView.contentInset.top;
CGFloat index = roundf((proposedContentOffset.y + margin) / itemH);
proposedContentOffset.y = index * self.collectionView.bounds.size.height - (index+1) * margin;
}
return proposedContentOffset;
}